fgc-google-experiments-bk.php 13 KB
<?php

add_filter('woocommerce_general_settings', 'add_experiments_ID_setting');

function add_experiments_ID_setting($settings) {
    $updated_settings = array();
    foreach ($settings as $section) {
// at the bottom of the General Options section
        if (isset($section['id']) && 'general_options' == $section['id'] &&
                isset($section['type']) && 'sectionend' == $section['type']) {
            $updated_settings[] = array(
                'name' => 'Experiment ID',
                'desc_tip' => 'Experiment ID',
                'id' => 'woocommerce_google_experiment_id',
                'type' => 'text',
                'css' => 'min-width:300px;',
                'std' => '05JR-AqkRqWBOVdeQicVGA', // WC < 2.0
                'default' => '05JR-AqkRqWBOVdeQicVGA', // WC >= 2.0
                'desc' => 'Sample ID: 05JR-AqkRqWBOVdeQicVGA',
            );
        }
        $updated_settings[] = $section;
    }
    return $updated_settings;
}

require( 'Experiment.php' );

class Fgc_Google_Experiments {

    public static function get_products_for_slider($isSubscription) {
        $type = self::get_type_for_slider();
        if ($isSubscription) {
            $sku = self::get_sku_by_type($type);
            self::get_subscription_product_for_slider($sku);
        } else {
            $category = self::get_category_by_type($type);
            self::get_single_products_for_slider($category);
        }
    }

    public static function get_type_for_slider() {
        if ($type = self::get_type_for_user()) { // Got type from order of user
        } else { // User don't have any order
//            $_gaCookie = $_COOKIE['_ga'];
            $google_experiment_id = self::get_google_experiment_id_cookie();
            $type = self::get_type_on_database_by_experiment_cookie($google_experiment_id);
            if ($type != false) { // User have type which's save on database with _ga cookie
            } else { // New user who doesn't have any type, need create by Google Experiment then update to database
                $type = self::get_and_update_experiment_value($google_experiment_id);
                if ($type != false) { // Get and update successful variation key and type to database
                } else {
                    $type = 'D';
                }
            }
        }
        return $type;
    }

    public static function create_experiment_at_first_load() {
        if (get_current_user_id() == 0 && !isset($_SESSION['_google_experiment_id'])) {
            $google_experiment_id = self::get_google_experiment_id_cookie();
            $_SESSION['_google_experiment_id'] = $google_experiment_id;
            self::get_and_update_experiment_value($google_experiment_id);
        }
    }

    public static function get_subscription_product_for_slider($sku) {
        echo WC_Shortcodes::product_page(array('sku' => $sku));
    }

    public static function get_single_products_for_slider($category) {
        echo WC_Shortcodes::product_category(array("category" => $category, "per_page" => "16", "orderby" => "price", "order" => "asc"));
    }

    public static function get_value_of_experiment_by_exiperiment_cookie($google_experiment_id) {
        global $wpdb;
        $select_get_type = "SELECT * FROM `" . $wpdb->prefix . "fgc_google_experiment` WHERE `GA_ID` = '" . $google_experiment_id . "'";
        $results = $wpdb->get_results($select_get_type);
        if (empty($results)) { // The customer doesn't have any order
            return false;
        } else { // The customer has order.
            return $results;
        }
    }

    public static function get_type_on_database_by_experiment_cookie($google_experiment_id) {
        if ($result = self::get_value_of_experiment_by_exiperiment_cookie($google_experiment_id)) {
            return $result[0]->type;
        }
        return false;
    }

    public static function get_variation_key_on_database_by_exiperiment_cookie($google_experiment_id) {
        if ($result = self::get_value_of_experiment_by_exiperiment_cookie($google_experiment_id)) {
            return $result[0]->var_key;
        }
        return false;
    }

    public static function get_and_update_experiment_value($google_experiment_id) {
        if ($google_experiment_id != false) {
            $user_id = (get_current_user_id() > 0) ? $user_id : '';
            $variationKey = self::get_variation_by_google_experiment();
            $type = self::get_type_by_variation_key($variationKey);
            global $wpdb;
            $query_insert = "INSERT INTO `" . $wpdb->prefix . "fgc_google_experiment` (`GA_ID`, `var_key`, `type`,`user_id`) VALUES ('$google_experiment_id', $variationKey , '$type','$user_id');";
            $result = $wpdb->query($query_insert);
            if ($result) {
                return $type;
            }
        }
        return false;
    }

    public static function get_type_for_user() {
        global $wpdb;
        $user_id = get_current_user_id();
        if ($user_id > 0) { // User logged in
            $select_get_orders = "SELECT * FROM `" . $wpdb->prefix . "postmeta` WHERE `meta_key` = '_customer_user' AND `meta_value` = " . $user_id;
            $results = $wpdb->get_results($select_get_orders);
//            var_dump($results);
            if (empty($results)) { // The customer doesn't have any order
                if (self::get_value_of_experiment_by_user_id($user_id)) { // User tung login roi
                    // check ga co giong nhau ko
                    self::update_cookie_after_login($user_id);
                } else {
                    self::update_user_id_on_db_after_login($user_id);
                }
                return self::get_value_of_experiment_by_user_id($user_id)->type;
            } else { // The customer has order.
                return $type = self::get_type_of_order($results[0]->post_id);
            }
        } else { // User didn't log in
            return false;
        }
    }

    public static function get_value_of_experiment_by_user_id($user_id) {
        global $wpdb;
        $select = "SELECT * FROM `" . $wpdb->prefix . "fgc_google_experiment` WHERE `user_id` = " . $user_id;
        $results = $wpdb->get_results($select);
        if (empty($results)) { // The customer doesn't have any order
            return false;
        } else { // The customer has order.
            return $results[0];
        }
    }

    public static function update_user_id_on_db_after_login($user_id) {
        $google_experiment_id = self::get_google_experiment_id_cookie();
        global $wpdb;
        $query_update = "UPDATE `" . $wpdb->prefix . "fgc_google_experiment` SET `user_id` = " . $user_id . " WHERE `GA_ID` = '" . $google_experiment_id . "'";
        $result_update = $wpdb->query($query_update);
        if ($result_update) {
            return true;
        } else {
            return false;
        }
    }

    public static function update_cookie_after_login($user_id) {
        $google_experiment_id = self::get_google_experiment_id_cookie();
        $google_experiment_id_from_db = self::get_value_of_experiment_by_user_id($user_id)->GA_ID;
        if ($google_experiment_id != $google_experiment_id_from_db) {
            setcookie('google_experiment_id', $google_experiment_id_from_db, time() + 365 * 24 * 60 * 60);
        }
    }

    public static function get_user_id_from_experiment_by_google_experiment_id($google_experiment_id) {
        if ($result = self::get_value_of_experiment_by_exiperiment_cookie($google_experiment_id)) {
            $user_id = $result[0]->user_id;
            if ($user_id != 0) {
                return $user_id;
            } else {
                return false;
            }
        }
        return false;
    }

    public static function get_type_of_order($order_id) {
        $order = new WC_Order($order_id);
        $items = $order->get_items();

        foreach ($items as $key => $item) {
            $item_meta_array = $item['item_meta_array'];
            break;
        }
        foreach ($item_meta_array as $key => $value) {
            if ($value->key == '_variation_id') {
                $product_variation_id = $value->value;
            }
            if ($value->key == '_product_id') {
                $product_id = $value->value;
            }
        }
        if ($product_variation_id) {
            $product = new WC_Product($product_id);
            $sku = $product->get_sku();
            return $type = self::get_type_by_sku($sku);
        } else {
            $product = new WC_Product($product_id);
            $category = $product->get_categories();
            return $type = self::get_type_by_category($category);
        }
    }

    public static function get_type_by_sku($sku) {
        switch ($sku) {
            case 'REIZEA':
                return 'A';
            case 'REIZEB':
                return 'B';
            case 'REIZEC':
                return 'C';
            case 'REIZE1':
                return 'D';
            default:
                return 'D';
        }
    }

    public static function get_sku_by_type($type) {
        switch ($type) {
            case 'A':
                return 'REIZEA';
            case 'B':
                return 'REIZEB';
            case 'C':
                return 'REIZEC';
            case 'D':
                return 'REIZE1';
            default:
                return 'REIZE1';
        }
    }

    public static function get_type_by_category($category) {
        switch ($category) {
            case 'Single Purchase A':
                return 'A';
            case 'Single Purchase B':
                return 'B';
            case 'Single Purchase C':
                return 'C';
            case 'Single Purchase':
                return 'D';
            default:
                return 'D';
        }
    }

    public static function get_category_by_type($type) {
        switch ($type) {
            case 'A':
                return 'Single Purchase A';
            case 'B':
                return 'Single Purchase B';
            case 'C':
                return 'Single Purchase C';
            case 'D':
                return 'Single Purchase';
            default:
                return 'Single Purchase';
        }
    }

    public static function get_type_by_variation_key($variationKey) {
        switch ($variationKey) {
            case 0:
                return 'A';
            case 1:
                return 'B';
            case 2:
                return 'C';
            default:
                return 'D';
        }
    }

    public static function get_variation_key_by_type($type) {
        switch ($type) {
            case 'A':
                return 0;
            case 'B':
                return 1;
            case 'C':
                return 2;
            default:
                return -1;
        }
    }

    public static function get_variation_by_google_experiment() {
        $google_experiment_id = get_option('woocommerce_google_experiment_id');
        $experiment = new \UnitedPrototype\GoogleAnalytics\Experiment($google_experiment_id);
//        return -1;
        return $variationKey = $experiment->chooseVariation();
    }

    public static function get_sku_for_get_started_page() {
        $type = self::get_type_for_slider();
        return $sku = self::get_sku_by_type($type);
    }

    public static function get_google_experiment_id_cookie() {
        if (isset($_SESSION['_google_experiment_id'])) {
            return $_SESSION['_google_experiment_id'];
        } else {
            $google_experiment_id = self::generate_key_for_google_experiment(16);
            setcookie('google_experiment_id', $google_experiment_id, time() + 365 * 24 * 60 * 60);
            $_SESSION['_google_experiment_id'] = $google_experiment_id;
            return $google_experiment_id;
        }
    }

    public static function generate_key_for_google_experiment($length) {
        $characters = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
        $charactersLength = strlen($characters);
        $randomString = '';
        for ($i = 0; $i < $length; $i++) {
            $randomString .= $characters[rand(0, $charactersLength - 1)];
        }
        return $randomString;
    }

}

function test_google_experiment() {
    if (isset($_REQUEST['testgoogleexperiment'])) {
//        Fgc_Google_Experiments::get_type_for_slider();

        var_dump(Fgc_Google_Experiments::get_variation_by_google_experiment());
        exit();
    }
}

//add_action('init', 'test_google_experiment');
add_action('init', 'Fgc_Google_Experiments::create_experiment_at_first_load');

global $wpdb;
$google_experiment_db_version = '1.6';

$google_experiment_installed_ver = get_option("google_experiment_db_version");

if ($google_experiment_installed_ver != $google_experiment_db_version) {

    $table_name_new1 = $wpdb->prefix . 'fgc_google_experiment_by_ip';

    $sql1 = "CREATE TABLE IF NOT EXISTS `$table_name_new1` (
  `id` BIGINT(20) NOT NULL AUTO_INCREMENT,
  `IpAddress` VARCHAR(255)  NULL,
  `var_key` BIGINT(20) NULL,
  `user_id` BIGINT(20) NULL,
  `type` VARCHAR(255) NULL,
  PRIMARY KEY (`id`));";

    require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
    dbDelta($sql1);

    update_option("google_experiment_db_version", $google_experiment_db_version);
}