-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathGoogleAnalytics.php
More file actions
156 lines (129 loc) · 4.5 KB
/
Copy pathGoogleAnalytics.php
File metadata and controls
156 lines (129 loc) · 4.5 KB
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
<?php
namespace Utopia\Analytics\Adapter;
use Utopia\Analytics\Adapter;
use Utopia\Analytics\Event;
class GoogleAnalytics extends Adapter
{
/**
* Endpoint for Google Analytics
*/
public string $endpoint = 'https://www.google-analytics.com/collect';
/**
* Endpoint for Google Analytics Debug
*/
public string $debugEndpoint = 'https://www.google-analytics.com/debug/collect';
/**
* Tracking ID for Google Analytics
*/
private string $tid;
/**
* A unique identifer for Google Analytics
*/
private string $cid;
/**
* Gets the name of the adapter.
*/
public function getName(): string
{
return 'Google Analytics';
}
/**
* @param string $cid
* Adapter configuration
* @return GoogleAnalytics
*/
public function __construct(string $tid, string $cid)
{
$this->tid = $tid;
$this->cid = $cid;
}
public function validate(Event $event): bool
{
if (! $this->enabled) {
return false;
}
if (empty($event->getType())) {
throw new \Exception('Event type is required');
}
if (empty($event->getUrl())) {
throw new \Exception('Event URL is required');
}
if (empty($event->getName())) {
throw new \Exception('Event name is required');
}
$query = [
'ec' => $event->getProp('category'),
'ea' => $event->getProp('action'),
'el' => $event->getName(),
'ev' => $event->getValue(),
'dh' => parse_url($event->getUrl())['host'],
'dp' => parse_url($event->getUrl())['path'],
'dt' => $event->getProp('documentTitle'),
't' => ($event->getType() === 'pageview') ? 'pageview' : 'event',
'uip' => $this->clientIP ?? '',
'ua' => $this->userAgent ?? '',
'sr' => $event->getProp('screenResolution'),
'vp' => $event->getProp('viewportSize'),
'dr' => $event->getProp('referrer'),
];
$query = array_filter($query, fn ($value) => ! is_null($value) && $value !== '');
$validateResponse = $this->call('POST', $this->debugEndpoint, [], array_merge(
$query,
[
'tid' => $this->tid,
'cid' => $this->cid,
'v' => 1,
]
));
$validateResponse = json_decode($validateResponse, true);
if ($validateResponse['hitParsingResult'][0]['valid'] !== true) {
throw new \Exception('Invalid event');
}
return true;
}
/**
* Creates an Event on the remote analytics platform.
*/
public function send(Event $event): bool
{
if (! $this->enabled) {
return false;
}
if ($event->getType() !== 'pageview') {
$event->setProps(array_merge($event->getProps(), ['action' => $event->getType()]));
$event->setType('event');
}
if ($event->getProp('screenWidth') && $event->getProp('screenHeight')) {
$event->setProps(array_merge($event->getProps(), ['screenResolution' => $event->getProp('screenWidth').'x'.$event->getProp('screenHeight')]));
}
$query = [
'ec' => $event->getProp('category'),
'ea' => $event->getProp('action'),
'el' => $event->getName(),
'ev' => $event->getValue(),
'dh' => parse_url($event->getUrl())['host'],
'dp' => parse_url($event->getUrl())['path'],
'dt' => $event->getProp('documentTitle'),
't' => $event->getType(),
'uip' => $this->clientIP ?? '',
'ua' => $this->userAgent ?? '',
'sr' => $event->getProp('screenResolution'),
'vp' => $event->getProp('viewportSize'),
'dr' => $event->getProp('referrer'),
];
if ($event->getProp('account')) {
$query['cd1'] = $event->getProp('account');
}
$query = array_filter($query, fn ($value) => ! is_null($value) && $value !== '');
$result = $this->call('POST', $this->endpoint, [], array_merge([
'tid' => $this->tid,
'cid' => $this->cid,
'v' => 1,
], $query));
// Parse Debug data
if ($this->endpoint == 'https://www.google-analytics.com/debug/collect') {
return json_decode($result, true)['hitParsingResult'][0]['valid'];
}
return true;
}
}