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 | * Holds the single parameters added to the hit. |
||
183 | * |
||
184 | * @var SingleParameter[] |
||
185 | */ |
||
186 | private $singleParameters = []; |
||
187 | |||
188 | /** |
||
189 | * Holds the compound parameters collections added to the hit. |
||
190 | * |
||
191 | * @var CompoundParameterCollection[] |
||
192 | */ |
||
193 | private $compoundParametersCollections = []; |
||
194 | |||
195 | /** |
||
196 | * Holds the HTTP client used to connect to GA. |
||
197 | * |
||
198 | * @var HttpClient |
||
199 | */ |
||
200 | private $httpClient; |
||
201 | |||
202 | /** |
||
203 | * Initializes to a list of all the available parameters to be sent in a hit. |
||
204 | * |
||
205 | * @var array |
||
206 | */ |
||
207 | private $availableParameters = [ |
||
208 | 'ApplicationId' => 'AppTracking\\ApplicationId', |
||
209 | 'ApplicationInstallerId' => 'AppTracking\\ApplicationInstallerId', |
||
210 | 'ApplicationName' => 'AppTracking\\ApplicationName', |
||
211 | 'ApplicationVersion' => 'AppTracking\\ApplicationVersion', |
||
212 | 'ExperimentId' => 'ContentExperiments\\ExperimentId', |
||
213 | 'ExperimentVariant' => 'ContentExperiments\\ExperimentVariant', |
||
214 | 'ContentGroup' => 'ContentGrouping\\ContentGroup', |
||
215 | 'DocumentHostName' => 'ContentInformation\\DocumentHostName', |
||
216 | 'DocumentLocationUrl' => 'ContentInformation\\DocumentLocationUrl', |
||
217 | 'DocumentPath' => 'ContentInformation\\DocumentPath', |
||
218 | 'DocumentTitle' => 'ContentInformation\\DocumentTitle', |
||
219 | 'LinkId' => 'ContentInformation\\LinkId', |
||
220 | 'ScreenName' => 'ContentInformation\\ScreenName', |
||
221 | 'CustomDimension' => 'CustomDimensionsMetrics\\CustomDimension', |
||
222 | 'CustomMetric' => 'CustomDimensionsMetrics\\CustomMetric', |
||
223 | 'CurrencyCode' => 'Ecommerce\\CurrencyCode', |
||
224 | 'ItemCategory' => 'Ecommerce\\ItemCategory', |
||
225 | 'ItemCode' => 'Ecommerce\\ItemCode', |
||
226 | 'ItemName' => 'Ecommerce\\ItemName', |
||
227 | 'ItemPrice' => 'Ecommerce\\ItemPrice', |
||
228 | 'ItemQuantity' => 'Ecommerce\\ItemQuantity', |
||
229 | 'Affiliation' => 'EnhancedEcommerce\\Affiliation', |
||
230 | 'CheckoutStep' => 'EnhancedEcommerce\\CheckoutStep', |
||
231 | 'CheckoutStepOption' => 'EnhancedEcommerce\\CheckoutStepOption', |
||
232 | 'CouponCode' => 'EnhancedEcommerce\\CouponCode', |
||
233 | 'Product' => 'EnhancedEcommerce\\Product', |
||
234 | 'ProductAction' => 'EnhancedEcommerce\\ProductAction', |
||
235 | 'ProductActionList' => 'EnhancedEcommerce\\ProductActionList', |
||
236 | 'ProductCollection' => 'EnhancedEcommerce\\ProductCollection', |
||
237 | 'ProductImpression' => 'EnhancedEcommerce\\ProductImpression', |
||
238 | 'ProductImpressionCollection' => 'EnhancedEcommerce\\ProductImpressionCollection', |
||
239 | 'ProductImpressionListName' => 'EnhancedEcommerce\\ProductImpressionListName', |
||
240 | 'Promotion' => 'EnhancedEcommerce\\Promotion', |
||
241 | 'PromotionAction' => 'EnhancedEcommerce\\PromotionAction', |
||
242 | 'PromotionCollection' => 'EnhancedEcommerce\\PromotionCollection', |
||
243 | 'Revenue' => 'EnhancedEcommerce\\Revenue', |
||
244 | 'Shipping' => 'EnhancedEcommerce\\Shipping', |
||
245 | 'Tax' => 'EnhancedEcommerce\\Tax', |
||
246 | 'TransactionId' => 'EnhancedEcommerce\\TransactionId', |
||
247 | 'EventAction' => 'Event\\EventAction', |
||
248 | 'EventCategory' => 'Event\\EventCategory', |
||
249 | 'EventLabel' => 'Event\\EventLabel', |
||
250 | 'EventValue' => 'Event\\EventValue', |
||
251 | 'ExceptionDescription' => 'Exceptions\\ExceptionDescription', |
||
252 | 'IsExceptionFatal' => 'Exceptions\\IsExceptionFatal', |
||
253 | 'AnonymizeIp' => 'General\\AnonymizeIp', |
||
254 | 'CacheBuster' => 'General\\CacheBuster', |
||
255 | 'DataSource' => 'General\\DataSource', |
||
256 | 'ProtocolVersion' => 'General\\ProtocolVersion', |
||
257 | 'QueueTime' => 'General\\QueueTime', |
||
258 | 'TrackingId' => 'General\\TrackingId', |
||
259 | 'HitType' => 'Hit\\HitType', |
||
260 | 'NonInteractionHit' => 'Hit\\NonInteractionHit', |
||
261 | 'GeographicalOverride' => 'Session\\GeographicalOverride', |
||
262 | 'IpOverride' => 'Session\\IpOverride', |
||
263 | 'SessionControl' => 'Session\\SessionControl', |
||
264 | 'UserAgentOverride' => 'Session\\UserAgentOverride', |
||
265 | 'SocialAction' => 'SocialInteractions\\SocialAction', |
||
266 | 'SocialActionTarget' => 'SocialInteractions\\SocialActionTarget', |
||
267 | 'SocialNetwork' => 'SocialInteractions\\SocialNetwork', |
||
268 | 'DocumentEncoding' => 'SystemInfo\\DocumentEncoding', |
||
269 | 'FlashVersion' => 'SystemInfo\\FlashVersion', |
||
270 | 'JavaEnabled' => 'SystemInfo\\JavaEnabled', |
||
271 | 'ScreenColors' => 'SystemInfo\\ScreenColors', |
||
272 | 'ScreenResolution' => 'SystemInfo\\ScreenResolution', |
||
273 | 'UserLanguage' => 'SystemInfo\\UserLanguage', |
||
274 | 'ViewportSize' => 'SystemInfo\\ViewportSize', |
||
275 | 'ContentLoadTime' => 'Timing\\ContentLoadTime', |
||
276 | 'DnsTime' => 'Timing\\DnsTime', |
||
277 | 'DomInteractiveTime' => 'Timing\\DomInteractiveTime', |
||
278 | 'PageDownloadTime' => 'Timing\\PageDownloadTime', |
||
279 | 'PageLoadTime' => 'Timing\\PageLoadTime', |
||
280 | 'RedirectResponseTime' => 'Timing\\RedirectResponseTime', |
||
281 | 'ServerResponseTime' => 'Timing\\ServerResponseTime', |
||
282 | 'TcpConnectTime' => 'Timing\\TcpConnectTime', |
||
283 | 'UserTimingCategory' => 'Timing\\UserTimingCategory', |
||
284 | 'UserTimingLabel' => 'Timing\\UserTimingLabel', |
||
285 | 'UserTimingTime' => 'Timing\\UserTimingTime', |
||
286 | 'UserTimingVariableName' => 'Timing\\UserTimingVariableName', |
||
287 | 'CampaignContent' => 'TrafficSources\\CampaignContent', |
||
288 | 'CampaignId' => 'TrafficSources\\CampaignId', |
||
289 | 'CampaignKeyword' => 'TrafficSources\\CampaignKeyword', |
||
290 | 'CampaignMedium' => 'TrafficSources\\CampaignMedium', |
||
291 | 'CampaignName' => 'TrafficSources\\CampaignName', |
||
292 | 'CampaignSource' => 'TrafficSources\\CampaignSource', |
||
293 | 'DocumentReferrer' => 'TrafficSources\\DocumentReferrer', |
||
294 | 'GoogleAdwordsId' => 'TrafficSources\\GoogleAdwordsId', |
||
295 | 'GoogleDisplayAdsId' => 'TrafficSources\\GoogleDisplayAdsId', |
||
296 | 'ClientId' => 'User\\ClientId', |
||
297 | 'UserId' => 'User\\UserId', |
||
298 | ]; |
||
299 | |||
300 | /** |
||
301 | * When passed with an argument of TRUE, it will send the hit using HTTPS instead of plain HTTP. |
||
302 | * It parses the available parameters. |
||
303 | * |
||
304 | * @param bool $isSsl |
||
305 | * @throws \InvalidArgumentException |
||
306 | */ |
||
307 | public function __construct($isSsl = false) |
||
318 | |||
319 | /** |
||
320 | * Sets a request to be either synchronous or asynchronous (non-blocking). |
||
321 | * |
||
322 | * @param boolean $isAsyncRequest |
||
323 | * @return $this |
||
324 | */ |
||
325 | public function setAsyncRequest($isAsyncRequest) |
||
331 | |||
332 | /** |
||
333 | * Makes the request to GA asynchronous (non-blocking). |
||
334 | * |
||
335 | * @deprecated Use setAsyncRequest(boolean $isAsyncRequest) instead. To be removed in next major version. |
||
336 | * |
||
337 | * @return $this |
||
338 | */ |
||
339 | public function makeNonBlocking() |
||
345 | |||
346 | /** |
||
347 | * Sets the HttpClient. |
||
348 | * |
||
349 | * @param HttpClient $httpClient |
||
350 | * @return $this |
||
351 | */ |
||
352 | public function setHttpClient(HttpClient $httpClient) |
||
358 | |||
359 | /** |
||
360 | * Gets the HttpClient. |
||
361 | * |
||
362 | * @return HttpClient |
||
363 | */ |
||
364 | private function getHttpClient() |
||
374 | |||
375 | /** |
||
376 | * Gets the full endpoint to GA. |
||
377 | * |
||
378 | * @return string |
||
379 | */ |
||
380 | private function getEndpoint() |
||
384 | |||
385 | /** |
||
386 | * Sends a hit to GA. The hit will contain in the payload all the parameters added before. |
||
387 | * |
||
388 | * @param $methodName |
||
389 | * @return AnalyticsResponse |
||
390 | * @throws Exception\InvalidPayloadDataException |
||
391 | */ |
||
392 | private function sendHit($methodName) |
||
414 | |||
415 | /** |
||
416 | * Validates the minimum required parameters for every GA hit are being sent. |
||
417 | * |
||
418 | * @SuppressWarnings(PHPMD.LongVariable) |
||
419 | * |
||
420 | * @return bool |
||
421 | */ |
||
422 | private function hasMinimumRequiredParameters() |
||
439 | |||
440 | /** |
||
441 | * Sets a parameter action to the value specified by the method call. |
||
442 | * |
||
443 | * @param $parameter |
||
444 | * @param $action |
||
445 | * @return $this |
||
446 | */ |
||
447 | private function setParameterActionTo($parameter, $action) |
||
461 | |||
462 | /** |
||
463 | * Gets a contant from a class dynamically. |
||
464 | * |
||
465 | * @param $constant |
||
466 | * @param $exceptionMsg |
||
467 | * @return mixed |
||
468 | * @throws \BadMethodCallException |
||
469 | */ |
||
470 | private function getParameterClassConstant($constant, $exceptionMsg) |
||
478 | |||
479 | /** |
||
480 | * Sets the value for a parameter. |
||
481 | * |
||
482 | * @param $methodName |
||
483 | * @param array $methodArguments |
||
484 | * @return $this |
||
485 | * @throws \InvalidArgumentException |
||
486 | */ |
||
487 | private function setParameter($methodName, array $methodArguments) |
||
510 | |||
511 | /** |
||
512 | * Adds an item to a compund parameter collection. |
||
513 | * |
||
514 | * @SuppressWarnings(PHPMD.LongVariable) |
||
515 | * |
||
516 | * @param $methodName |
||
517 | * @param array $methodArguments |
||
518 | * @return $this |
||
519 | * @throws \InvalidArgumentException |
||
520 | */ |
||
521 | private function addItem($methodName, array $methodArguments) |
||
552 | |||
553 | /** |
||
554 | * Gets the index value from the arguments. |
||
555 | * |
||
556 | * @param $methodArguments |
||
557 | * @return string |
||
558 | */ |
||
559 | private function getIndexFromArguments($methodArguments) |
||
568 | |||
569 | /** |
||
570 | * Gets the fully qualified name for a parameter. |
||
571 | * |
||
572 | * @param $parameterClass |
||
573 | * @param $methodName |
||
574 | * @return string |
||
575 | * @throws \BadMethodCallException |
||
576 | */ |
||
577 | private function getFullParameterClass($parameterClass, $methodName) |
||
585 | |||
586 | /** |
||
587 | * Routes the method call to the adequate private method. |
||
588 | * |
||
589 | * @param $methodName |
||
590 | * @param array $methodArguments |
||
591 | * @return $this|AnalyticsResponse |
||
592 | * @throws \BadMethodCallException |
||
593 | */ |
||
594 | public function __call($methodName, array $methodArguments) |
||
616 | |||
617 | /** |
||
618 | * Fix typos that went into releases, this way we ensure we don't break scripts in production. |
||
619 | * |
||
620 | * @param string $methodName |
||
621 | * @return string |
||
622 | */ |
||
623 | private function fixTypos($methodName) |
||
632 | } |
||
633 |