fgc-google-experiments-bk.php
13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
<?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);
}