diff --git a/src/Krizon/Google/Analytics/MeasurementProtocol/MeasurementProtocolClient.php b/src/Krizon/Google/Analytics/MeasurementProtocol/MeasurementProtocolClient.php index c315895..356641a 100644 --- a/src/Krizon/Google/Analytics/MeasurementProtocol/MeasurementProtocolClient.php +++ b/src/Krizon/Google/Analytics/MeasurementProtocol/MeasurementProtocolClient.php @@ -14,6 +14,7 @@ use Guzzle\Common\Collection; use Guzzle\Http\Message\Request; use Guzzle\Service\Client; +use Guzzle\Plugin\Async\AsyncPlugin; use Guzzle\Service\Description\ServiceDescription; class MeasurementProtocolClient extends Client @@ -22,6 +23,7 @@ public static function factory($config = array()) { $default = array( 'ssl' => false, + 'async' => false, 'tid' => null ); $required = array('ssl'); @@ -32,6 +34,10 @@ public static function factory($config = array()) $client = new self($baseUrl, $config); + if($config->get('async') === true) { + $client->addSubscriber(new AsyncPlugin()); + } + $description = ServiceDescription::factory(__DIR__ . '/Resources/service.php'); $client->setDescription($description); diff --git a/tests/Krizon/Google/Analytics/MeasurementProtocol/Test/MeasurementProtocolClientTest.php b/tests/Krizon/Google/Analytics/MeasurementProtocol/Test/MeasurementProtocolClientTest.php index d5fee8f..8cf69ba 100644 --- a/tests/Krizon/Google/Analytics/MeasurementProtocol/Test/MeasurementProtocolClientTest.php +++ b/tests/Krizon/Google/Analytics/MeasurementProtocol/Test/MeasurementProtocolClientTest.php @@ -12,6 +12,7 @@ namespace Krizon\Google\Analytics\MeasurementProtocol\Test; use Guzzle\Http\Message\Response; +use Guzzle\Plugin\Async\AsyncPlugin; use Guzzle\Plugin\History\HistoryPlugin; use Guzzle\Plugin\Mock\MockPlugin; use Guzzle\Service\Exception\ValidationException; @@ -42,6 +43,18 @@ public function testFactoryInitializesClientWithSsl() $this->assertTrue($client->getConfig('ssl')); } + public function testFactoryInitializesClientWithAsync() + { + $client = MeasurementProtocolClient::factory(array( + 'async' => true + )); + $this->assertEquals('http://www.google-analytics.com', $client->getBaseUrl()); + // We try to check if the is asynch plugin is correctly registered in the listeners list + $ed = $client-> getEventDispatcher()->getListeners(); + $classList = array_map(function($elt){return get_class($elt[0]);}, $ed['request.sent']); + $this->assertContains(get_class(new AsyncPlugin()), $classList); + } + public function testAbstractCollect() { $response = $this->getResponse('abstract.collect', array( @@ -223,6 +236,25 @@ public function testTrackingIdAsParamWins() $this->assertEquals('X3', $this->history->getLastRequest()->getQuery()->get('tid')); } + /** + * @group internet + */ + public function testAsyncRequestLive() + { + $client = MeasurementProtocolClient::factory(array( + 'async' => true + )); + $response = $this->getResponse('pageview', array( + 'tid' => $this->getTrackingId(), + 'cid' => $this->getCustomerId(), + 't' => 'pageview', + 'dh' => 'domain.do', + 'dp' => '/php-ga-measurement-protocol/phpunit-test/async', + 'dt' => 'PHP GA Measurement Protocol Async' + ), false, $client); + $this->assertEquals(200, $response->getStatusCode()); + } + /** * @param $operation * @param array $parameters