22
33namespace TheIconic \Tracking \GoogleAnalytics ;
44
5- use TheIconic \Tracking \GoogleAnalytics \Parameters \SingleParameter ;
6- use TheIconic \Tracking \GoogleAnalytics \Parameters \CompoundParameterCollection ;
5+ use BadMethodCallException ;
6+ use TheIconic \Tracking \GoogleAnalytics \Exception \EnqueueUrlsOverflowException ;
7+ use TheIconic \Tracking \GoogleAnalytics \Exception \InvalidPayloadDataException ;
78use TheIconic \Tracking \GoogleAnalytics \Network \HttpClient ;
89use TheIconic \Tracking \GoogleAnalytics \Network \PrepareUrl ;
9- use TheIconic \Tracking \GoogleAnalytics \Exception \InvalidPayloadDataException ;
10+ use TheIconic \Tracking \GoogleAnalytics \Parameters \CompoundParameterCollection ;
11+ use TheIconic \Tracking \GoogleAnalytics \Parameters \SingleParameter ;
1012
1113/**
1214 * Class Analytics
@@ -319,6 +321,14 @@ class Analytics
319321 */
320322 protected $ debugEndpoint = '://www.google-analytics.com/debug/collect ' ;
321323
324+ /**
325+ * Endpoint to connect to when sending batch data to GA.
326+ *
327+ * @var string
328+ */
329+ protected $ batchEndpoint = '://www.google-analytics.com/batch ' ;
330+
331+
322332 /**
323333 * Indicates if the request is in debug mode(validating hits).
324334 *
@@ -354,6 +364,11 @@ class Analytics
354364 */
355365 protected $ isDisabled = false ;
356366
367+ /**
368+ * @var array
369+ */
370+ protected $ enqueuedUrls = [];
371+
357372 /**
358373 * @var array
359374 */
@@ -553,6 +568,16 @@ protected function getEndpoint()
553568 return ($ this ->isDebug ) ? $ this ->uriScheme . $ this ->debugEndpoint : $ this ->uriScheme . $ this ->endpoint ;
554569 }
555570
571+ /**
572+ * Gets the full batch endpoint to GA.
573+ *
574+ * @return string
575+ */
576+ protected function getBatchEndpoint ()
577+ {
578+ return $ this ->uriScheme . $ this ->batchEndpoint ;
579+ }
580+
556581 /**
557582 * Sets debug mode to true or false.
558583 *
@@ -578,6 +603,47 @@ protected function sendHit($methodName)
578603 {
579604 $ hitType = strtoupper (substr ($ methodName , 4 ));
580605
606+ $ this ->setAndValidateHit ($ hitType );
607+
608+ if ($ this ->isDisabled ) {
609+ return new NullAnalyticsResponse ();
610+ }
611+
612+ return $ this ->getHttpClient ()->post ($ this ->getUrl (), $ this ->getHttpClientOptions ());
613+ }
614+
615+ /**
616+ * Enqueue a hit to GA. The hit will contain in the payload all the parameters added before.
617+ *
618+ * @param $methodName
619+ * @return $this
620+ * @throws Exception\InvalidPayloadDataException
621+ */
622+ protected function enqueueHit ($ methodName )
623+ {
624+
625+ if (count ($ this ->enqueuedUrls ) == 20 ) {
626+ throw new EnqueueUrlsOverflowException ();
627+ }
628+
629+ $ hitType = strtoupper (substr ($ methodName , 7 ));
630+
631+ $ this ->setAndValidateHit ($ hitType );
632+ $ this ->enqueuedUrls [] = $ this ->getUrl (true );
633+
634+ return $ this ;
635+ }
636+
637+ /**
638+ * Validate and set hitType
639+ *
640+ * @param $methodName
641+ * @return void
642+ * @throws Exception\InvalidPayloadDataException
643+ */
644+ protected function setAndValidateHit ($ hitType )
645+ {
646+
581647 $ hitConstant = $ this ->getParameterClassConstant (
582648 'TheIconic\Tracking\GoogleAnalytics\Parameters\Hit\HitType::HIT_TYPE_ ' . $ hitType ,
583649 'Hit type ' . $ hitType . ' is not defined, check spelling '
@@ -588,12 +654,24 @@ protected function sendHit($methodName)
588654 if (!$ this ->hasMinimumRequiredParameters ()) {
589655 throw new InvalidPayloadDataException ();
590656 }
657+ }
591658
659+ /**
660+ * Sends enqueued hits to GA. These hits will contain in the payload all the parameters added before.
661+ *
662+ * @return AnalyticsResponseInterface
663+ */
664+ public function sendEnqueuedHits ()
665+ {
592666 if ($ this ->isDisabled ) {
593667 return new NullAnalyticsResponse ();
594668 }
595669
596- return $ this ->getHttpClient ()->post ($ this ->getUrl (), $ this ->getHttpClientOptions ());
670+ $ response = $ this ->getHttpClient ()->batch ($ this ->getBatchEndpoint (), $ this ->enqueuedUrls , $ this ->getHttpClientOptions ());
671+
672+ $ this ->emptyQueue ();
673+
674+ return $ response ;
597675 }
598676
599677 /**
@@ -618,14 +696,15 @@ protected function getHttpClientOptions()
618696 * @api
619697 * @return string
620698 */
621- public function getUrl ()
699+ public function getUrl ($ onlyQuery = false )
622700 {
623701 $ prepareUrl = new PrepareUrl ;
624702
625703 return $ prepareUrl ->build (
626704 $ this ->getEndpoint (),
627705 $ this ->singleParameters ,
628- $ this ->compoundParametersCollections
706+ $ this ->compoundParametersCollections ,
707+ $ onlyQuery
629708 );
630709 }
631710
@@ -691,14 +770,14 @@ protected function setParameterActionTo($parameter, $action)
691770 * @param $constant
692771 * @param $exceptionMsg
693772 * @return mixed
694- * @throws \ BadMethodCallException
773+ * @throws BadMethodCallException
695774 */
696775 protected function getParameterClassConstant ($ constant , $ exceptionMsg )
697776 {
698777 if (defined ($ constant )) {
699778 return constant ($ constant );
700779 } else {
701- throw new \ BadMethodCallException ($ exceptionMsg );
780+ throw new BadMethodCallException ($ exceptionMsg );
702781 }
703782 }
704783
@@ -760,8 +839,9 @@ protected function addItem($methodName, array $methodArguments)
760839
761840 $ collectionIndex = $ this ->getIndexFromArguments ($ methodArguments );
762841
763- if (isset ($ this ->compoundParametersCollections [$ parameterClass . $ collectionIndex ])) {
764- $ this ->compoundParametersCollections [$ parameterClass . $ collectionIndex ]->add ($ parameterObject );
842+ $ parameterIndex = $ parameterClass . $ collectionIndex ;
843+ if (isset ($ this ->compoundParametersCollections [$ parameterIndex ])) {
844+ $ this ->compoundParametersCollections [$ parameterIndex ]->add ($ parameterObject );
765845 } else {
766846 $ fullParameterCollectionClass = $ fullParameterClass . 'Collection ' ;
767847
@@ -770,7 +850,7 @@ protected function addItem($methodName, array $methodArguments)
770850
771851 $ parameterObjectCollection ->add ($ parameterObject );
772852
773- $ this ->compoundParametersCollections [$ parameterClass . $ collectionIndex ] = $ parameterObjectCollection ;
853+ $ this ->compoundParametersCollections [$ parameterIndex ] = $ parameterObjectCollection ;
774854 }
775855
776856 return $ this ;
@@ -847,15 +927,27 @@ protected function getIndexFromArguments($methodArguments)
847927 * @param $parameterClass
848928 * @param $methodName
849929 * @return string
850- * @throws \ BadMethodCallException
930+ * @throws BadMethodCallException
851931 */
852932 protected function getFullParameterClass ($ parameterClass , $ methodName )
853933 {
854934 if (empty ($ this ->availableParameters [$ parameterClass ])) {
855- throw new \BadMethodCallException ('Method ' . $ methodName . ' not defined for Analytics class ' );
856- } else {
857- return '\\TheIconic \\Tracking \\GoogleAnalytics \\Parameters \\' . $ this ->availableParameters [$ parameterClass ];
935+ throw new BadMethodCallException ('Method ' . $ methodName . ' not defined for Analytics class ' );
858936 }
937+
938+ return '\\TheIconic \\Tracking \\GoogleAnalytics \\Parameters \\' . $ this ->availableParameters [$ parameterClass ];
939+ }
940+
941+ /**
942+ * Empty batch queue
943+ *
944+ * @return $this
945+ */
946+ public function emptyQueue ()
947+ {
948+ $ this ->enqueuedUrls = [];
949+
950+ return $ this ;
859951 }
860952
861953 /**
@@ -864,7 +956,7 @@ protected function getFullParameterClass($parameterClass, $methodName)
864956 * @param $methodName
865957 * @param array $methodArguments
866958 * @return mixed
867- * @throws \ BadMethodCallException
959+ * @throws BadMethodCallException
868960 */
869961 public function __call ($ methodName , array $ methodArguments )
870962 {
@@ -886,12 +978,16 @@ public function __call($methodName, array $methodArguments)
886978 return $ this ->sendHit ($ methodName );
887979 }
888980
981+ if (preg_match ('/^(enqueue)(\w+)/ ' , $ methodName , $ matches )) {
982+ return $ this ->enqueueHit ($ methodName );
983+ }
984+
889985 // Get Parameters
890986 if (preg_match ('/^(get)(\w+)/ ' , $ methodName , $ matches )) {
891987 return $ this ->getParameter ($ methodName , $ methodArguments );
892988 }
893989
894- throw new \ BadMethodCallException ('Method ' . $ methodName . ' not defined for Analytics class ' );
990+ throw new BadMethodCallException ('Method ' . $ methodName . ' not defined for Analytics class ' );
895991 }
896992
897993 /**
0 commit comments