-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
81 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
src/Moriony/Google/Analytics/MeasurementProtocol/Plugin/GoogleAnalyticsCookieParser.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
}); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
...s/Moriony/Google/Analytics/MeasurementProtocol/Plugin/GoogleAnalyticsCookieParserTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |