|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace TheIconic\Tracking\GoogleAnalytics; |
|
4
|
|
|
|
|
5
|
|
|
use TheIconic\Tracking\GoogleAnalytics\Parameters\SingleParameter; |
|
6
|
|
|
use TheIconic\Tracking\GoogleAnalytics\Parameters\CompoundParameterCollection; |
|
7
|
|
|
use TheIconic\Tracking\GoogleAnalytics\Network\HttpClient; |
|
8
|
|
|
use TheIconic\Tracking\GoogleAnalytics\Exception\InvalidPayloadDataException; |
|
9
|
|
|
use Symfony\Component\Finder\Finder; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Class Analytics |
|
13
|
|
|
* |
|
14
|
|
|
* The main interface for the clients, it relies heavily in magic methods exposing |
|
15
|
|
|
* an interface with method tags. |
|
16
|
|
|
* |
|
17
|
|
|
* General |
|
18
|
|
|
* @method \TheIconic\Tracking\GoogleAnalytics\Analytics setProtocolVersion($value) |
|
19
|
|
|
* @method \TheIconic\Tracking\GoogleAnalytics\Analytics setTrackingId($value) |
|
20
|
|
|
* @method \TheIconic\Tracking\GoogleAnalytics\Analytics setAnonymizeIp($value) |
|
21
|
|
|
* @method \TheIconic\Tracking\GoogleAnalytics\Analytics setDataSource($value) |
|
22
|
|
|
* @method \TheIconic\Tracking\GoogleAnalytics\Analytics setQueueTime($value) |
|
23
|
|
|
* @method \TheIconic\Tracking\GoogleAnalytics\Analytics setCacheBuster($value) |
|
24
|
|
|
* |
|
25
|
|
|
* User |
|
26
|
|
|
* @method \TheIconic\Tracking\GoogleAnalytics\Analytics setClientId($value) |
|
27
|
|
|
* @method \TheIconic\Tracking\GoogleAnalytics\Analytics setUserId($value) |
|
28
|
|
|
* |
|
29
|
|
|
* Session |
|
30
|
|
|
* @method \TheIconic\Tracking\GoogleAnalytics\Analytics setSessionControl($value) |
|
31
|
|
|
* @method \TheIconic\Tracking\GoogleAnalytics\Analytics setIpOverride($value) |
|
32
|
|
|
* @method \TheIconic\Tracking\GoogleAnalytics\Analytics setUserAgentOverride($value) |
|
33
|
|
|
* @method \TheIconic\Tracking\GoogleAnalytics\Analytics setGeographicalOverride($value) |
|
34
|
|
|
* |
|
35
|
|
|
* Traffic Sources |
|
36
|
|
|
* @method \TheIconic\Tracking\GoogleAnalytics\Analytics setDocumentReferrer($value) |
|
37
|
|
|
* @method \TheIconic\Tracking\GoogleAnalytics\Analytics setCampaignName($value) |
|
38
|
|
|
* @method \TheIconic\Tracking\GoogleAnalytics\Analytics setCampaignSource($value) |
|
39
|
|
|
* @method \TheIconic\Tracking\GoogleAnalytics\Analytics setCampaignMedium($value) |
|
40
|
|
|
* @method \TheIconic\Tracking\GoogleAnalytics\Analytics setCampaignKeyword($value) |
|
41
|
|
|
* @method \TheIconic\Tracking\GoogleAnalytics\Analytics setCampaignContent($value) |
|
42
|
|
|
* @method \TheIconic\Tracking\GoogleAnalytics\Analytics setCampaignId($value) |
|
43
|
|
|
* @method \TheIconic\Tracking\GoogleAnalytics\Analytics setGoogleAdwordsId($value) |
|
44
|
|
|
* @method \TheIconic\Tracking\GoogleAnalytics\Analytics setGoogleDisplayAdsId($value) |
|
45
|
|
|
* |
|
46
|
|
|
* System Info |
|
47
|
|
|
* @method \TheIconic\Tracking\GoogleAnalytics\Analytics setScreenResolution($value) |
|
48
|
|
|
* @method \TheIconic\Tracking\GoogleAnalytics\Analytics setViewportSize($value) |
|
49
|
|
|
* @method \TheIconic\Tracking\GoogleAnalytics\Analytics setDocumentEncoding($value) |
|
50
|
|
|
* @method \TheIconic\Tracking\GoogleAnalytics\Analytics setScreenColors($value) |
|
51
|
|
|
* @method \TheIconic\Tracking\GoogleAnalytics\Analytics setUserLanguage($value) |
|
52
|
|
|
* @method \TheIconic\Tracking\GoogleAnalytics\Analytics setJavaEnabled($value) |
|
53
|
|
|
* @method \TheIconic\Tracking\GoogleAnalytics\Analytics setFlashVersion($value) |
|
54
|
|
|
* |
|
55
|
|
|
* Hit |
|
56
|
|
|
* @method \TheIconic\Tracking\GoogleAnalytics\Analytics setHitType($value) |
|
57
|
|
|
* @method \TheIconic\Tracking\GoogleAnalytics\Analytics setNonInteractionHit($value) |
|
58
|
|
|
* @method \TheIconic\Tracking\GoogleAnalytics\AnalyticsResponse sendPageview() |
|
59
|
|
|
* @method \TheIconic\Tracking\GoogleAnalytics\AnalyticsResponse sendEvent() |
|
60
|
|
|
* @method \TheIconic\Tracking\GoogleAnalytics\AnalyticsResponse sendScreenview() |
|
61
|
|
|
* @method \TheIconic\Tracking\GoogleAnalytics\AnalyticsResponse sendTransaction() |
|
62
|
|
|
* @method \TheIconic\Tracking\GoogleAnalytics\AnalyticsResponse sendItem() |
|
63
|
|
|
* @method \TheIconic\Tracking\GoogleAnalytics\AnalyticsResponse sendSocial() |
|
64
|
|
|
* @method \TheIconic\Tracking\GoogleAnalytics\AnalyticsResponse sendException() |
|
65
|
|
|
* @method \TheIconic\Tracking\GoogleAnalytics\AnalyticsResponse sendTiming() |
|
66
|
|
|
* |
|
67
|
|
|
* Content Information |
|
68
|
|
|
* @method \TheIconic\Tracking\GoogleAnalytics\Analytics setDocumentLocationUrl($value) |
|
69
|
|
|
* @method \TheIconic\Tracking\GoogleAnalytics\Analytics setDocumentHostName($value) |
|
70
|
|
|
* @method \TheIconic\Tracking\GoogleAnalytics\Analytics setDocumentPath($value) |
|
71
|
|
|
* @method \TheIconic\Tracking\GoogleAnalytics\Analytics setDocumentTitle($value) |
|
72
|
|
|
* @method \TheIconic\Tracking\GoogleAnalytics\Analytics setScreenName($value) |
|
73
|
|
|
* @method \TheIconic\Tracking\GoogleAnalytics\Analytics setLinkId($value) |
|
74
|
|
|
* |
|
75
|
|
|
* App Tracking |
|
76
|
|
|
* @method \TheIconic\Tracking\GoogleAnalytics\Analytics setApplicationName($value) |
|
77
|
|
|
* @method \TheIconic\Tracking\GoogleAnalytics\Analytics setApplicationId($value) |
|
78
|
|
|
* @method \TheIconic\Tracking\GoogleAnalytics\Analytics setApplicationVersion($value) |
|
79
|
|
|
* @method \TheIconic\Tracking\GoogleAnalytics\Analytics setApplicationInstallerId($value) |
|
80
|
|
|
* |
|
81
|
|
|
* Event Tracking |
|
82
|
|
|
* @method \TheIconic\Tracking\GoogleAnalytics\Analytics setEventCategory($value) |
|
83
|
|
|
* @method \TheIconic\Tracking\GoogleAnalytics\Analytics setEventAction($value) |
|
84
|
|
|
* @method \TheIconic\Tracking\GoogleAnalytics\Analytics setEventLabel($value) |
|
85
|
|
|
* @method \TheIconic\Tracking\GoogleAnalytics\Analytics setEventValue($value) |
|
86
|
|
|
* |
|
87
|
|
|
* E-commerce |
|
88
|
|
|
* @method \TheIconic\Tracking\GoogleAnalytics\Analytics setItemName($value) |
|
89
|
|
|
* @method \TheIconic\Tracking\GoogleAnalytics\Analytics setItemPrice($value) |
|
90
|
|
|
* @method \TheIconic\Tracking\GoogleAnalytics\Analytics setItemQuantity($value) |
|
91
|
|
|
* @method \TheIconic\Tracking\GoogleAnalytics\Analytics setItemCode($value) |
|
92
|
|
|
* @method \TheIconic\Tracking\GoogleAnalytics\Analytics setItemCategory($value) |
|
93
|
|
|
* @method \TheIconic\Tracking\GoogleAnalytics\Analytics setCurrencyCode($value) |
|
94
|
|
|
* |
|
95
|
|
|
* Enhanced E-Commerce |
|
96
|
|
|
* @method \TheIconic\Tracking\GoogleAnalytics\Analytics setTransactionId($value) |
|
97
|
|
|
* @method \TheIconic\Tracking\GoogleAnalytics\Analytics setAffiliation($value) |
|
98
|
|
|
* @method \TheIconic\Tracking\GoogleAnalytics\Analytics setRevenue($value) |
|
99
|
|
|
* @method \TheIconic\Tracking\GoogleAnalytics\Analytics setTax($value) |
|
100
|
|
|
* @method \TheIconic\Tracking\GoogleAnalytics\Analytics setShipping($value) |
|
101
|
|
|
* @method \TheIconic\Tracking\GoogleAnalytics\Analytics setCouponCode($value) |
|
102
|
|
|
* @method \TheIconic\Tracking\GoogleAnalytics\Analytics setProductActionList($value) |
|
103
|
|
|
* @method \TheIconic\Tracking\GoogleAnalytics\Analytics setCheckoutStep($value) |
|
104
|
|
|
* @method \TheIconic\Tracking\GoogleAnalytics\Analytics setCheckoutStepOption($value) |
|
105
|
|
|
* @method \TheIconic\Tracking\GoogleAnalytics\Analytics addProduct(array $productData) |
|
106
|
|
|
* @method \TheIconic\Tracking\GoogleAnalytics\Analytics setProductAction($value) |
|
107
|
|
|
* @method \TheIconic\Tracking\GoogleAnalytics\Analytics setProductActionToDetail() |
|
108
|
|
|
* @method \TheIconic\Tracking\GoogleAnalytics\Analytics setProductActionToClick() |
|
109
|
|
|
* @method \TheIconic\Tracking\GoogleAnalytics\Analytics setProductActionToAdd() |
|
110
|
|
|
* @method \TheIconic\Tracking\GoogleAnalytics\Analytics setProductActionToRemove() |
|
111
|
|
|
* @method \TheIconic\Tracking\GoogleAnalytics\Analytics setProductActionToCheckout() |
|
112
|
|
|
* @method \TheIconic\Tracking\GoogleAnalytics\Analytics setProductActionToCheckoutOption() |
|
113
|
|
|
* @method \TheIconic\Tracking\GoogleAnalytics\Analytics setProductActionToPurchase() |
|
114
|
|
|
* @method \TheIconic\Tracking\GoogleAnalytics\Analytics setProductActionToRefund() |
|
115
|
|
|
* @method \TheIconic\Tracking\GoogleAnalytics\Analytics setProductImpressionListName($value, $index) |
|
116
|
|
|
* @method \TheIconic\Tracking\GoogleAnalytics\Analytics addProductImpression(array $productData, $index) |
|
117
|
|
|
* @method \TheIconic\Tracking\GoogleAnalytics\Analytics addPromotion(array $promotionData) |
|
118
|
|
|
* @method \TheIconic\Tracking\GoogleAnalytics\Analytics setPromotionAction($value) |
|
119
|
|
|
* @method \TheIconic\Tracking\GoogleAnalytics\Analytics setPromotionActionToClick() |
|
120
|
|
|
* @method \TheIconic\Tracking\GoogleAnalytics\Analytics setPromotionActionToView() |
|
121
|
|
|
* |
|
122
|
|
|
* Social Interactions |
|
123
|
|
|
* @method \TheIconic\Tracking\GoogleAnalytics\Analytics setSocialNetwork($value) |
|
124
|
|
|
* @method \TheIconic\Tracking\GoogleAnalytics\Analytics setSocialAction($value) |
|
125
|
|
|
* @method \TheIconic\Tracking\GoogleAnalytics\Analytics setSocialActionTarget($value) |
|
126
|
|
|
* |
|
127
|
|
|
* Timing |
|
128
|
|
|
* @method \TheIconic\Tracking\GoogleAnalytics\Analytics setUserTiminCategory($value) |
|
129
|
|
|
* @method \TheIconic\Tracking\GoogleAnalytics\Analytics setUserTimingVariableName($value) |
|
130
|
|
|
* @method \TheIconic\Tracking\GoogleAnalytics\Analytics setUserTimingTime($value) |
|
131
|
|
|
* @method \TheIconic\Tracking\GoogleAnalytics\Analytics setUserTimingLabel($value) |
|
132
|
|
|
* @method \TheIconic\Tracking\GoogleAnalytics\Analytics setPageLoadTime($value) |
|
133
|
|
|
* @method \TheIconic\Tracking\GoogleAnalytics\Analytics setDnsTime($value) |
|
134
|
|
|
* @method \TheIconic\Tracking\GoogleAnalytics\Analytics setPageDownloadTime($value) |
|
135
|
|
|
* @method \TheIconic\Tracking\GoogleAnalytics\Analytics setRedirectResponseTime($value) |
|
136
|
|
|
* @method \TheIconic\Tracking\GoogleAnalytics\Analytics setTcpConnectTime($value) |
|
137
|
|
|
* @method \TheIconic\Tracking\GoogleAnalytics\Analytics setServerResponseTime($value) |
|
138
|
|
|
* @method \TheIconic\Tracking\GoogleAnalytics\Analytics setDomInteractiveTime($value) |
|
139
|
|
|
* @method \TheIconic\Tracking\GoogleAnalytics\Analytics setContentLoadTime($value) |
|
140
|
|
|
* |
|
141
|
|
|
* Exceptions |
|
142
|
|
|
* @method \TheIconic\Tracking\GoogleAnalytics\Analytics setExceptionDescription($value) |
|
143
|
|
|
* @method \TheIconic\Tracking\GoogleAnalytics\Analytics setIsExceptionFatal($value) |
|
144
|
|
|
* |
|
145
|
|
|
* Custom Dimension / Metrics |
|
146
|
|
|
* @method \TheIconic\Tracking\GoogleAnalytics\Analytics setCustomDimension($value, $index) |
|
147
|
|
|
* @method \TheIconic\Tracking\GoogleAnalytics\Analytics setCustomMetric($value, $index) |
|
148
|
|
|
* |
|
149
|
|
|
* Content Grouping |
|
150
|
|
|
* @method \TheIconic\Tracking\GoogleAnalytics\Analytics setContentGroup($value, $index) |
|
151
|
|
|
* |
|
152
|
|
|
* Content Experiments |
|
153
|
|
|
* @method \TheIconic\Tracking\GoogleAnalytics\Analytics setExperimentId($value) |
|
154
|
|
|
* @method \TheIconic\Tracking\GoogleAnalytics\Analytics setExperimentVariant($value) |
|
155
|
|
|
* |
|
156
|
|
|
* |
|
157
|
|
|
* @package TheIconic\Tracking\GoogleAnalytics |
|
158
|
|
|
*/ |
|
159
|
|
|
class Analytics |
|
160
|
|
|
{ |
|
161
|
|
|
/** |
|
162
|
|
|
* URI scheme for the GA API. |
|
163
|
|
|
* |
|
164
|
|
|
* @var string |
|
165
|
|
|
*/ |
|
166
|
|
|
private $uriScheme = 'http'; |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* Indicates if the request to GA will be asynchronous (non-blocking). |
|
170
|
|
|
* |
|
171
|
|
|
* @var boolean |
|
172
|
|
|
*/ |
|
173
|
|
|
private $isAsyncRequest = false; |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* Endpoint to connect to when sending data to GA. |
|
177
|
|
|
* |
|
178
|
|
|
* @var string |
|
179
|
|
|
*/ |
|
180
|
|
|
private $endpoint = '://www.google-analytics.com/collect'; |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* Endpoint to connect to when validating hits. |
|
184
|
|
|
* @link https://developers.google.com/analytics/devguides/collection/protocol/v1/validating-hits |
|
185
|
|
|
* |
|
186
|
|
|
* @var string |
|
187
|
|
|
*/ |
|
188
|
|
|
private $debugEndpoint = '://www.google-analytics.com/debug/collect'; |
|
189
|
|
|
|
|
190
|
|
|
/** |
|
191
|
|
|
* Indicates if the request is in debug mode(validating hits). |
|
192
|
|
|
* |
|
193
|
|
|
* @var boolean |
|
194
|
|
|
*/ |
|
195
|
|
|
private $isDebug = false; |
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* Holds the single parameters added to the hit. |
|
199
|
|
|
* |
|
200
|
|
|
* @var SingleParameter[] |
|
201
|
|
|
*/ |
|
202
|
|
|
private $singleParameters = []; |
|
203
|
|
|
|
|
204
|
|
|
/** |
|
205
|
|
|
* Holds the compound parameters collections added to the hit. |
|
206
|
|
|
* |
|
207
|
|
|
* @var CompoundParameterCollection[] |
|
208
|
|
|
*/ |
|
209
|
|
|
private $compoundParametersCollections = []; |
|
210
|
|
|
|
|
211
|
|
|
/** |
|
212
|
|
|
* Initializes to a list of all the available parameters to be sent in a hit. |
|
213
|
|
|
* |
|
214
|
|
|
* @var array |
|
215
|
|
|
*/ |
|
216
|
|
|
private $availableParameters; |
|
217
|
|
|
|
|
218
|
|
|
/** |
|
219
|
|
|
* Holds the HTTP client used to connect to GA. |
|
220
|
|
|
* |
|
221
|
|
|
* @var HttpClient |
|
222
|
|
|
*/ |
|
223
|
|
|
private $httpClient; |
|
224
|
|
|
|
|
225
|
|
|
/** |
|
226
|
|
|
* When passed with an argument of TRUE, it will send the hit using HTTPS instead of plain HTTP. |
|
227
|
|
|
* It parses the available parameters. |
|
228
|
|
|
* |
|
229
|
|
|
* @param bool $isSsl |
|
230
|
|
|
* @throws \InvalidArgumentException |
|
231
|
|
|
*/ |
|
232
|
|
|
public function __construct($isSsl = false) |
|
233
|
|
|
{ |
|
234
|
|
|
if (!is_bool($isSsl)) { |
|
235
|
|
|
throw new \InvalidArgumentException('First constructor argument "isSSL" must be boolean'); |
|
236
|
|
|
} |
|
237
|
|
|
|
|
238
|
|
|
if ($isSsl) { |
|
239
|
|
|
$this->uriScheme .= 's'; |
|
240
|
|
|
$this->endpoint = str_replace('www', 'ssl', $this->endpoint); |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
|
|
$this->availableParameters = $this->getAvailableParameters(); |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
|
|
/** |
|
247
|
|
|
* Sets a request to be either synchronous or asynchronous (non-blocking). |
|
248
|
|
|
* |
|
249
|
|
|
* @param boolean $isAsyncRequest |
|
250
|
|
|
* @return $this |
|
251
|
|
|
*/ |
|
252
|
|
|
public function setAsyncRequest($isAsyncRequest) |
|
253
|
|
|
{ |
|
254
|
|
|
$this->isAsyncRequest = $isAsyncRequest; |
|
255
|
|
|
|
|
256
|
|
|
return $this; |
|
257
|
|
|
} |
|
258
|
|
|
|
|
259
|
|
|
/** |
|
260
|
|
|
* Makes the request to GA asynchronous (non-blocking). |
|
261
|
|
|
* |
|
262
|
|
|
* @deprecated Use setAsyncRequest(boolean $isAsyncRequest) instead. To be removed in next major version. |
|
263
|
|
|
* |
|
264
|
|
|
* @return $this |
|
265
|
|
|
*/ |
|
266
|
|
|
public function makeNonBlocking() |
|
267
|
|
|
{ |
|
268
|
|
|
$this->isAsyncRequest = true; |
|
269
|
|
|
|
|
270
|
|
|
return $this; |
|
271
|
|
|
} |
|
272
|
|
|
|
|
273
|
|
|
/** |
|
274
|
|
|
* Sets the HttpClient. |
|
275
|
|
|
* |
|
276
|
|
|
* @param HttpClient $httpClient |
|
277
|
|
|
* @return $this |
|
278
|
|
|
*/ |
|
279
|
|
|
public function setHttpClient(HttpClient $httpClient) |
|
280
|
|
|
{ |
|
281
|
|
|
$this->httpClient = $httpClient; |
|
282
|
|
|
|
|
283
|
|
|
return $this; |
|
284
|
|
|
} |
|
285
|
|
|
|
|
286
|
|
|
/** |
|
287
|
|
|
* Gets the HttpClient. |
|
288
|
|
|
* |
|
289
|
|
|
* @return HttpClient |
|
290
|
|
|
*/ |
|
291
|
|
|
private function getHttpClient() |
|
292
|
|
|
{ |
|
293
|
|
|
if ($this->httpClient === null) { |
|
294
|
|
|
// @codeCoverageIgnoreStart |
|
295
|
|
|
$this->setHttpClient(new HttpClient()); |
|
296
|
|
|
} |
|
297
|
|
|
// @codeCoverageIgnoreEnd |
|
298
|
|
|
|
|
299
|
|
|
return $this->httpClient; |
|
300
|
|
|
} |
|
301
|
|
|
|
|
302
|
|
|
/** |
|
303
|
|
|
* Returns an array containing all the available parameters that can be sent in the hit. |
|
304
|
|
|
* |
|
305
|
|
|
* @return array |
|
306
|
|
|
*/ |
|
307
|
|
|
private function getAvailableParameters() |
|
308
|
|
|
{ |
|
309
|
|
|
$parameterClassNames = []; |
|
310
|
|
|
|
|
311
|
|
|
$finder = new Finder(); |
|
312
|
|
|
|
|
313
|
|
|
$finder->files()->in(__DIR__ . '/Parameters'); |
|
314
|
|
|
|
|
315
|
|
|
foreach ($finder as $file) { |
|
316
|
|
|
$categorisedParameter = str_replace( |
|
317
|
|
|
['.php', '/'], |
|
318
|
|
|
['', '\\'], |
|
319
|
|
|
$file->getRelativePathname() |
|
320
|
|
|
); |
|
321
|
|
|
$categorisedParameterArray = explode('\\', $categorisedParameter); |
|
322
|
|
|
|
|
323
|
|
|
$validCategorisedParameterCount = 2; |
|
324
|
|
|
if (count($categorisedParameterArray) >= $validCategorisedParameterCount) { |
|
325
|
|
|
$parameterClassNames[$categorisedParameterArray[1]] = $categorisedParameter; |
|
326
|
|
|
} |
|
327
|
|
|
} |
|
328
|
|
|
|
|
329
|
|
|
return $parameterClassNames; |
|
330
|
|
|
} |
|
331
|
|
|
|
|
332
|
|
|
/** |
|
333
|
|
|
* Gets the full endpoint to GA. |
|
334
|
|
|
* |
|
335
|
|
|
* @return string |
|
336
|
|
|
*/ |
|
337
|
|
|
private function getEndpoint() |
|
338
|
|
|
{ |
|
339
|
|
|
return ($this->isDebug)?$this->uriScheme . $this->debugEndpoint:$this->uriScheme . $this->endpoint; |
|
340
|
|
|
} |
|
341
|
|
|
|
|
342
|
|
|
/** |
|
343
|
|
|
* Set debug mode true or false |
|
344
|
|
|
* |
|
345
|
|
|
*/ |
|
346
|
|
|
public function setDebug($value) { |
|
347
|
|
|
$this->isDebug = $value; |
|
348
|
|
|
return $this; |
|
349
|
|
|
} |
|
350
|
|
|
|
|
351
|
|
|
/** |
|
352
|
|
|
* Sends a hit to GA. The hit will contain in the payload all the parameters added before. |
|
353
|
|
|
* |
|
354
|
|
|
* @param $methodName |
|
355
|
|
|
* @return AnalyticsResponse |
|
356
|
|
|
* @throws Exception\InvalidPayloadDataException |
|
357
|
|
|
*/ |
|
358
|
|
|
private function sendHit($methodName) |
|
359
|
|
|
{ |
|
360
|
|
|
$hitType = strtoupper(substr($methodName, 4)); |
|
361
|
|
|
|
|
362
|
|
|
$hitConstant = $this->getParameterClassConstant( |
|
363
|
|
|
'TheIconic\Tracking\GoogleAnalytics\Parameters\Hit\HitType::HIT_TYPE_' . $hitType, |
|
364
|
|
|
'Hit type ' . $hitType . ' is not defined, check spelling' |
|
365
|
|
|
); |
|
366
|
|
|
|
|
367
|
|
|
$this->setHitType($hitConstant); |
|
368
|
|
|
|
|
369
|
|
|
if (!$this->hasMinimumRequiredParameters()) { |
|
370
|
|
|
throw new InvalidPayloadDataException(); |
|
371
|
|
|
} |
|
372
|
|
|
|
|
373
|
|
|
return $this->getHttpClient()->post( |
|
374
|
|
|
$this->getEndpoint(), |
|
375
|
|
|
$this->singleParameters, |
|
376
|
|
|
$this->compoundParametersCollections, |
|
377
|
|
|
$this->isAsyncRequest |
|
378
|
|
|
); |
|
379
|
|
|
} |
|
380
|
|
|
|
|
381
|
|
|
/** |
|
382
|
|
|
* Validates the minimum required parameters for every GA hit are being sent. |
|
383
|
|
|
* |
|
384
|
|
|
* @return bool |
|
385
|
|
|
*/ |
|
386
|
|
|
private function hasMinimumRequiredParameters() |
|
387
|
|
|
{ |
|
388
|
|
|
$minimumRequiredParameters = [ |
|
389
|
|
|
'v' => false, |
|
390
|
|
|
'tid' => false, |
|
391
|
|
|
'cid' => false, |
|
392
|
|
|
't' => false, |
|
393
|
|
|
]; |
|
394
|
|
|
|
|
395
|
|
|
foreach ($minimumRequiredParameters as $parameterName => $isParamPresent) { |
|
396
|
|
|
if (in_array($parameterName, array_keys($this->singleParameters))) { |
|
397
|
|
|
$minimumRequiredParameters[$parameterName] = true; |
|
398
|
|
|
} |
|
399
|
|
|
} |
|
400
|
|
|
|
|
401
|
|
|
return !in_array(false, $minimumRequiredParameters, true); |
|
402
|
|
|
} |
|
403
|
|
|
|
|
404
|
|
|
/** |
|
405
|
|
|
* Sets a parameter action to the value specified by the method call. |
|
406
|
|
|
* |
|
407
|
|
|
* @param $parameter |
|
408
|
|
|
* @param $action |
|
409
|
|
|
* @return $this |
|
410
|
|
|
*/ |
|
411
|
|
|
private function setParameterActionTo($parameter, $action) |
|
412
|
|
|
{ |
|
413
|
|
|
$actionConstant = $this->getParameterClassConstant( |
|
414
|
|
|
'TheIconic\Tracking\GoogleAnalytics\Parameters\EnhancedEcommerce\\' |
|
415
|
|
|
. $parameter . 'Action::ACTION_' . strtoupper($action), |
|
416
|
|
|
$parameter . ' action ' . $action . ' does not exist, check spelling' |
|
417
|
|
|
); |
|
418
|
|
|
|
|
419
|
|
|
$function = 'set' . $parameter . 'Action'; |
|
420
|
|
|
|
|
421
|
|
|
$this->$function($actionConstant); |
|
422
|
|
|
|
|
423
|
|
|
return $this; |
|
424
|
|
|
} |
|
425
|
|
|
|
|
426
|
|
|
/** |
|
427
|
|
|
* Gets a contant from a class dynamically. |
|
428
|
|
|
* |
|
429
|
|
|
* @param $constant |
|
430
|
|
|
* @param $exceptionMsg |
|
431
|
|
|
* @return mixed |
|
432
|
|
|
* @throws \BadMethodCallException |
|
433
|
|
|
*/ |
|
434
|
|
|
private function getParameterClassConstant($constant, $exceptionMsg) |
|
435
|
|
|
{ |
|
436
|
|
|
if (defined($constant)) { |
|
437
|
|
|
return constant($constant); |
|
438
|
|
|
} else { |
|
439
|
|
|
throw new \BadMethodCallException($exceptionMsg); |
|
440
|
|
|
} |
|
441
|
|
|
} |
|
442
|
|
|
|
|
443
|
|
|
/** |
|
444
|
|
|
* Sets the value for a parameter. |
|
445
|
|
|
* |
|
446
|
|
|
* @param $methodName |
|
447
|
|
|
* @param array $methodArguments |
|
448
|
|
|
* @return $this |
|
449
|
|
|
* @throws \InvalidArgumentException |
|
450
|
|
|
*/ |
|
451
|
|
|
private function setParameter($methodName, array $methodArguments) |
|
452
|
|
|
{ |
|
453
|
|
|
$parameterClass = substr($methodName, 3); |
|
454
|
|
|
|
|
455
|
|
|
$fullParameterClass = $this->getFullParameterClass($parameterClass, $methodName); |
|
456
|
|
|
|
|
457
|
|
|
$parameterIndex = $this->getIndexFromArguments($methodArguments); |
|
458
|
|
|
|
|
459
|
|
|
/** @var SingleParameter $parameterObject */ |
|
460
|
|
|
$parameterObject = new $fullParameterClass($parameterIndex); |
|
461
|
|
|
|
|
462
|
|
|
if (!isset($methodArguments[0])) { |
|
463
|
|
|
throw new \InvalidArgumentException( |
|
464
|
|
|
'For Analytics object, you must specify a value to be set for ' . $methodName |
|
465
|
|
|
); |
|
466
|
|
|
} else { |
|
467
|
|
|
$parameterObject->setValue($methodArguments[0]); |
|
468
|
|
|
} |
|
469
|
|
|
|
|
470
|
|
|
$this->singleParameters[$parameterObject->getName()] = $parameterObject; |
|
471
|
|
|
|
|
472
|
|
|
return $this; |
|
473
|
|
|
} |
|
474
|
|
|
|
|
475
|
|
|
/** |
|
476
|
|
|
* Adds an item to a compund parameter collection. |
|
477
|
|
|
* |
|
478
|
|
|
* @param $methodName |
|
479
|
|
|
* @param array $methodArguments |
|
480
|
|
|
* @return $this |
|
481
|
|
|
* @throws \InvalidArgumentException |
|
482
|
|
|
*/ |
|
483
|
|
|
private function addItem($methodName, array $methodArguments) |
|
484
|
|
|
{ |
|
485
|
|
|
$parameterClass = substr($methodName, 3); |
|
486
|
|
|
|
|
487
|
|
|
$fullParameterClass = $this->getFullParameterClass($parameterClass, $methodName); |
|
488
|
|
|
|
|
489
|
|
|
if (!isset($methodArguments[0])) { |
|
490
|
|
|
throw new \InvalidArgumentException( |
|
491
|
|
|
'You must specify a ' . $parameterClass . ' to be add for ' . $methodName |
|
492
|
|
|
); |
|
493
|
|
|
} else { |
|
494
|
|
|
$parameterObject = new $fullParameterClass($methodArguments[0]); |
|
495
|
|
|
} |
|
496
|
|
|
|
|
497
|
|
|
$collectionIndex = $this->getIndexFromArguments($methodArguments); |
|
498
|
|
|
|
|
499
|
|
|
if (isset($this->compoundParametersCollections[$parameterClass . $collectionIndex])) { |
|
500
|
|
|
$this->compoundParametersCollections[$parameterClass . $collectionIndex]->add($parameterObject); |
|
501
|
|
|
} else { |
|
502
|
|
|
$fullParameterCollectionClass = $fullParameterClass . 'Collection'; |
|
503
|
|
|
|
|
504
|
|
|
/** @var CompoundParameterCollection $parameterObjectCollection */ |
|
505
|
|
|
$parameterObjectCollection = new $fullParameterCollectionClass($collectionIndex); |
|
506
|
|
|
|
|
507
|
|
|
$parameterObjectCollection->add($parameterObject); |
|
508
|
|
|
|
|
509
|
|
|
$this->compoundParametersCollections[$parameterClass . $collectionIndex] = $parameterObjectCollection; |
|
510
|
|
|
} |
|
511
|
|
|
|
|
512
|
|
|
return $this; |
|
513
|
|
|
} |
|
514
|
|
|
|
|
515
|
|
|
/** |
|
516
|
|
|
* Gets the index value from the arguments. |
|
517
|
|
|
* |
|
518
|
|
|
* @param $methodArguments |
|
519
|
|
|
* @return string |
|
520
|
|
|
*/ |
|
521
|
|
|
private function getIndexFromArguments($methodArguments) |
|
522
|
|
|
{ |
|
523
|
|
|
$index = ''; |
|
524
|
|
|
if (isset($methodArguments[1]) && is_numeric($methodArguments[1])) { |
|
525
|
|
|
$index = $methodArguments[1]; |
|
526
|
|
|
} |
|
527
|
|
|
|
|
528
|
|
|
return $index; |
|
529
|
|
|
} |
|
530
|
|
|
|
|
531
|
|
|
/** |
|
532
|
|
|
* Gets the fully qualified name for a parameter. |
|
533
|
|
|
* |
|
534
|
|
|
* @param $parameterClass |
|
535
|
|
|
* @param $methodName |
|
536
|
|
|
* @return string |
|
537
|
|
|
* @throws \BadMethodCallException |
|
538
|
|
|
*/ |
|
539
|
|
|
private function getFullParameterClass($parameterClass, $methodName) |
|
540
|
|
|
{ |
|
541
|
|
|
if (empty($this->availableParameters[$parameterClass])) { |
|
542
|
|
|
throw new \BadMethodCallException('Method ' . $methodName . ' not defined for Analytics class'); |
|
543
|
|
|
} else { |
|
544
|
|
|
return '\\TheIconic\\Tracking\\GoogleAnalytics\\Parameters\\' . $this->availableParameters[$parameterClass]; |
|
545
|
|
|
} |
|
546
|
|
|
} |
|
547
|
|
|
|
|
548
|
|
|
/** |
|
549
|
|
|
* Routes the method call to the adequate private method. |
|
550
|
|
|
* |
|
551
|
|
|
* @param $methodName |
|
552
|
|
|
* @param array $methodArguments |
|
553
|
|
|
* @return $this|AnalyticsResponse |
|
554
|
|
|
* @throws \BadMethodCallException |
|
555
|
|
|
*/ |
|
556
|
|
|
public function __call($methodName, array $methodArguments) |
|
557
|
|
|
{ |
|
558
|
|
|
if (preg_match('/^set(Product|Promotion)ActionTo(\w+)/', $methodName, $matches)) { |
|
559
|
|
|
return $this->setParameterActionTo($matches[1], $matches[2]); |
|
560
|
|
|
} |
|
561
|
|
|
|
|
562
|
|
|
if (preg_match('/^(set)(\w+)/', $methodName, $matches)) { |
|
563
|
|
|
return $this->setParameter($methodName, $methodArguments); |
|
564
|
|
|
} |
|
565
|
|
|
|
|
566
|
|
|
if (preg_match('/^(add)(\w+)/', $methodName, $matches)) { |
|
567
|
|
|
return $this->addItem($methodName, $methodArguments); |
|
568
|
|
|
} |
|
569
|
|
|
|
|
570
|
|
|
if (preg_match('/^(send)(\w+)/', $methodName, $matches)) { |
|
571
|
|
|
return $this->sendHit($methodName); |
|
572
|
|
|
} |
|
573
|
|
|
|
|
574
|
|
|
throw new \BadMethodCallException('Method ' . $methodName . ' not defined for Analytics class'); |
|
575
|
|
|
} |
|
576
|
|
|
} |
|
577
|
|
|
|