-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGoogleAnalytics.php
218 lines (186 loc) · 5.94 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
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
<?php
namespace Apix\Log;
use Apix\Log\Emitter\EmitterInterface as LogEmitter;
use Psr\Log\InvalidArgumentException;
/*
TODO:
A maximum of 20 hits can be specified per request.
The total size of all hit payloads cannot be greater than 16K bytes.
No single hit payload can be greater than 8K bytes.
*/
/**
* Google Analytics logger for Apix Log.
*
* @see https://developers.google.com/analytics/devguides/collection/protocol/v1/devguide
* @see https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters
*/
class GoogleAnalytics extends AbstractTracker
{
const TRACKER_URL_ONE = 'https://www.google-analytics.com/collect';
const TRACKER_URL_MANY = 'https://www.google-analytics.com/batch';
//const DEFAULT_PARAMS = array(
private $DEFAULT_PARAMS = array(
'v' => 1, // API Version
'tid' => null, // Tracking/Property (required) ID e.g. UA-XX-XX
'cid' => null, // Anonymous Client ID UUIDv4
// see http://www.ietf.org/rfc/rfc4122.txt
'ds' => __NAMESPACE__, // Data Source
't' => null, // Hit type (required)
);
/**
* Constructor.
*
* @param array $params Array of Google Analytics parameters
*/
public function __construct(
array $params, LogEmitter $emitter = null, LogFormatter $formatter = null
) {
if (!isset($params['tid'])) {
throw new InvalidArgumentException(sprintf(
'%s expects `tid` to bet provided, got: %s.',
__CLASS__, json_encode($params)
));
}
if (!isset($params['cid'])) {
$params['cid'] = self::generateUuid();
}
$this->uuid = $params['cid'];
$this->setEmitter(
$emitter ? $emitter : new Emitter\Async(),
$formatter ? $formatter : new LogFormatter\QueryString()
);
$this->emitter->setParams($this->DEFAULT_PARAMS);
if (isset($_SERVER['HTTP_USER_AGENT']) && !isset($params['ua'])) {
$params['ua'] = $_SERVER['HTTP_USER_AGENT'];
}
if (isset($_SERVER['HTTP_REFERER']) && !isset($params['dr'])) {
$params['dr'] = $_SERVER['HTTP_REFERER'];
}
if (isset($_SERVER['REMOTE_ADDR']) && !isset($params['uip'])) {
$params['uip'] = $_SERVER['REMOTE_ADDR'];
}
$this->emitter->addParams($params);
}
/**
* Returns a Page Tracking dataset.
*
* @param string $url The full URL for ht page document
* @param string $title The title of the page / document
* @param string $location Document location URL
*
* @return array
*/
public function getPage($url, $title = null, $location = null)
{
$params = array();
if (0 != strpos($url, '/')) {
$_ = parse_url($url);
// Document hostname
if (isset($_['host'])) {
$params['dh'] = $_['host'];
}
// Page
$params['dp'] = $_['path'];
}
// Page title
if ($title) {
$params['dt'] = $title;
}
// Document location URL
$params['dl'] = $location ? $location : $url;
return $this->get('pageview', $params);
}
/**
* Returns an Event Tracking dataset.
*
* @param string $category
* @param string $action
* @param string $label
* @param string $value
*
* @return array
*/
public function getEvent($category, $action, $label = null, $value = null)
{
$params = array(
'ec' => $category, // Event Category. Required.
'ea' => $action, // Event Action. Required.
);
// Event label
if ($label) {
$params['el'] = (string) $label;
}
// Event value
if ($value) {
$params['ev'] = (int) $value; // GA does not allow float!
}
return $this->get('event', $params);
}
/**
* Returns a Social Interactions dataset.
*
* @param string $action Social Action (e.g. like)
* @param string $label Social Network (e.g. facebook)
* @param string $value Social Target. (e.g. /home)
*
* @return array
*/
public function getSocial($action, $network, $target)
{
$params = array(
'sa' => (string) $action,
'sn' => (string) $network,
'st' => (string) $target,
);
return $this->get('social', $params);
}
/**
* Returns an Exception Tracking dataset.
*
* @param string $description Exception description
* @param string $isFatal Specifies whether the exception was fatal
*
* @return array
*/
public function getException($description, $isFatal = true)
{
$params = array(
'exd' => (string) $description,
'exf' => $isFatal ? '1' : '0',
);
return $this->get('exception', $params);
}
/**
* Returns an App / Screen Tracking dataset.
*
* @param string $name App name
* @param string $version App version
* @param string $id App Id
* @param string $iid App Installer Id
*
* @return array
*/
public function getApp($name, $version = null, $id = null, $iid = null)
{
$params = array(
'an' => (string) $name,
'av' => (string) $version,
'aid' => (string) $id,
'aiid' => (string) $iid,
);
return $this->get('screenview', $params);
}
/**
* Returns the named tracking dataset.
*
* @return array
*/
public function get($type, array $params)
{
$this->emitter->setParam('t', $type);
$this->emitter->setUrl(
$this->deferred ? self::TRACKER_URL_MANY : self::TRACKER_URL_ONE
);
return array_merge($this->emitter->getParams(), $params);
}
}