fgc-google-experiments-bkip.php
11.4 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
<?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 getTableName() {
return "fgc_google_experiment_by_ip";
}
public static function getVarKeyType() {
return array(0 => 'A', 1 => 'B', 2 => 'C');
}
public static function getTypeVarKey() {
return array('A' => 0, 'B' => 1, 'C' => 2, 'D' => -1);
}
public static function getTypeSku() {
return array('A' => 'REIZEA', 'B' => 'REIZEB', 'C' => 'REIZEC', 'D' => 'REIZE1');
}
public static function getSkuType() {
return array('REIZEA' => 'A', 'REIZEB' => 'B', 'REIZEC' => 'C', 'REIZE1' => 'D');
}
public static function getTypeCategory() {
return array('A' => 'Single Purchase A', 'B' => 'Single Purchase B', 'C' => 'Single Purchase C', 'D' => 'Single Purchase');
}
public static function getCategoryType() {
return array('Single Purchase A' => 'A', 'Single Purchase B' => 'B', 'Single Purchase C' => 'C', 'Single Purchase' => 'D');
}
public static function getIpAddress() {
// if ($_REQUEST['ipaddress']) {
// return $_REQUEST['ipaddress'];
// }
return $_SERVER['REMOTE_ADDR'];
// return $mac;
}
public static function init() {
$ip_address = self::getIpAddress();
$user_id = get_current_user_id();
$current_ip_address_info = self::get_google_experiment_data_by_ipAddress($ip_address);
if ($current_ip_address_info) { // Da co trong db
if ($user_id > 0) {
if ($current_ip_address_info->user_id == '0') { // Chua cap nhat vao db, can cap nhat vao db
if ($old_ip_address_info = self::get_google_experiment_data_by_user_id($user_id)[0]) { // user nay da tung login o ip khac, can cap nhat thong tin cho ip hien tai
self::update_info_for_ip_address($old_ip_address_info->id, $old_ip_address_info->var_key, $old_ip_address_info->user_id, $old_ip_address_info->type);
} else { // user nay chua login vao ip khac, chi can cap nhat userid
if ($type = self::get_type_of_user($user_id)) { // khach hang cu da co order
$var_key = self::getTypeVarKey()[$type];
self::update_info_for_ip_address($ip_address, $var_key, $user_id, $type);
} else { // khach hang moi, chi can cap nhat user id
self::update_user_id_for_ip_address($ip_address, $user_id);
}
}
} else { // Da cap nhat vao db. Ko can sua doi
}
} else { // ko lam gi
}
} else { // Chua co ipaddress trong db, vao site lan dau hoac la khach hang cu
if ($user_id > 0) { // Khach hang cu
if ($type = self::get_type_of_user($user_id)) {
$var_key = self::getTypeVarKey()[$type];
self::add_new_ip_address($ip_address, $var_key, $type, $user_id);
} else { // khach hang chua co order, tao type bat ky
$var_key = self::get_variation_by_google_experiment();
$type = self::getVarKeyType() [$var_key];
self::add_new_ip_address($ip_address, $var_key, $type, $user_id);
}
} else { // vao site lan dau
$var_key = self::get_variation_by_google_experiment();
$type = self::getVarKeyType() [$var_key];
self::add_new_ip_address($ip_address, $var_key, $type);
}
}
}
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 add_new_ip_address($ip_address = '', $var_key = 0, $type = '', $user_id = 0) {
if ($ip_address != '' && $var_key != 0 && $type != '') {
global $wpdb;
$query = "INSERT INTO " . $wpdb->prefix . self::getTableName() . " (`IpAddress`, `var_key`, `user_id`,`type`) VALUES ('$ip_address', '$var_key', '$user_id' , '$type');";
return $wpdb->query($query);
}
return false;
}
public static function update_user_id_for_ip_address($ip_address = '', $user_id = '') {
if ($ip_address != '' && $user_id != '') {
global $wpdb;
$query = "UPDATE `" . $wpdb->prefix . self::getTableName() . " SET user_id = '$user_id' WHERE IpAddress = '" . $ip_address . "'";
return $wpdb->query($query);
}
return false;
}
public static function update_info_for_ip_address($ip_address, $var_key = '', $user_id = '', $type = '') {
if ($ip_address != '' && $user_id != '') {
global $wpdb;
$query = "UPDATE " . $wpdb->prefix . self::getTableName() . " SET user_id = '$user_id' , var_key = '$var_key' , type = '$type' WHERE IpAddress = '$ip_address'";
return $wpdb->query($query);
}
return false;
}
public static function get_google_experiment_data_by_ipAddress($ip_address = false) {
if ($ip_address) {
global $wpdb;
$query = "SELECT * FROM " . $wpdb->prefix . self::getTableName() . " WHERE IpAddress = '" . $ip_address . "'";
$results = $wpdb->get_results($query);
if (empty($results)) { // The customer doesn't have any order
return false;
} else { // The customer has order.
return $results[0];
}
}
return false;
}
public static function get_google_experiment_data_by_user_id($user_id = false) {
if (!$user_id && $user_id != '') {
global $wpdb;
$query = "SELECT * FROM " . $wpdb->prefix . self::getTableName() . " WHERE user_id = '" . $user_id . "'";
$results = $wpdb->get_results($query);
if (empty($results)) { // The customer doesn't have any order
return false;
} else { // The customer has order.
return $results;
}
}
}
public static function get_type_of_user($user_id) {
global $wpdb;
$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
return false;
} else { // The customer has order.
return self::get_type_of_order($results[0]->post_id);
}
}
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 self::getSkuType()[$sku];
} else {
$product = new WC_Product($product_id);
$category = $product->get_categories();
return self::getCategoryType()[$category];
}
}
public static function get_products_for_slider($isSubscription) {
$type = self::get_type_for_slider();
if ($isSubscription) {
$sku = self::getTypeSku()[$type];
self::get_subscription_product_for_slider($sku);
} else {
$category = self::getTypeCategory()[$type];
self::get_single_products_for_slider($category);
}
}
public static function get_type_for_slider() {
$ip_address = self::getIpAddress();
$current_ip_address_info = self::get_google_experiment_data_by_ipAddress($ip_address);
if ($current_ip_address_info) {
return $current_ip_address_info->type;
}
return 'D';
}
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_sku_for_get_started_page() {
$type = self::get_type_for_slider();
return self::getTypeSku()[$type];
}
public static function get_var_key_for_get_started_page() {
$ip_address = self::getIpAddress();
$current_ip_address_info = self::get_google_experiment_data_by_ipAddress($ip_address);
return $current_ip_address_info->var_key;
}
}
function test_google_experiment() {
if (isset($_REQUEST['testgoogleexperiment'])) {
// Fgc_Google_Experiments::get_type_for_slider();
echo '<pre>';
ob_start();
system('getmac');
$mycom = ob_get_contents();
ob_clean();
var_dump($mycom);
ob_start();
system('ipconfig-a');
$mycom = ob_get_contents();
ob_clean();
var_dump($mycom);
$findme = "Physical";
$pmac = strpos($mycom, $findme);
$mac = substr($mycom, ($pmac + 36), 17);
var_dump($mycom);
exit();
}
}
add_action('init', 'test_google_experiment');
add_action('init', 'Fgc_Google_Experiments::init');
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);
}