Skip to content

Commit

Permalink
Symfony4 compatibility (#18)
Browse files Browse the repository at this point in the history
* Updated the way Analytics are created, allowing AnalyticsFactory to be public and shared

* Fixed CS checks

* feat(symfony4): adds symfony 4 to composer compatibility list

* feat(symfony4): fixes invalid json

* feat(symfony4): adds required service alias
  • Loading branch information
Felix Imobersteg authored and foaly-nr1 committed Jul 9, 2018
1 parent e121425 commit 42d741c
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 44 deletions.
18 changes: 9 additions & 9 deletions Resources/config/services.xml
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<parameters>
<parameter key="gamp.analytics.class">FourLabs\GampBundle\Service\AnalyticsFactory</parameter>
</parameters>

<services>
<service id="gamp.analytics.factory" class="%gamp.analytics.class%" public="false" />

<service id="gamp.analytics" class="TheIconic\Tracking\GoogleAnalytics\Analytics" shared="false">
<factory service="gamp.analytics.factory" method="createAnalytics" />
<argument type="service" id="request_stack" />
<service id="gamp.analytics.factory" class="%gamp.analytics.class%">
<argument type="service" id="request_stack"/>
<argument>%gamp.protocol_version%</argument>
<argument>%gamp.tracking_id%</argument>
<argument>%gamp.use_ssl%</argument>
Expand All @@ -21,5 +16,10 @@
<argument>%kernel.debug%</argument>
<argument>%gamp.sandbox%</argument>
</service>
<service id="FourLabs\GampBundle\Service\AnalyticsFactory" alias="gamp.analytics.factory">
</service>
<service id="gamp.analytics" class="TheIconic\Tracking\GoogleAnalytics\Analytics" shared="false">
<factory service="gamp.analytics.factory" method="createAnalytics"/>
</service>
</services>
</container>
103 changes: 77 additions & 26 deletions Service/AnalyticsFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,39 +8,90 @@
class AnalyticsFactory
{
/**
* @param RequestStack $requestStack
* @param int $version
* @param string $trackingId
* @param bool $ssl
* @param bool $anonymize
* @param bool $async
* @param bool $debug
* @param bool $sandbox
*
* @var RequestStack
*/
private $requestStack;

/**
* @var int
*/
private $version;

/**
* @var string
*/
private $trackingId;

/**
* @var bool
*/
private $ssl;

/**
* @var bool
*/
private $anonymize;

/**
* @var bool
*/
private $async;

/**
* @var bool
*/
private $debug;

/**
* @var bool
*/
private $sandbox;

/**
* @param RequestStack $requestStack Request stack
* @param int $version GA Version
* @param string $trackingId GA tracking ID
* @param bool $ssl Use ssl
* @param bool $anonymize Anonymize IP
* @param bool $async Async calls
* @param bool $debug Enable debug
* @param bool $sandbox Sandbox
*/
public function __construct(RequestStack $requestStack, $version, $trackingId, $ssl, $anonymize, $async, $debug, $sandbox)
{
$this->requestStack = $requestStack;
$this->version = $version;
$this->trackingId = $trackingId;
$this->ssl = $ssl;
$this->anonymize = $anonymize;
$this->async = $async;
$this->debug = $debug;
$this->sandbox = $sandbox;
}

/**
* @return Analytics
*/
public function createAnalytics(RequestStack $requestStack, $version, $trackingId, $ssl, $anonymize, $async, $debug, $sandbox)
public function createAnalytics()
{
$analytics = new Analytics($ssl, $sandbox);
$analytics = new Analytics($this->ssl, $this->sandbox);

$analytics
->setProtocolVersion($version)
->setTrackingId($trackingId)
->setAnonymizeIp($anonymize)
->setAsyncRequest($async && !$debug)
->setDebug($debug)
;

if (($request = $requestStack->getCurrentRequest())) {
$userAgent = $request->headers->has('User-Agent') ? $request->headers->get('User-Agent') : '';
->setProtocolVersion($this->version)
->setTrackingId($this->trackingId)
->setAnonymizeIp($this->anonymize)
->setAsyncRequest($this->async && !$this->debug)
->setDebug($this->debug);

if (null !== $request = $this->requestStack->getCurrentRequest()) {
$analytics
->setIpOverride($request->getClientIp())
->setUserAgentOverride($userAgent)
;
->setUserAgentOverride($request->headers->get('User-Agent', ''));

// set clientId from ga cookie if exists, otherwise this must be set at a later point
if ($request->cookies->has('_ga')) {
$cookie = $this->parseCookie($request->cookies->get('_ga'));
// Set clientId from "_ga" cookie if exists,
// otherwise this must be set at a later point
if (null !== $ga = $request->cookies->get('_ga')) {
$cookie = $this->parseCookie($ga);
$analytics->setClientId(array_pop($cookie));
}
}
Expand All @@ -50,11 +101,11 @@ public function createAnalytics(RequestStack $requestStack, $version, $trackingI

/**
* Parse the GA Cookie and return data as an array.
* Example of GA cookie: _ga:GA1.2.492973748.1449824416.
*
* @param $cookie
*
* @return array(version, domainDepth, cid)
* Example of GA cookie: _ga:GA1.2.492973748.1449824416
*/
public function parseCookie($cookie)
{
Expand Down
16 changes: 8 additions & 8 deletions Tests/AnalyticsFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,22 @@ final class AnalyticsFactoryTest extends TestCase
protected $factory;

/**
* @var array
* @var RequestStack
*/
protected $parameters;
protected $requestStack;

protected function setUp()
{
$this->factory = new AnalyticsFactory();
$this->parameters = [new RequestStack(), 1, 'UA-XXXXXXXX-X', true, false, true, true, false];
$this->requestStack = new RequestStack();
$this->factory = new AnalyticsFactory($this->requestStack, 1, 'UA-XXXXXXXX-X', true, false, true, true, false);
}

/**
* @return Analytics
*/
private function callFactory()
{
return call_user_func_array([$this->factory, 'createAnalytics'], $this->parameters);
return $this->factory->createAnalytics();
}

public function testNoRequest()
Expand All @@ -43,7 +43,7 @@ public function testNoRequest()

public function testEmptyRequest()
{
$this->parameters[0]->push(Request::create(''));
$this->requestStack->push(Request::create(''));

$analytics = $this->callFactory();

Expand All @@ -61,7 +61,7 @@ public function testParseCookie()

public function testGaCookie()
{
$this->parameters[0]->push(Request::create('', 'GET', [], ['_ga' => 'GA1.2.1792370315.1501194811']));
$this->requestStack->push(Request::create('', 'GET', [], ['_ga' => 'GA1.2.1792370315.1501194811']));

$analytics = $this->callFactory();

Expand All @@ -75,7 +75,7 @@ public function testUserAgentOverride()

$request = Request::create('');
$request->headers->set('User-Agent', $userAgentString);
$this->parameters[0]->push($request);
$this->requestStack->push($request);

$analytics = $this->callFactory();

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"require": {
"php": ">=5.5",
"theiconic/php-ga-measurement-protocol": "^2.7",
"symfony/framework-bundle": "~2.4||~3.0.0||~3.1.0"
"symfony/framework-bundle": "~2.4||~3.0||~4.0"
},
"require-dev": {
"phpunit/phpunit": "4.8.*"
Expand Down

0 comments on commit 42d741c

Please sign in to comment.