-
Notifications
You must be signed in to change notification settings - Fork 92
/
Copy pathAnalytics.php
386 lines (345 loc) · 9.15 KB
/
Analytics.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
<?php
namespace AntiMattr\GoogleBundle;
use AntiMattr\GoogleBundle\Analytics\CustomVariable;
use AntiMattr\GoogleBundle\Analytics\Event;
use AntiMattr\GoogleBundle\Analytics\Item;
use AntiMattr\GoogleBundle\Analytics\Transaction;
use Symfony\Component\DependencyInjection\ContainerInterface;
class Analytics
{
const EVENT_QUEUE_KEY = 'google_analytics/event/queue';
const CUSTOM_PAGE_VIEW_KEY = 'google_analytics/page_view';
const PAGE_VIEW_QUEUE_KEY = 'google_analytics/page_view/queue';
const TRANSACTION_KEY = 'google_analytics/transaction';
const ITEMS_KEY = 'google_analytics/items';
private $container;
private $customVariables = array();
private $pageViewsWithBaseUrl = true;
private $trackers;
public function __construct(ContainerInterface $container, array $trackers = array())
{
$this->container = $container;
$this->trackers = $trackers;
}
public function excludeBaseUrl()
{
$this->pageViewsWithBaseUrl = false;
}
public function includeBaseUrl()
{
$this->pageViewsWithBaseUrl = true;
}
/**
* @param string $trackerKey
* @param boolean $allowHash
*/
public function setAllowHash($trackerKey, $allowHash)
{
if (!array_key_exists($trackerKey, $this->trackers)) {
return;
}
$this->trackers[$trackerKey]['allowHash'] = $allowHash;
}
/**
* @param string $trackerKey
* @return boolean $allowHash
*/
public function getAllowHash($trackerKey)
{
if (!array_key_exists($trackerKey, $this->trackers)) {
return false;
}
$trackerConfig = $this->trackers[$trackerKey];
if (!array_key_exists('allowHash', $trackerConfig)) {
return false;
}
return $trackerConfig['allowHash'];
}
/**
* @param string $trackerKey
* @param boolean $allowLinker
*/
public function setAllowLinker($trackerKey, $allowLinker)
{
if (!array_key_exists($trackerKey, $this->trackers)) {
return;
}
$this->trackers[$trackerKey]['allowLinker'] = $allowLinker;
}
/**
* @param string $trackerKey
* @return boolean $allowLinker
*/
public function getAllowLinker($trackerKey)
{
if (!array_key_exists($trackerKey, $this->trackers)) {
return true;
}
$trackerConfig = $this->trackers[$trackerKey];
if (!array_key_exists('allowLinker', $trackerConfig)) {
return true;
}
return $trackerConfig['allowLinker'];
}
/**
* @return string $customPageView
*/
public function getCustomPageView()
{
$customPageView = $this->container->get('session')->get(self::CUSTOM_PAGE_VIEW_KEY);
$this->container->get('session')->remove(self::CUSTOM_PAGE_VIEW_KEY);
return $customPageView;
}
/**
* @return boolean $hasCustomPageView
*/
public function hasCustomPageView()
{
return $this->has(self::CUSTOM_PAGE_VIEW_KEY);
}
/**
* @param string $customPageView
*/
public function setCustomPageView($customPageView)
{
$this->container->get('session')->set(self::CUSTOM_PAGE_VIEW_KEY, $customPageView);
}
/**
* @param CustomVariable $customVariable
*/
public function addCustomVariable(CustomVariable $customVariable)
{
$this->customVariables[] = $customVariable;
}
/**
* @return array $customVariables
*/
public function getCustomVariables()
{
return $this->customVariables;
}
/**
* @return boolean $hasCustomVariables
*/
public function hasCustomVariables()
{
if (!empty($this->customVariables)) {
return true;
}
return false;
}
/**
* @param Event $event
*/
public function enqueueEvent(Event $event)
{
$this->add(self::EVENT_QUEUE_KEY, $event);
}
/**
* @param array $eventQueue
*/
public function getEventQueue()
{
return $this->getOnce(self::EVENT_QUEUE_KEY);
}
/**
* @return boolean $hasEventQueue
*/
public function hasEventQueue()
{
return $this->has(self::EVENT_QUEUE_KEY);
}
/**
* @param Item $item
*/
public function addItem(Item $item)
{
$this->add(self::ITEMS_KEY, $item);
}
/**
* @return boolean $hasItems
*/
public function hasItems()
{
return $this->has(self::ITEMS_KEY);
}
/**
* @param Item $item
* @return boolean $hasItem
*/
public function hasItem(Item $item)
{
if (!$this->hasItems()) {
return false;
}
$items = $this->getItemsFromSession();
return in_array($item, $items, true);
}
/**
* @param array $items
*/
public function setItems($items)
{
$this->container->get('session')->set(self::ITEMS_KEY, $items);
}
public function getItems()
{
return $this->getOnce(self::ITEMS_KEY);
}
/**
* @param string $pageView
*/
public function enqueuePageView($pageView)
{
$this->add(self::PAGE_VIEW_QUEUE_KEY, $pageView);
}
/**
* @param array $pageViewQueue
*/
public function getPageViewQueue()
{
return $this->getOnce(self::PAGE_VIEW_QUEUE_KEY);
}
/**
* @return boolean $hasPageViewQueue
*/
public function hasPageViewQueue()
{
return $this->has(self::PAGE_VIEW_QUEUE_KEY);
}
/**
* @return Symfony\Component\HttpFoundation\Request $request
*/
public function getRequest()
{
return $this->container->get('request');
}
/**
* If Custom Page view not set,
* Then requestUri is used as an alternative
*
* @return string $requestUri
*/
public function getRequestUri()
{
$request = $this->getRequest();
$requestUri = $request->getRequestUri();
if (!$this->pageViewsWithBaseUrl) {
$baseUrl = $request->getBaseUrl();
if ($baseUrl != '/') {
return str_replace($baseUrl, '', $requestUri);
}
return $requestUri;
}
return $requestUri;
}
/**
* @return array $trackers
*/
public function getTrackers(array $trackers = array())
{
if (!empty($trackers)) {
$trackers = array();
foreach ($trackers as $key) {
if (isset($this->trackers[$key])) {
$trackers[$key] = $this->trackers[$key];
}
}
return $trackers;
} else {
return $this->trackers;
}
}
/**
* @return boolean $isTransactionValid
*/
public function isTransactionValid()
{
if (!$this->hasTransaction() || (null === $this->getTransactionFromSession()->getOrderNumber())) {
return false;
}
if ($this->hasItems()) {
$items = $this->getItemsFromSession();
foreach ($items as $item) {
if (!$item->getOrderNumber() || !$item->getSku() || !$item->getPrice() || !$item->getQuantity()) {
return false;
}
}
}
return true;
}
/**
* @return Transaction $transaction
*/
public function getTransaction()
{
$transaction = $this->getTransactionFromSession();
$this->container->get('session')->remove(self::TRANSACTION_KEY);
return $transaction;
}
/**
* @return boolean $hasTransaction
*/
public function hasTransaction()
{
return $this->has(self::TRANSACTION_KEY);
}
/**
* @param Transaction $transaction
*/
public function setTransaction(Transaction $transaction)
{
$this->container->get('session')->set(self::TRANSACTION_KEY, $transaction);
}
/**
* @param string $key
* @param mixed $value
*/
private function add($key, $value)
{
$bucket = $this->container->get('session')->get($key, array());
$bucket[] = $value;
$this->container->get('session')->set($key, $bucket);
}
/**
* @param string $key
* @return boolean $hasKey
*/
private function has($key)
{
$bucket = $this->container->get('session')->get($key, array());
return !empty($bucket);
}
/**
* @param string $key
* @return array $value
*/
private function get($key)
{
return $this->container->get('session')->get($key, array());
}
/**
* @param string $key
* @return array $value
*/
private function getOnce($key)
{
$value = $this->container->get('session')->get($key, array());
$this->container->get('session')->remove($key);
return $value;
}
/**
* @return array $items
*/
private function getItemsFromSession()
{
return $this->get(self::ITEMS_KEY);
}
/**
* @return Transaction $transaction
*/
private function getTransactionFromSession()
{
return $this->container->get('session')->get(self::TRANSACTION_KEY);
}
}