1
1
<?php
2
2
3
- define ('SENSORS_ANALYTICS_SDK_VERSION ' , '1.7.2 ' );
3
+ define ('SENSORS_ANALYTICS_SDK_VERSION ' , '1.10.0 ' );
4
4
5
5
class SensorsAnalyticsException extends \Exception {
6
6
}
@@ -21,16 +21,17 @@ class SensorsAnalytics {
21
21
22
22
private $ _consumer ;
23
23
private $ _super_properties ;
24
-
24
+ private $ _is_win ;
25
25
/**
26
26
* 初始化一个 SensorsAnalytics 的实例用于数据发送。
27
27
*
28
28
* @param AbstractConsumer $consumer
29
29
*/
30
30
public function __construct ($ consumer ) {
31
+ $ this ->_is_win = false ;
31
32
// 不支持 Windows,因为 Windows 版本的 PHP 都不支持 long
32
33
if (strtoupper (substr (PHP_OS , 0 , 3 )) == "WIN " ) {
33
- throw new SensorsAnalyticsException ( " Sensors Analytics PHP SDK dons't not support Windows " ) ;
34
+ $ this -> _is_win = true ;
34
35
}
35
36
$ this ->_consumer = $ consumer ;
36
37
$ this ->clear_super_properties ();
@@ -47,14 +48,33 @@ private function _normalize_data($data) {
47
48
$ data ['distinct_id ' ] = strval ($ data ['distinct_id ' ]);
48
49
49
50
// 检查 time
50
- $ ts = (int )($ data ['time ' ]);
51
- $ ts_num = strlen ($ ts );
52
- if ($ ts_num < 10 || $ ts_num > 13 ) {
53
- throw new SensorsAnalyticsIllegalDataException ("property [time] must be a timestamp in microseconds " );
54
- }
51
+ if ($ this ->_is_win ) { // windows use string(windows 32bit do not support int64)
52
+ if (!is_string ($ data ['time ' ])) {
53
+ throw new SensorsAnalyticsIllegalDataException ("property [time] type must be string " );
54
+ }
55
+ $ ts = $ data ['time ' ];
56
+ $ ts_num = strlen ($ ts );
57
+ if (strlen ($ ts_num ) == 15 ) {
58
+ $ ts = substr ($ ts , 0 , 13 );
59
+ }
60
+
61
+ if ($ ts_num < 10 || $ ts_num > 13 ) {
62
+ throw new SensorsAnalyticsIllegalDataException ("property [time] must be a timestamp in microseconds " );
63
+ }
55
64
56
- if ($ ts_num == 10 ) {
57
- $ ts *= 1000 ;
65
+ if ($ ts_num == 10 ) {
66
+ $ ts .= "000 " ;
67
+ }
68
+ } else { // linux use int
69
+ $ ts = (int )($ data ['time ' ]);
70
+ $ ts_num = strlen ($ ts );
71
+ if ($ ts_num < 10 || $ ts_num > 13 ) {
72
+ throw new SensorsAnalyticsIllegalDataException ("property [time] must be a timestamp in microseconds " );
73
+ }
74
+
75
+ if ($ ts_num == 10 ) {
76
+ $ ts *= 1000 ;
77
+ }
58
78
}
59
79
$ data ['time ' ] = $ ts ;
60
80
@@ -119,15 +139,19 @@ private function _normalize_data($data) {
119
139
* 如果用户传入了 $time 字段,则不使用当前时间。
120
140
*
121
141
* @param array $properties
122
- * @return int
142
+ * @return int/string
123
143
*/
124
144
private function _extract_user_time (&$ properties = array ()) {
125
145
if (array_key_exists ('$time ' , $ properties )) {
126
146
$ time = $ properties ['$time ' ];
127
147
unset($ properties ['$time ' ]);
128
148
return $ time ;
129
149
}
130
- return (int )(microtime (true ) * 1000 );
150
+ if ($ this ->_is_win ) { // windows return string
151
+ return substr ((microtime (true ) * 1000 ), 0 , 13 );
152
+ } else {
153
+ return (int )(microtime (true ) * 1000 );
154
+ }
131
155
}
132
156
133
157
/**
0 commit comments