-
Notifications
You must be signed in to change notification settings - Fork 21
/
universal-analytics.php
304 lines (262 loc) · 8.75 KB
/
universal-analytics.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
<?php
###############################################################################
# Universal Analytics for PHP
# Copyright (c) 2013, Analytics Pros
#
# This project is free software, distributed under the BSD license.
# Analytics Pros offers consulting and integration services if your firm needs
# assistance in strategy, implementation, or auditing existing work.
###############################################################################
defined('ANALYTICS_HASH_IDS') || define('ANALYTICS_HASH_IDS', false);
class UniversalBeacon {
/* Implements CURL POST to Google Analytics */
private $endpoint = 'https://www.google-analytics.com/collect';
private $data = null;
private $user_agent = null;
public function __construct($data, $user_agent = null){
$this->data = $data;
$this->user_agent = $user_agent;
}
public function handle($data_update = null, $debug = false){
$data = array_merge($this->data, (array)$data_update);
return self::curl($this->endpoint, $data, $this->user_agent, $debug);
}
// Issue an HTTP request via CURL
public static function & curl($url, $data, $ua = null, $debug = true){
$h = curl_init($url);
$payload = self::combine($data);
curl_setopt($h, CURLOPT_AUTOREFERER, true);
curl_setopt($h, CURLOPT_NOPROGRESS, true);
if($debug){
print_r($data);
print_r($payload);
print "\n"; # readability
}
if(is_string($ua)){
curl_setopt($h, CURLOPT_USERAGENT, $ua);
}
curl_setopt($h, CURLOPT_VERBOSE, $debug);
curl_setopt($h, CURLOPT_POST, count($data));
curl_setopt($h, CURLOPT_POSTFIELDS, $payload);
curl_setopt($h, CURLOPT_RETURNTRANSFER, true);
curl_setopt($h, CURLOPT_HEADER, 0);
return $h;
// $v = curl_exec($h);
// curl_close($h);
// return $v;
}
// A simpler parameter joining method
public static function combine($params, $pair = '=', $sep = '&'){
$c = count($params);
return $c ? implode($sep, array_map(
'sprintf', // NOTE: even built-in functions require names given as strings when mapping
array_fill(0, $c, '%s%s%s'), // format string
array_keys($params), // keys
array_fill(0, $c, $pair), // pairing (=)
array_map('urlencode', array_values($params)) // values
)) : '';
}
}
class UniversalBeaconPool {
const MAXIMUM_REQUEST_QUEUE = 10;
private $user_agent = 'user_agent';
private $debug = false;
private $handler = null;
private $request_queue = array();
public function __construct($user_agent = null, $debug = false){
$this->handler = curl_multi_init();
// This option isn't supported before PHP 5.5
// curl_multi_setopt($this->handler, CURLMOPT_PIPELINING, 1);
if(is_string($user_agent)){
$this->user_agent = $user_agent;
}
if($debug){
$this->debug = true;
}
}
public function addRequest($data, $user_agent = null){
$user_agent = (is_string($user_agent) ? $user_agent : $this->user_agent);
$request = new UniversalBeacon($data, $user_agent);
$handle = $request->handle(null, $this->debug);
array_push($this->request_queue, $handle);
curl_multi_add_handle($this->handler, $handle);
if(count($this->request_queue) >= self::MAXIMUM_REQUEST_QUEUE){
self::process($this->handler, $this->request_queue);
}
}
public static function process(& $handler, & $request_queue){
do {
curl_multi_exec($handler, $running);
} while($running > 0);
while($handle = array_pop($request_queue)){
curl_multi_remove_handle($handler, $handle);
}
}
public function __destruct(){
self::process($this->handler, $this->request_queue);
curl_multi_close($this->handler);
}
}
class Tracker {
const VERSION = 1;
const USER_AGENT = 'Analytics Pros - Universal Analytics (PHP)';
private $account = null;
private $state = null;
private $user_agent = null;
private $pool = null;
public $debug = false;
public function __construct($account, $client_id = null, $user_id = null, $debug = false){
$this->account = $account;
$this->debug = (bool) $debug;
$this->pool = new UniversalBeaconPool(self::USER_AGENT, $this->debug);
if(!is_null($client_id) && constant('ANALYTICS_HASH_IDS'))
$client_id = self::hash_uuid($client_id);
elseif(is_null($client_id))
$client_id = self::generateUUID4();
$this->state = array(
'v' => self::VERSION,
'tid' => $account,
'cid' => $client_id
);
if(!is_null($user_id)){
if(constant('ANALYTICS_HASH_IDS'))
$user_id = self::hash_uuid($user_id);
$this->state[ 'uid' ] = $user_id;
}
}
/* Return an MD5 checksum spaced in UUD4-format */
public static function hash_uuid($value){
$checksum = md5($value);
return sprintf('%8s-%4s-%4s-%4s-%12s',
substr($checksum, 0, 8),
substr($checksum, 8, 4),
substr($checksum, 12, 4),
substr($checksum, 16, 4),
substr($checksum, 20, 12)
);
}
public function setUserAgent($ua){
if(is_string($ua)){
$this->user_agent = $ua;
}
}
public function send($hit_type, $attribs = null, $ua = null){
$agent = (is_string($ua)
? $ua
: (is_string($this->user_agent)
? $this->user_agent
: self::USER_AGENT
)
);
return $this->pool->addRequest($this->hitdata($hit_type, $attribs), $agent);
}
public function set($name, $value){
$this->state[ $name ] = $value;
}
public function get($name){
if(array_key_exists($name, $this->state)){
return $this->state[ $name ];
} else {
return null;
}
}
public function hitdata($type, $attribs = null){
return self::params($type, array_merge($this->state, (array)$attribs));
}
public static function & params($type, $data){
$result_data = array();
$result_keys_in = array_keys($data);
$result_keys = str_replace(array_keys(self::$name_map), array_values(self::$name_map), $result_keys_in);
$result_keys = preg_replace(array_keys(self::$name_map_re), array_values(self::$name_map_re), $result_keys);
for($i = 0; $i < count($result_keys_in); $i++){
$result_data[ $result_keys[ $i ] ] = $data[ $result_keys_in[ $i ] ];
}
$result_data[ 't' ] = $type;
return $result_data;
}
public static $name_map = array(
'clientId' => 'cid',
'userId' => 'uid',
'eventCategory' => 'ec',
'eventAction' => 'ea',
'eventLabel' => 'el',
'eventValue' => 'ev',
'nonInteraction' => 'ni',
'nonInteractive' => 'ni',
'documentPath' => 'dp',
'documentTitle' => 'dt',
'title' => 'dt',
'path' => 'dp',
'page' => 'dp',
'location' => 'dl',
'documentLocation' => 'dl',
'hostname' => 'dh',
'documentHostname' => 'dh',
'sessionControl' => 'sc',
'referrer' => 'dr',
'documentReferrer' => 'dr',
'queueTime' => 'qt',
'campaignName' => 'cn',
'campaignSource' => 'cs',
'campaignMedium' => 'cm',
'campaignKeyword' => 'ck',
'campaignContent' => 'cc',
'campaignId' => 'ci',
'screenResolution' => 'sr',
'viewportSize' => 'vp',
'documentEncoding' => 'de',
'screenColors' => 'sd',
'userLanguage' => 'ul',
'appName' => 'an',
'contentDescription' => 'cd',
'appVersion' => 'av',
'transactionAffiliation' => 'ta',
'transactionId' => 'ti',
'transactionRevenue' => 'tr',
'transactionShipping' => 'ts',
'transactionTax' => 'tt',
'transactionCurrency' => 'cu',
'itemName' => 'in',
'itemPrice' => 'ip',
'itemQuantity' => 'iq',
'itemCode' => 'ic',
'itemVariation' => 'iv',
'itemCategory' => 'iv',
'socialAction' => 'sa',
'socialNetwork' => 'sn',
'socialTarget' => 'st',
'exceptionDescription' => 'exd',
'exceptionFatal' => 'exf',
'timingCategory' => 'utc',
'timingVariable' => 'utv',
'timingTime' => 'utt',
'timingLabel' => 'utl',
'timingDNS' => 'dns',
'timingPageLoad' => 'pdt',
'timingRedirect' => 'rrt',
'timingTCPConnect' => 'tcp',
'timingServerResponse' => 'srt'
);
public static $name_map_re = array(
'@^dimension([0-9]+)$@' => 'cd$1',
'@^metric([0-9]+)$@' => 'cm$1'
);
public static function generateUUID4(){
return sprintf( '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
// 32 bits for "time_low"
mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ),
// 16 bits for "time_mid"
mt_rand( 0, 0xffff ),
// 16 bits for "time_hi_and_version",
// four most significant bits holds version number 4
mt_rand( 0, 0x0fff ) | 0x4000,
// 16 bits, 8 bits for "clk_seq_hi_res",
// 8 bits for "clk_seq_low",
// two most significant bits holds zero and one for variant DCE1.1
mt_rand( 0, 0x3fff ) | 0x8000,
// 48 bits for "node"
mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff )
);
}
}
?>