1 | <?php |
||
158 | class Analytics |
||
159 | { |
||
160 | /** |
||
161 | * URI scheme for the GA API. |
||
162 | * |
||
163 | * @var string |
||
164 | */ |
||
165 | private $uriScheme = 'http'; |
||
166 | |||
167 | /** |
||
168 | * Indicates if the request to GA will be asynchronous (non-blocking). |
||
169 | * |
||
170 | * @var boolean |
||
171 | */ |
||
172 | private $isAsyncRequest = false; |
||
173 | |||
174 | /** |
||
175 | * Endpoint to connect to when sending data to GA. |
||
176 | * |
||
177 | * @var string |
||
178 | */ |
||
179 | private $endpoint = '://www.google-analytics.com/collect'; |
||
180 | |||
181 | /** |
||
182 | * Endpoint to connect to when validating hits. |
||
183 | * @link https://developers.google.com/analytics/devguides/collection/protocol/v1/validating-hits |
||
184 | * |
||
185 | * @var string |
||
186 | */ |
||
187 | private $debugEndpoint = '://www.google-analytics.com/debug/collect'; |
||
188 | |||
189 | /** |
||
190 | * Indicates if the request is in debug mode(validating hits). |
||
191 | * |
||
192 | * @var boolean |
||
193 | */ |
||
194 | private $isDebug = false; |
||
195 | |||
196 | /** |
||
197 | * Holds the single parameters added to the hit. |
||
198 | * |
||
199 | * @var SingleParameter[] |
||
200 | */ |
||
201 | private $singleParameters = []; |
||
202 | |||
203 | /** |
||
204 | * Holds the compound parameters collections added to the hit. |
||
205 | * |
||
206 | * @var CompoundParameterCollection[] |
||
207 | */ |
||
208 | private $compoundParametersCollections = []; |
||
209 | |||
210 | /** |
||
211 | * Holds the HTTP client used to connect to GA. |
||
212 | * |
||
213 | * @var HttpClient |
||
214 | */ |
||
215 | private $httpClient; |
||
216 | |||
217 | /** |
||
218 | * Initializes to a list of all the available parameters to be sent in a hit. |
||
219 | * |
||
220 | * @var array |
||
221 | */ |
||
222 | private $availableParameters = [ |
||
223 | 'ApplicationId' => 'AppTracking\\ApplicationId', |
||
224 | 'ApplicationInstallerId' => 'AppTracking\\ApplicationInstallerId', |
||
225 | 'ApplicationName' => 'AppTracking\\ApplicationName', |
||
226 | 'ApplicationVersion' => 'AppTracking\\ApplicationVersion', |
||
227 | 'ExperimentId' => 'ContentExperiments\\ExperimentId', |
||
228 | 'ExperimentVariant' => 'ContentExperiments\\ExperimentVariant', |
||
229 | 'ContentGroup' => 'ContentGrouping\\ContentGroup', |
||
230 | 'DocumentHostName' => 'ContentInformation\\DocumentHostName', |
||
231 | 'DocumentLocationUrl' => 'ContentInformation\\DocumentLocationUrl', |
||
232 | 'DocumentPath' => 'ContentInformation\\DocumentPath', |
||
233 | 'DocumentTitle' => 'ContentInformation\\DocumentTitle', |
||
234 | 'LinkId' => 'ContentInformation\\LinkId', |
||
235 | 'ScreenName' => 'ContentInformation\\ScreenName', |
||
236 | 'CustomDimension' => 'CustomDimensionsMetrics\\CustomDimension', |
||
237 | 'CustomMetric' => 'CustomDimensionsMetrics\\CustomMetric', |
||
238 | 'CurrencyCode' => 'Ecommerce\\CurrencyCode', |
||
239 | 'ItemCategory' => 'Ecommerce\\ItemCategory', |
||
240 | 'ItemCode' => 'Ecommerce\\ItemCode', |
||
241 | 'ItemName' => 'Ecommerce\\ItemName', |
||
242 | 'ItemPrice' => 'Ecommerce\\ItemPrice', |
||
243 | 'ItemQuantity' => 'Ecommerce\\ItemQuantity', |
||
244 | 'Affiliation' => 'EnhancedEcommerce\\Affiliation', |
||
245 | 'CheckoutStep' => 'EnhancedEcommerce\\CheckoutStep', |
||
246 | 'CheckoutStepOption' => 'EnhancedEcommerce\\CheckoutStepOption', |
||
247 | 'CouponCode' => 'EnhancedEcommerce\\CouponCode', |
||
248 | 'Product' => 'EnhancedEcommerce\\Product', |
||
249 | 'ProductAction' => 'EnhancedEcommerce\\ProductAction', |
||
250 | 'ProductActionList' => 'EnhancedEcommerce\\ProductActionList', |
||
251 | 'ProductCollection' => 'EnhancedEcommerce\\ProductCollection', |
||
252 | 'ProductImpression' => 'EnhancedEcommerce\\ProductImpression', |
||
253 | 'ProductImpressionCollection' => 'EnhancedEcommerce\\ProductImpressionCollection', |
||
254 | 'ProductImpressionListName' => 'EnhancedEcommerce\\ProductImpressionListName', |
||
255 | 'Promotion' => 'EnhancedEcommerce\\Promotion', |
||
256 | 'PromotionAction' => 'EnhancedEcommerce\\PromotionAction', |
||
257 | 'PromotionCollection' => 'EnhancedEcommerce\\PromotionCollection', |
||
258 | 'Revenue' => 'EnhancedEcommerce\\Revenue', |
||
259 | 'Shipping' => 'EnhancedEcommerce\\Shipping', |
||
260 | 'Tax' => 'EnhancedEcommerce\\Tax', |
||
261 | 'TransactionId' => 'EnhancedEcommerce\\TransactionId', |
||
262 | 'EventAction' => 'Event\\EventAction', |
||
263 | 'EventCategory' => 'Event\\EventCategory', |
||
264 | 'EventLabel' => 'Event\\EventLabel', |
||
265 | 'EventValue' => 'Event\\EventValue', |
||
266 | 'ExceptionDescription' => 'Exceptions\\ExceptionDescription', |
||
267 | 'IsExceptionFatal' => 'Exceptions\\IsExceptionFatal', |
||
268 | 'AnonymizeIp' => 'General\\AnonymizeIp', |
||
269 | 'CacheBuster' => 'General\\CacheBuster', |
||
270 | 'DataSource' => 'General\\DataSource', |
||
271 | 'ProtocolVersion' => 'General\\ProtocolVersion', |
||
272 | 'QueueTime' => 'General\\QueueTime', |
||
273 | 'TrackingId' => 'General\\TrackingId', |
||
274 | 'HitType' => 'Hit\\HitType', |
||
275 | 'NonInteractionHit' => 'Hit\\NonInteractionHit', |
||
276 | 'GeographicalOverride' => 'Session\\GeographicalOverride', |
||
277 | 'IpOverride' => 'Session\\IpOverride', |
||
278 | 'SessionControl' => 'Session\\SessionControl', |
||
279 | 'UserAgentOverride' => 'Session\\UserAgentOverride', |
||
280 | 'SocialAction' => 'SocialInteractions\\SocialAction', |
||
281 | 'SocialActionTarget' => 'SocialInteractions\\SocialActionTarget', |
||
282 | 'SocialNetwork' => 'SocialInteractions\\SocialNetwork', |
||
283 | 'DocumentEncoding' => 'SystemInfo\\DocumentEncoding', |
||
284 | 'FlashVersion' => 'SystemInfo\\FlashVersion', |
||
285 | 'JavaEnabled' => 'SystemInfo\\JavaEnabled', |
||
286 | 'ScreenColors' => 'SystemInfo\\ScreenColors', |
||
287 | 'ScreenResolution' => 'SystemInfo\\ScreenResolution', |
||
288 | 'UserLanguage' => 'SystemInfo\\UserLanguage', |
||
289 | 'ViewportSize' => 'SystemInfo\\ViewportSize', |
||
290 | 'ContentLoadTime' => 'Timing\\ContentLoadTime', |
||
291 | 'DnsTime' => 'Timing\\DnsTime', |
||
292 | 'DomInteractiveTime' => 'Timing\\DomInteractiveTime', |
||
293 | 'PageDownloadTime' => 'Timing\\PageDownloadTime', |
||
294 | 'PageLoadTime' => 'Timing\\PageLoadTime', |
||
295 | 'RedirectResponseTime' => 'Timing\\RedirectResponseTime', |
||
296 | 'ServerResponseTime' => 'Timing\\ServerResponseTime', |
||
297 | 'TcpConnectTime' => 'Timing\\TcpConnectTime', |
||
298 | 'UserTimingCategory' => 'Timing\\UserTimingCategory', |
||
299 | 'UserTimingLabel' => 'Timing\\UserTimingLabel', |
||
300 | 'UserTimingTime' => 'Timing\\UserTimingTime', |
||
301 | 'UserTimingVariableName' => 'Timing\\UserTimingVariableName', |
||
302 | 'CampaignContent' => 'TrafficSources\\CampaignContent', |
||
303 | 'CampaignId' => 'TrafficSources\\CampaignId', |
||
304 | 'CampaignKeyword' => 'TrafficSources\\CampaignKeyword', |
||
305 | 'CampaignMedium' => 'TrafficSources\\CampaignMedium', |
||
306 | 'CampaignName' => 'TrafficSources\\CampaignName', |
||
307 | 'CampaignSource' => 'TrafficSources\\CampaignSource', |
||
308 | 'DocumentReferrer' => 'TrafficSources\\DocumentReferrer', |
||
309 | 'GoogleAdwordsId' => 'TrafficSources\\GoogleAdwordsId', |
||
310 | 'GoogleDisplayAdsId' => 'TrafficSources\\GoogleDisplayAdsId', |
||
311 | 'ClientId' => 'User\\ClientId', |
||
312 | 'UserId' => 'User\\UserId', |
||
313 | ]; |
||
314 | |||
315 | /** |
||
316 | * When passed with an argument of TRUE, it will send the hit using HTTPS instead of plain HTTP. |
||
317 | * It parses the available parameters. |
||
318 | * |
||
319 | * @param bool $isSsl |
||
320 | * @throws \InvalidArgumentException |
||
321 | */ |
||
322 | public function __construct($isSsl = false) |
||
333 | |||
334 | /** |
||
335 | * Sets a request to be either synchronous or asynchronous (non-blocking). |
||
336 | * |
||
337 | * @param boolean $isAsyncRequest |
||
338 | * @return $this |
||
339 | */ |
||
340 | public function setAsyncRequest($isAsyncRequest) |
||
346 | |||
347 | /** |
||
348 | * Makes the request to GA asynchronous (non-blocking). |
||
349 | * |
||
350 | * @deprecated Use setAsyncRequest(boolean $isAsyncRequest) instead. To be removed in next major version. |
||
351 | * |
||
352 | * @return $this |
||
353 | */ |
||
354 | public function makeNonBlocking() |
||
360 | |||
361 | /** |
||
362 | * Sets the HttpClient. |
||
363 | * |
||
364 | * @param HttpClient $httpClient |
||
365 | * @return $this |
||
366 | */ |
||
367 | public function setHttpClient(HttpClient $httpClient) |
||
373 | |||
374 | /** |
||
375 | * Gets the HttpClient. |
||
376 | * |
||
377 | * @return HttpClient |
||
378 | */ |
||
379 | private function getHttpClient() |
||
389 | |||
390 | /** |
||
391 | * Gets the full endpoint to GA. |
||
392 | * |
||
393 | * @return string |
||
394 | */ |
||
395 | private function getEndpoint() |
||
399 | |||
400 | /** |
||
401 | * Set debug mode true or false |
||
402 | * |
||
403 | */ |
||
404 | public function setDebug($value) { |
||
408 | |||
409 | /** |
||
410 | * Sends a hit to GA. The hit will contain in the payload all the parameters added before. |
||
411 | * |
||
412 | * @param $methodName |
||
413 | * @return AnalyticsResponse |
||
414 | * @throws Exception\InvalidPayloadDataException |
||
415 | */ |
||
416 | private function sendHit($methodName) |
||
438 | |||
439 | /** |
||
440 | * Validates the minimum required parameters for every GA hit are being sent. |
||
441 | * |
||
442 | * @SuppressWarnings(PHPMD.LongVariable) |
||
443 | * |
||
444 | * @return bool |
||
445 | */ |
||
446 | private function hasMinimumRequiredParameters() |
||
463 | |||
464 | /** |
||
465 | * Sets a parameter action to the value specified by the method call. |
||
466 | * |
||
467 | * @param $parameter |
||
468 | * @param $action |
||
469 | * @return $this |
||
470 | */ |
||
471 | private function setParameterActionTo($parameter, $action) |
||
485 | |||
486 | /** |
||
487 | * Gets a contant from a class dynamically. |
||
488 | * |
||
489 | * @param $constant |
||
490 | * @param $exceptionMsg |
||
491 | * @return mixed |
||
492 | * @throws \BadMethodCallException |
||
493 | */ |
||
494 | private function getParameterClassConstant($constant, $exceptionMsg) |
||
502 | |||
503 | /** |
||
504 | * Sets the value for a parameter. |
||
505 | * |
||
506 | * @param $methodName |
||
507 | * @param array $methodArguments |
||
508 | * @return $this |
||
509 | * @throws \InvalidArgumentException |
||
510 | */ |
||
511 | private function setParameter($methodName, array $methodArguments) |
||
534 | |||
535 | /** |
||
536 | * Adds an item to a compund parameter collection. |
||
537 | * |
||
538 | * @SuppressWarnings(PHPMD.LongVariable) |
||
539 | * |
||
540 | * @param $methodName |
||
541 | * @param array $methodArguments |
||
542 | * @return $this |
||
543 | * @throws \InvalidArgumentException |
||
544 | */ |
||
545 | private function addItem($methodName, array $methodArguments) |
||
576 | |||
577 | /** |
||
578 | * Gets the index value from the arguments. |
||
579 | * |
||
580 | * @param $methodArguments |
||
581 | * @return string |
||
582 | */ |
||
583 | private function getIndexFromArguments($methodArguments) |
||
592 | |||
593 | /** |
||
594 | * Gets the fully qualified name for a parameter. |
||
595 | * |
||
596 | * @param $parameterClass |
||
597 | * @param $methodName |
||
598 | * @return string |
||
599 | * @throws \BadMethodCallException |
||
600 | */ |
||
601 | private function getFullParameterClass($parameterClass, $methodName) |
||
609 | |||
610 | /** |
||
611 | * Routes the method call to the adequate private method. |
||
612 | * |
||
613 | * @param $methodName |
||
614 | * @param array $methodArguments |
||
615 | * @return $this|AnalyticsResponse |
||
616 | * @throws \BadMethodCallException |
||
617 | */ |
||
618 | public function __call($methodName, array $methodArguments) |
||
640 | |||
641 | /** |
||
642 | * Fix typos that went into releases, this way we ensure we don't break scripts in production. |
||
643 | * |
||
644 | * @param string $methodName |
||
645 | * @return string |
||
646 | */ |
||
647 | private function fixTypos($methodName) |
||
656 | } |
||
657 |