-
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.
add user ip override and user agent override plugins
- Loading branch information
Showing
4 changed files
with
162 additions
and
0 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
src/Moriony/Google/Analytics/MeasurementProtocol/Plugin/UserAgentOverrider.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,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']); | ||
}); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
src/Moriony/Google/Analytics/MeasurementProtocol/Plugin/UserIpOverrider.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,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']); | ||
}); | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
tests/Moriony/Google/Analytics/MeasurementProtocol/Plugin/UserAgentOverriderTest.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,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; | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
tests/Moriony/Google/Analytics/MeasurementProtocol/Plugin/UserIpOverriderTest.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,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; | ||
} | ||
} |