Skip to content

Commit

Permalink
add user ip override and user agent override plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
moriony committed Sep 29, 2014
1 parent 15393a1 commit 66960dd
Show file tree
Hide file tree
Showing 4 changed files with 162 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Moriony\Google\Analytics\MeasurementProtocol\Plugin;

use Krizon\Google\Analytics\MeasurementProtocol\MeasurementProtocolClient;
use Moriony\Google\Analytics\MeasurementProtocol\Hit\HitInterface;

class UserAgentOverrider implements PluginInterface
{
/**
* @param MeasurementProtocolClient $client
*/
public function register($client)
{
$client->getEventDispatcher()->addListener('command.before_prepare', function ($e) {
/** @var \Guzzle\Service\Command\OperationCommand $command */
$command = $e['command'];
if (!isset($_SERVER['HTTP_USER_AGENT'])) {
return;
}
if ($command->hasKey(HitInterface::FIELD_USER_AGENT)) {
return;
}
$command->set(HitInterface::FIELD_USER_AGENT, $_SERVER['HTTP_USER_AGENT']);
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Moriony\Google\Analytics\MeasurementProtocol\Plugin;

use Krizon\Google\Analytics\MeasurementProtocol\MeasurementProtocolClient;
use Moriony\Google\Analytics\MeasurementProtocol\Hit\HitInterface;

class UserIpOverrider implements PluginInterface
{
/**
* @param MeasurementProtocolClient $client
*/
public function register($client)
{
$client->getEventDispatcher()->addListener('command.before_prepare', function ($e) {
/** @var \Guzzle\Service\Command\OperationCommand $command */
$command = $e['command'];
if (!isset($_SERVER['REMOTE_ADDR'])) {
return;
}
if ($command->hasKey(HitInterface::FIELD_USER_IP)) {
return;
}
$command->set(HitInterface::FIELD_USER_IP, $_SERVER['REMOTE_ADDR']);
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?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 UserAgentOverriderTest extends GuzzleTestCase
{
public function setUp()
{
$_SERVER['HTTP_USER_AGENT'] = null;
}

public function testSuccessfulUserAgentOverride()
{
$_SERVER['HTTP_USER_AGENT'] = 'Opera/9.80 (Windows NT 6.0) Presto/2.12.388 Version/12.14';
$command = new OperationCommand();
$this->dispatchCommand($command);
$this->assertSame('Opera/9.80 (Windows NT 6.0) Presto/2.12.388 Version/12.14', $command->get('ua'));
}

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

public function testAlreadyOverriddenUserAgent()
{
$_SERVER['HTTP_USER_AGENT'] = 'Opera/9.80 (Windows NT 6.0) Presto/2.12.388 Version/12.14';
$command = new OperationCommand();
$command->set('ua', 'some_value');
$this->dispatchCommand($command);
$this->assertSame('some_value', $command->get('ua'));
}

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

public function tearDown()
{
$_SERVER['HTTP_USER_AGENT'] = null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?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 UserIpOverriderTest extends GuzzleTestCase
{
public function setUp()
{
$_SERVER['REMOTE_ADDR'] = null;
}

public function testSuccessfulUserAgentOverride()
{
$_SERVER['REMOTE_ADDR'] = '1.2.3.4';
$command = new OperationCommand();
$this->dispatchCommand($command);
$this->assertSame('1.2.3.4', $command->get('uip'));
}

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

public function testAlreadyOverriddenUserAgent()
{
$_SERVER['REMOTE_ADDR'] = '1.2.3.4';
$command = new OperationCommand();
$command->set('uip', '4.3.2.1');
$this->dispatchCommand($command);
$this->assertSame('4.3.2.1', $command->get('uip'));
}

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

public function tearDown()
{
$_SERVER['REMOTE_ADDR'] = null;
}
}

0 comments on commit 66960dd

Please sign in to comment.