-
Notifications
You must be signed in to change notification settings - Fork 0
/
googleanalytics.php
107 lines (79 loc) · 2.16 KB
/
googleanalytics.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
<?php
/**
Google Analytics
* @author: Willem Labu
* Flint n Tinder
*
* Google's Mobile Analytics was deprecated.
* Rewriting it as per the guidelines here: http://goo.gl/ii3Bvl
*/
// Your UA code
$GA_ACCOUNT = "UA-XX-X";
/**
* A unique identifier for the current user
* http://goo.gl/EeHnoo
*/
$UID = '';
/**
* A switch
*
true
*
* BLOCKING
* Best to include this at the _bottom_ of your page.
*
* Uses `file_get_contents`
* Make sure your server has `allow_url_fopen` turned on.
*
false
*
* NON-BLOCKING
* Best to include this at the _top_ of your page.
*
* Uses `exec` & `cURL`
* Make sure your server has `php_curl` installed and on.
*/
$BLOCKING_REQUEST = false;
/**************************************
* NO NEED TO EDIT ANYTHING BELOW *
**************************************/
// The current page URL
$PAGE = $_SERVER["REQUEST_URI"];
// GA's URL to POST to
$GA_URL = "http://www.google-analytics.com/collect";
// MXit specific overwrites
$UID = isset($_SERVER["HTTP_X_MXIT_USERID_R"]) ? md5($_SERVER["HTTP_X_MXIT_USERID_R"]) : $UID;
$UA = isset($_SERVER["HTTP_X_DEVICE_USER_AGENT"]) ? $_SERVER["HTTP_X_DEVICE_USER_AGENT"] : $_SERVER['HTTP_USER_AGENT'];
$MXIT_PIXELS = isset($_SERVER["HTTP_UA_PIXELS"]) ? $_SERVER["HTTP_UA_PIXELS"] : '';
// Here we go!
if ($BLOCKING_REQUEST) {
$data = array(
'payload_data' => '',
'v' => '1',
't' => 'pageview',
'dp' => $PAGE,
'tid' => $GA_ACCOUNT,
'cid' => $UID,
'uid' => $UID,
'ua' => $UA,
'sr' => $MXIT_PIXELS
);
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data),
),
);
$context = stream_context_create($options);
// Hit!
echo file_get_contents($GA_URL, false, $context);
} else {
$url = "$GA_URL?payload_data&v=1&t=pageview&dp=" .
urlencode($PAGE) . "&tid=$GA_ACCOUNT&cid=$UID&uid=$UID&ua=" .
urlencode($UA) . "&sr=$MXIT_PIXELS";
$cmd = "curl '" . $url . "' > /dev/null 2>&1 &";
// Hit!
exec($cmd, $output, $exit);
}
?>