-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSensorsABTesting.php
executable file
·481 lines (427 loc) · 18.8 KB
/
SensorsABTesting.php
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
<?php
define('SENSORS_ABTESTING_SDK_VERSION', '0.0.8');
require_once(__DIR__ . "/lib/LRUCache.php");
require_once(__DIR__."/lib/cache.php");
class SensorsABTesting {
/*
* SensorsAnalytics 埋点 SDK 实例
*/
public $_sa;
/*
* 项目数据接收地址
*/
public $_api_url;
/*
* 是否开启自动上报事件
*/
private $_enable_event_cache = true;
/*
* 请求超时时间
*/
private $_request_timeout = 3000;
/*
* 实验结果缓存
*/
private $_experiments_cache;
/*
* $ABTestTrigger事件缓存
*/
private $_events_cache;
/*
* $lib_plugin_version 缓存
*/
private $_lib_plugin_version_cache;
/*
* log path
*/
private $_log_path;
/**
* @param string $api_url 项目数据接收地址
* @param object $sa SensorsAnalytics 埋点 SDK 实例
* @param object $init_params
*/
public function __construct($api_url, $sa, $cache_config = null) {
if (is_string($api_url) && !empty($api_url)) {
$this->_api_url = $api_url;
} else {
throw new Exception("api_url invalid, please set string");
}
if ($sa instanceof SensorsAnalytics) {
$this->_sa = $sa;
} else {
throw new Exception("\$sa is not an instance of SensorsAnalytics");
}
// connect cache service
if (isset($cache_config)) {
if (!is_array($cache_config)) {
throw new Exception("cache_config must be an array");
}
if (!isset($cache_config['enable_event_cache'])) {
$cache_config['enable_event_cache'] = true;
}
if (isset($cache_config['type'])) {
if (!($cache_config['type'] == 'redis' || $cache_config['type'] == 'memcached')) {
throw new Exception("Invalid cache server type, only redis/memcached supported");
}
}
if (isset($cache_config['host']) && isset($cache_config['port'])) {
$experiment_cache_size = 4096;
if (isset($cache_config['experiment_cache_size']) && intval($cache_config['experiment_cache_size']) > 0) {
$experiment_cache_size = intval($cache_config['experiment_cache_size']);
}
$experiment_cache_time = 86400;
if (isset($cache_config['experiment_cache_time'])
&& intval($cache_config['experiment_cache_time']) > 0
&& intval($cache_config['experiment_cache_time']) < $experiment_cache_time) {
$experiment_cache_time = intval($cache_config['experiment_cache_time']);
}
try {
$this->_experiments_cache = new LRUCache(
$cache_config['type'],
$cache_config['host'],
$cache_config['port'],
isset($cache_config['auth']) ? $cache_config['auth'] : null ,
"experiment",
$experiment_cache_size,
$experiment_cache_time
);
if (!isset($this->_experiments_cache)) {
$this->print_log("Experiment cache server can not connect. fast_fetch will fallback to async_fetch");
}
} catch (Exception $e) {
$this->print_log("Experiment cache server can not connect. fast_fetch will fallback to async_fetch");
}
}
if ($cache_config['enable_event_cache']) {
$event_cache_size = 4096;
if (isset($cache_config['event_cache_size']) && intval($cache_config['event_cache_size']) > 0) {
$event_cache_size = intval($cache_config['event_cache_size']);
}
$event_cache_time = 86400;
if (isset($cache_config['event_cache_time'])
&& intval($cache_config['event_cache_time']) > 0
&& intval($cache_config['event_cache_time']) < $event_cache_time) {
$event_cache_time = intval($cache_config['event_cache_time']);
}
try {
$this->_events_cache = new LRUCache(
$cache_config['type'],
$cache_config['host'],
$cache_config['port'],
isset($cache_config['auth']) ? $cache_config['auth'] : null ,
"event",
$event_cache_size,
$event_cache_time
);
if (!isset($this->_events_cache)) {
$this->print_log("Event cache server can not connect. Event cache disabled");
$this->_enable_event_cache = false;
}
} catch (\Throwable $th) {
$this->print_log("Event cache server can not connect. Event cache disabled");
$this->_enable_event_cache = false;
}
} else {
$this->_enable_event_cache = false;
}
try {
$this->_lib_plugin_version_cache = new Cache(
$cache_config['type'],
$cache_config['host'],
$cache_config['port'],
isset($cache_config['auth']) ? $cache_config['auth'] : null
);
} catch (\Throwable $th) {
}
if (isset($cache_config['log_path'])) {
$this->_log_path = $cache_config['log_path'];
} else {
$this->_log_path = __DIR__ . '/abtesting.log';
}
}
return $this;
}
/**
* distinct_id: 用户标识(必填)
* is_login_id: 是否是登录 ID(必填)
* request_param: 请求参数(必填)
* {
* default_value: 默认值(必填)
* param_name: 试验变量名(必填)
* enable_auto_track_event: 是否自动触发 A/B Testing 埋点事件
* $properties: 请求试验的分流筛选属性
* timeout_milliseconds: 网络请求超时时间,单位 ms,默认值3000
* }
*/
public function async_fetch_abtest($distinct_id, $is_login_id, $request_params) {
return $this->fetch_ab_test($distinct_id, $is_login_id, $request_params, false);
}
/**
* distinct_id: 用户标识(必填)
* is_login_id: 是否是登录 ID(必填)
* request_param: 请求参数(必填)
* {
* default_value: 默认值(必填)
* param_name: 试验变量名(必填)
* enable_auto_track_event: 是否自动触发 A/B Testing 埋点事件
* timeout_milliseconds: 网络请求超时时间,单位 ms,默认值3000
* }
*/
public function fast_fetch_abtest($distinct_id, $is_login_id, $request_params) {
return $this->fetch_ab_test($distinct_id, $is_login_id, $request_params, true);
}
public function fetch_ab_test($distinct_id, $is_login_id, $request_params, $enable_cache) {
if (!isset($request_params['default_value'])) {
throw new Exception("default_value must be set in \$request_params");
}
if (!isset($request_params['value_type'])) {
throw new Exception("value_type must be set in \$request_params");
}
if (!is_string($request_params['value_type'])) {
throw new Exception("value_type must be string");
}
if (!$this->verify_type($request_params['default_value'], $request_params['value_type'])) {
throw new Exception("default_value should match value_type");
}
if (!isset($request_params['param_name'])) {
throw new Exception("param_name must be set in \$request_params");
}
if (!is_string($request_params['param_name'])) {
throw new Exception("param_name must be string");
}
if (!is_string($distinct_id)) {
throw new Exception("\$distinct_id must be string");
}
if (!is_bool($is_login_id)) {
throw new Exception("\$is_login_id must be boolean");
}
$enable_auto_track_event = true;
if (isset($request_params['enable_auto_track_event']) && is_bool($request_params['enable_auto_track_event'])) {
$enable_auto_track_event = $request_params['enable_auto_track_event'];
}
if ($enable_cache && isset($this->_experiments_cache)) {
try {
$experiments = $this->_experiments_cache->get($distinct_id.$is_login_id);
if (empty($experiments)) {
$experiments = $this->_fetch_experiment_result($distinct_id, $is_login_id, $request_params);
$this->_experiments_cache->put($distinct_id.$is_login_id, $experiments);
}
} catch(Exception $e) {
$this->print_log("Cache server connection lost. Fetch experiment result from server directly");
$experiments = $this->_fetch_experiment_result($distinct_id, $is_login_id, $request_params);
} catch (Error $e) {
$this->print_log("Error occurred: " . $e->getMessage());
$experiments = $this->_fetch_experiment_result($distinct_id, $is_login_id, $request_params);
}
} else {
$experiments = $this->_fetch_experiment_result($distinct_id, $is_login_id, $request_params);
}
$experiment_result = $this->_convert_experiments($experiments, $distinct_id, $is_login_id, $request_params['param_name'], $request_params['value_type'], $request_params['default_value']);
if ($enable_auto_track_event) {
$this->track_abtesttrigger($experiment_result);
}
return $experiment_result;
}
public function track_abtesttrigger($experiment_result, $custom_properties = []) {
try {
if (isset($experiment_result['is_white_list']) && $experiment_result['is_white_list']) {
return;
}
if (isset($experiment_result['abtest_experiment_id'])) {
$distinct_id = $experiment_result['distinct_id'];
$is_login_id = $experiment_result['is_login_id'];
$event_cache = false;
if ($this->_enable_event_cache && $this->_events_cache) {
$event_cache_key = $distinct_id.$is_login_id.$experiment_result['abtest_experiment_id'];
try {
$event_cache = $this->_events_cache->get($event_cache_key);
$this->_events_cache->put($event_cache_key, 1);
} catch(Exception $e) {
$this->print_log("Cache server connection lost. Event cache disabled");
}
}
if (!$event_cache) {
$properties = array(
'$abtest_experiment_id' => $experiment_result['abtest_experiment_id'],
'$abtest_experiment_group_id' => $experiment_result['abtest_experiment_group_id'],
);
if ($this->_lib_plugin_version_cache) {
try {
$lib_cache_key = "has_lib_plugin_version";
if (!$this->_lib_plugin_version_cache->get($lib_cache_key)) {
$properties = array_merge($properties, [
'$lib_plugin_version' => ["php_abtesting:".SENSORS_ABTESTING_SDK_VERSION]
]);
$expire = (new DateTime())->getTimestamp() + 86400;
$this->_lib_plugin_version_cache->set($lib_cache_key, 1, $expire);;
}
} catch(Exception $e) { }
} else {
$properties = array_merge($properties, [
'$lib_plugin_version' => ["php_abtesting:".SENSORS_ABTESTING_SDK_VERSION]
]);
}
$properties = array_merge($properties, $custom_properties);
$this->_sa->track(
$distinct_id,
(boolean)$is_login_id,
'$ABTestTrigger',
$properties
);
}
}
} catch (Exception $e) {
$this->print_log("Track \$abtestTrigger failed: ".$e->getMessage());
}
}
public function _fetch_experiment_result($distinct_id, $is_login_id, $request_params) {
try {
$params = array(
"platform" => "php",
"abtest_lib_version" => SENSORS_ABTESTING_SDK_VERSION,
"properties" => new stdClass()
);
$params = array_merge($params, $is_login_id ?
array("login_id" => $distinct_id) :
array("anonymous_id" => $distinct_id)
);
$request_timeout = 3000;
if (isset($request_params['timeout_milliseconds']) && $request_params['timeout_milliseconds'] > 0) {
$request_timeout = $request_params['timeout_milliseconds'];
}
$ret = $this->_do_request($params, $request_timeout);
return json_decode($ret['ret_content']) -> results;
} catch (Exception $e) {
$this->print_log("Fetch experiment result failed: ".$e->getMessage());
return [];
}
}
public function _convert_experiments($experiments, $distinct_id, $is_login_id, $param_name, $param_type, $default_value) {
if (!is_array($experiments)) {
return array(
"distinct_id" => $distinct_id,
"is_login_id" => $is_login_id,
"value" => $default_value
);
}
foreach ($experiments as $key => $experiment) {
if (isset($experiment) && isset($experiment->variables)) {
foreach ($experiment->variables as $key => $variable) {
if (isset($variable) && isset($variable->name)) {
if ($variable->name == $param_name) {
$data_type = $variable->type;
if ($data_type != $param_type) {
continue;
}
$value = $variable->value;
switch ($data_type) {
case 'STRING':
$value = strval($value);
break;
case 'INTEGER':
$value = intval($value);
break;
case 'JSON':
$value = json_decode($value);
break;
case 'BOOLEAN':
$value = filter_var($value, FILTER_VALIDATE_BOOLEAN);
break;
default:
$value = null;
break;
}
if ($value !== null) {
return array(
"distinct_id" => $distinct_id,
"is_login_id" => $is_login_id,
"abtest_experiment_id" => $experiment->abtest_experiment_id,
"abtest_experiment_group_id" => $experiment->abtest_experiment_group_id,
"is_white_list" => $experiment->is_white_list,
"is_control_group" => $experiment->is_control_group,
"value" => $value
);
}
}
}
}
}
}
return array(
"distinct_id" => $distinct_id,
"is_login_id" => $is_login_id,
"value" => $default_value
);
}
/**
* 发送数据包给远程服务器。
*
* @param array $data
* @return array
* @throws SensorsAnalyticsDebugException
*/
protected function _do_request($data, $request_timeout) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $this->_api_url);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT_MS, $request_timeout);
curl_setopt($ch, CURLOPT_TIMEOUT_MS, $request_timeout);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_USERAGENT, "PHP ABTesting SDK");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
//judge https
$pos = strpos($this->_api_url, "https");
if ($pos === 0) {
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
}
$http_response_header = curl_exec($ch);
if (!$http_response_header) {
throw new SensorsAnalyticsDebugException(
"Failed to connect to SensorsAnalytics A/B Testing. [error='".curl_error($ch)."']");
}
$result = array(
"ret_content" => $http_response_header,
"ret_code" => curl_getinfo($ch, CURLINFO_HTTP_CODE)
);
curl_close($ch);
return $result;
}
private function verify_type($value, $type) {
switch ($type) {
case 'INTEGER':
if (is_numeric($value)) {
return true;
}
break;
case 'STRING':
if (is_string($value)) {
return true;
}
break;
case 'JSON':
if (is_object($value)) {
return true;
}
break;
case 'BOOLEAN':
if (is_bool($value)) {
return true;
}
break;
default:
throw new Exception("invalid value_type, value_type must be in 'INTEGER' | 'STRING' | 'JSON' | 'BOOLEAN'");
return false;
}
}
private function print_log($msg) {
$time = date(DATE_W3C);
$log_msg = $time . ": " . $msg . PHP_EOL;
if ($this->_log_path) {
error_log($log_msg, 3, $this->_log_path);
} else {
error_log($log_msg);
}
}
}