forked from dancameron/server-side-google-analytics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ss-ga.class.php
409 lines (335 loc) · 9.31 KB
/
ss-ga.class.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
<?php
/**
* Simple Server Side Analytics
*
*/
class ssga {
const GA_URL = 'https://stats.g.doubleclick.net/__utm.gif'; //@nczz update v5.6.4dc
private $data = array(
'utmac' => null,
'utmcc' => null,
'utmcn' => null,
'utmcr' => null,
'utmcs' => null,
'utmdt' => '-',
'utmfl' => '-',
'utme' => null,
'utmni' => null,
'utmhn' => null,
'utmipc' => null,
'utmipn' => null,
'utmipr' => null,
'utmiqt' => null,
'utmiva' => null,
'utmje' => 0,
'utmn' => null,
'utmp' => null,
'utmr' => null,
'utmsc' => '-',
'utmvp' => '-',
'utmsr' => '-',
'utmt' => null,
'utmtci' => null,
'utmtco' => null,
'utmtid' => null,
'utmtrg' => null,
'utmtsp' => null,
'utmtst' => null,
'utmtto' => null,
'utmttx' => null,
'utmul' => '-',
'utmhid' => null,
'utmht' => null,
'utmwv' => '5.6.4dc' );
private $tracking;
public function __construct( $UA = null, $domain = null ) {
$this->data['utmac'] = $UA;
$this->data['utmhn'] = isset( $domain ) ? $domain : $_SERVER['SERVER_NAME'];
$this->data['utmp'] = $_SERVER['PHP_SELF'];
$this->data['utmn'] = rand( 1000000000, 9999999999 );
$this->data['utmr'] = isset( $_SERVER['HTTP_REFERER'] ) ? $_SERVER['HTTP_REFERER'] : '';
$this->data['utmcc'] = $this->create_cookie();
$this->data['utmhid'] = rand( 1000000000, 9999999999 );
$this->data['utmht'] = time() * 1000;
}
/**
* Create the GA callback url, aka the gif
*
* @return string
*/
public function create_gif() {
$data = array();
foreach ( $this->data as $key => $item ) {
if ( $item !== null ) {
$data[$key] = $item;
}
}
return $this->tracking = self::GA_URL . '?' . http_build_query( $data );
}
/**
* Send tracking code/gif to GB
*
* @return
*/
public function send() {
if ( !isset( $this->tracking ) )
$this->create_gif();
return $this->remote_call();
}
/**
* Use WP's HTTP class or CURL or fopen
* @return array|null
*/
private function remote_call() {
if ( function_exists( 'wp_remote_head' ) ) { // Check if this is being used with WordPress, if so use it's excellent HTTP class
$response = wp_remote_head( $this->tracking );
return $response;
} elseif ( function_exists( 'curl_init' ) ) {
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $this->tracking );
curl_setopt( $ch, CURLOPT_HEADER, false );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false); //@nczz Fixed HTTPS GET method
curl_setopt( $ch, CURLOPT_TIMEOUT, 10 );
curl_exec( $ch );
curl_close( $ch );
} else {
$handle = fopen( $this->tracking, "r" );
fclose( $handle );
}
return;
}
/**
* Reset Defaults
* @return null
*/
public function reset() {
$data = array(
'utmac' => null,
'utmcc' => $this->create_cookie(),
'utmcn' => null,
'utmcr' => null,
'utmcs' => null,
'utmdt' => '-',
'utmfl' => '-',
'utme' => null,
'utmni' => null,
'utmipc' => null,
'utmipn' => null,
'utmipr' => null,
'utmiqt' => null,
'utmiva' => null,
'utmje' => '0',
'utmn' => rand( 1000000000, 9999999999 ),
'utmp' => $_SERVER['PHP_SELF'],
'utmr' => isset( $_SERVER['HTTP_REFERER'] ) ? $_SERVER['HTTP_REFERER'] : '',
'utmsc' => '-',
'utmsr' => '-',
'utmt' => null,
'utme' => null,
'utmtci' => null,
'utmtco' => null,
'utmtid' => null,
'utmtrg' => null,
'utmtsp' => null,
'utmtst' => null,
'utmtto' => null,
'utmttx' => null,
'utmul' => 'php',
'utmht' => time() * 1000,
'utmwv' => '5.6.4dc' );
$this->tracking = null;
return $this->data = $data;
}
/**
* Create unique cookie
* @return string
*/
private function create_cookie() {
$rand_id = rand( 10000000, 99999999 );
$random = rand( 1000000000, 2147483647 );
$var = '-';
$time = time();
$cookie = '';
$cookie .= '__utma=' . $rand_id . '.' . $random . '.' . $time . '.' . $time . '.' . $time . '.2;+';
$cookie .= '__utmb=' . $rand_id . ';+';
$cookie .= '__utmc=' . $rand_id . ';+';
$cookie .= '__utmz=' . $rand_id . '.' . $time . '.2.2.utmccn=(direct)|utmcsr=(direct)|utmcmd=(none);+';
$cookie .= '__utmv=' . $rand_id . '.' . $var . ';';
return $cookie;
}
////////////
// Params //
////////////
/////////////
// Product //
/////////////
public function set_product_code( $var = null ) {
return $this->data['utmipc'] = $var;
}
public function set_product_name( $var = null ) {
return $this->data['utmipn'] = $var;
}
public function set_unit_price( $var = null ) {
return $this->data['utmipr'] = $var;
}
public function set_qty( $var = null ) {
return $this->data['utmiqt'] = $var;
}
public function set_variation( $var = null ) {
return $this->data['utmiva'] = $var;
}
//////////
// Misc //
//////////
public function set_java( $var = null ) {
return $this->data['utmje'] = $var;
}
public function set_encode_type( $var = null ) {
return $this->data['utmcs'] = $var;
}
public function set_flash_version( $var = null ) {
return $this->data['utmfl'] = $var;
}
public function set_host( $var = null ) {
return $this->data['utmhn'] = $var;
}
public function set_screen_depth( $var = null ) {
return $this->data['utmsc'] = $var;
}
public function set_screen_resolution( $var = null ) {
return $this->data['utmsr'] = $var;
}
public function set_lang( $var = null ) {
return $this->data['utmul'] = $var;
}
public function set_ga_version( $var = null ) {
return $this->data['utmwv'] = isset( $var ) ? $var : $this->data['utmwv'];
}
//////////
// Page //
//////////
public function set_page( $var = null ) {
return $this->data['utmp'] = $var;
}
public function set_page_title( $var = null ) {
return $this->data['utmdt'] = $var;
}
public function set_campaign( $var=null ) {
return $this->data['utmcn'] = $var;
}
public function clone_campaign( $var=null ) {
return $this->data['utmcr'] = $var;
}
public function set_referal( $var = null ) {
return $this->data['utmr'] = $var;
}
////////////
// Events //
////////////
public function set_event( $category, $action, $label = '', $value = '', $opt_noninteraction = false) {
$event_category = (string) $category;
$event_action = (string) $action;
$event_string = '5(' . $event_category . '*' . $event_action;
if (!empty($label)) {
$event_string .= '*' . ((string) $label) . ')';
} else {
$event_string .= ')';
}
if (!empty($value)) {
$event_string .= '(' . ((int) intval($value)) . ')';
}
if ($opt_noninteraction) {
$this->data['utmni'] = '1';
}
$this->data['utmt'] = 'event';
return $this->data['utme'] = $event_string;
}
///////////
// Order //
///////////
public function set_order_id( $var = null ) {
return $this->data['utmtid'] = $var;
}
public function set_billing_city( $var = null ) {
return $this->data['utmtci'] = $var;
}
public function set_billing_country( $var = null ) {
return $this->data['utmtco'] = $var;
}
public function set_billing_region( $var = null ) {
return $this->data['utmtrg'] = $var;
}
public function set_shipping_cost( $var = null ) {
return $this->data['utmtsp'] = $var;
}
public function set_affiliate( $var = null ) {
return $this->data['utmtst'] = $var;
}
public function set_total( $var = null ) {
return $this->data['utmtto'] = $var;
}
public function set_taxes( $var = null ) {
return $this->data['utmttx'] = $var;
}
////////////////////////
// Ecommerce Tracking //
////////////////////////
private static $requests_for_this_session = 0;
/**
* Create and send a transaction object
*
* Parameter order from https://developers.google.com/analytics/devguides/collection/gajs/gaTrackingEcommerce
*/
public function send_transaction($transaction_id, $affiliation, $total, $tax, $shipping, $city, $region, $country) {
$this->data['utmvw'] = '5.6.4dc';
$this->data['utms'] = ++self::$requests_for_this_session;
$this->data['utmt'] = 'tran';
$this->data['utmtid'] = $transaction_id;
$this->data['utmtst'] = $affiliation;
$this->data['utmtto'] = $total;
$this->data['utmttx'] = $tax;
$this->data['utmtsp'] = $shipping;
$this->data['utmtci'] = $city;
$this->data['utmtrg'] = $region;
$this->data['utmtco'] = $country;
$this->data['utmcs'] = 'UTF-8';
$this->send();
$this->reset();
return $this;
}
/**
* Add item to the created $transaction_id
*
* Parameter order from https://developers.google.com/analytics/devguides/collection/gajs/gaTrackingEcommerce
*/
public function send_item($transaction_id, $sku, $product_name, $variation, $unit_price, $quantity) {
$this->data['utmvw'] = '5.6.4dc';
$this->data['utms'] = ++self::$requests_for_this_session;
$this->data['utmt'] = 'item';
$this->data['utmtid'] = $transaction_id;
$this->data['utmipc'] = $sku;
$this->data['utmipn'] = $product_name;
$this->data['utmiva'] = $variation;
$this->data['utmipr'] = $unit_price;
$this->data['utmiqt'] = $quantity;
$this->data['utmcs'] = 'UTF-8';
$this->send();
$this->reset();
return $this;
}
}
/**
* Instantiate new class and push data
* @param string $UA The UA string of the GA account to use
* @param string $domain domain
* @param string $page the page to set the pageview
* @return null
*/
function ssga_track( $UA = null, $domain = null, $page = null ) {
$ssga = new ssga( $UA, $domain );
$ssga->set_page( $page );
$ssga->send();
$ssga->reset();
return $ssga;
}