-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathga-beacon.php
183 lines (142 loc) · 4.09 KB
/
ga-beacon.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
<?php
/**
* GA Beacon
*
* Send pageview to Google Analytics via img file.
*
* @version 1.0.0
* @see https://github.com/aucor/ga-beacon-php
* @see https://github.com/igrigorik/ga-beacon
*/
class GA_Beacon {
// set explicit account if needed
private $account = '';
private $beacon_url = 'https://www.google-analytics.com/collect';
private $debug = [];
function __construct() {
if ($this->is_valid_request()) {
$this->send_pageview();
}
}
/**
* Validate request
*
* @return bool
*/
protected function is_valid_request() {
if (!isset($_SERVER['REQUEST_URI']) || empty($_SERVER['REQUEST_URI'])) {
throw new Exception('Missing request parameters');
return false;
}
if (!isset($_GET['path'])) {
throw new Exception('Missing path of the page view');
return false;
}
if (!isset($_GET['account']) && empty($this->account)) {
throw new Exception('Missing account');
return false;
}
return true;
}
/**
* Get account
*
* @return string account UA code
*/
protected function get_account() {
if (empty($this->account)) {
$this->account = $_GET['account'];
}
return trim($this->account);
}
/**
* Get path
*
* @return string account UA code
*/
protected function get_path() {
$path = trim(urldecode($_GET['path']));
if (empty($path)) {
$path = '/';
}
return $path;
}
/**
* Get UUID
*
* @return string randomized uuid
*/
protected function get_uuid() {
if (isset($_COOKIE['cid']) && !empty($_COOKIE['cid'])) {
return (string) $_COOKIE['cid'];
}
$cid = (string) random_bytes(16);
$_COOKIE['cid'] = $cid;
setcookie('cid', $cid, time()+24*60*60);
return $cid;
}
/**
* Get IP
*
* @return string ip address
*/
protected function get_ip() {
if (isset($_SERVER['REMOTE_ADDR']) && !empty($_SERVER['REMOTE_ADDR'])) {
return $_SERVER['REMOTE_ADDR'];
}
return '';
}
/**
* Send GA Pageview
*/
protected function send_pageview() {
// GA Protocol reference: https://developers.google.com/analytics/devguides/collection/protocol/v1/reference
$payload = [
'v' => 1, // protocol version = 1
't' => 'pageview', // hit type
'tid' => $this->get_account(), // tracking / property ID
'cid' => $this->get_uuid(), // unique client ID (server generated UUID)
'dp' => $this->get_path(), // page path
'uip' => $this->get_ip() // IP address of the user
];
// allow query param override to report arbitrary values to GA
foreach ($_GET as $key => $value) {
if (!in_array($key, ['account', 'path', 'tid'])) {
$payload[$key] = $value;
}
}
$headers = [
'User-Agent: ' . isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '',
'Content-Type: application/x-www-form-urlencoded'
];
$this->debug['payload'] = $payload;
$this->debug['headers'] = $headers;
$ch = curl_init($this->beacon_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($payload));
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
$this->debug['status'] = $status;
$this->debug['response'] = $response;
// debug
// var_dump($this->debug);
// die();
if ($status >= 200 & $status < 300) {
// send image metadata to browser
header('Content-Type: image/gif');
header('Cache-Control: no-cache, no-store, must-revalidate, private');
header('Expires: ' . gmdate('D, d M Y H:i:s \G\M\T', time()));
header('CID: ' . $this->get_uuid());
// send 1x1 transparent gif
echo base64_decode('R0lGODlhAQABAJAAAP8AAAAAACH5BAUQAAAALAAAAAABAAEAAAICBAEAOw==');
exit;
} else {
throw new Exception((string) $response);
}
}
}
// self init
new GA_Beacon();