Skip to content

Commit

Permalink
add cookie parser plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
moriony committed Sep 29, 2014
1 parent fa6f0e5 commit 0dd3f7e
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 1 deletion.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"minimum-stability": "dev",
"require": {
"krizon/php-ga-measurement-protocol": "0.1.*",
"guzzle/guzzle": "~3.7"
"guzzle/guzzle": "~3.7",
"waarneembemiddeling/php-google-analytics-cookie-parser": "0.1.*"
},
"require-dev": {
"phpunit/phpunit": ">=3.7.28",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace Moriony\Google\Analytics\MeasurementProtocol\Plugin;

use Krizon\Google\Analytics\MeasurementProtocol\MeasurementProtocolClient;
use Moriony\Google\Analytics\MeasurementProtocol\Hit\HitInterface;
use Wb\GoogleAnalyticsCookieParser\GoogleAnalyticsCookieParser as Parser;

class GoogleAnalyticsCookieParser implements PluginInterface
{
const DEFAULT_COOKIE_NAME = '_ga';

protected $cookieName;

public function __construct($cookieName = self::DEFAULT_COOKIE_NAME)
{
$this->cookieName = $cookieName;
}

/**
* @param MeasurementProtocolClient $client
*/
public function register($client)
{
$client->getEventDispatcher()->addListener('command.before_prepare', function ($e) {
if (!array_key_exists($this->cookieName, $_COOKIE)) {
return;
}
$parser = new Parser();
$data = $parser->parse($_COOKIE[$this->cookieName]);

/** @var \Guzzle\Service\Command\OperationCommand $command */
$command = $e['command'];
if (!$command->hasKey(HitInterface::FIELD_CUSTOMER_ID)) {
$command->set(HitInterface::FIELD_CUSTOMER_ID, $data->getClientId());
}
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Moriony\Google\Analytics\MeasurementProtocol\Plugin;

use Guzzle\Tests\GuzzleTestCase;
use Krizon\Google\Analytics\MeasurementProtocol\MeasurementProtocolClient;
use Guzzle\Service\Command\OperationCommand;
use Guzzle\Common\Event;

class GoogleAnalyticsCookieParserTest extends GuzzleTestCase
{
public function setUp()
{
$_COOKIE = array();
}

public function testSuccessfulCustomerIdSet()
{
$_COOKIE['_ga'] = 'GA1.2.230657868.1384941727';
$command = new OperationCommand();
$this->dispatchCommand($command);
$this->assertSame('230657868', $command->get('cid'));
}

public function testNotExistedCookieParsing()
{
$command = new OperationCommand();
$this->dispatchCommand($command);
$this->assertSame(null, $command->get('cid'));
}

protected function dispatchCommand(OperationCommand $command)
{
$client = MeasurementProtocolClient::factory();
$plugin = new GoogleAnalyticsCookieParser();
$event = new Event(array('command' => $command));
$plugin->register($client);
$client->getEventDispatcher()->dispatch('command.before_prepare', $event);
}
}

0 comments on commit 0dd3f7e

Please sign in to comment.