Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EZP-23825: integrated the tracker via a Response Event #2

Merged
merged 6 commits into from Jan 5, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion DependencyInjection/Configuration.php
@@ -1,5 +1,10 @@
<?php

/**
* This file is part of the EzSystemsMarketingAutomationBundle package
*
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
namespace EzSystems\MarketingAutomationBundle\DependencyInjection;

use Symfony\Component\Config\Definition\Builder\TreeBuilder;
Expand Down
@@ -1,5 +1,10 @@
<?php

/**
* This file is part of the EzSystemsMarketingAutomationBundle package
*
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
namespace EzSystems\MarketingAutomationBundle\DependencyInjection;

use Symfony\Component\DependencyInjection\ContainerBuilder;
Expand Down
39 changes: 39 additions & 0 deletions DependencyInjection/Factory/ConfigResolverAwareFactory.php
@@ -0,0 +1,39 @@
<?php
/**
* This file is part of the EzSystemsMarketingAutomationBundle package
*
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
namespace EzSystems\MarketingAutomationBundle\DependencyInjection\Factory;

use eZ\Publish\Core\MVC\ConfigResolverInterface;
use EzSystems\MarketingAutomationBundle\EventListener\TrackerListener;

/**
* Factory for services that depend on a legacy configuration variable
*/
class ConfigResolverAwareFactory
{
/** @var \eZ\Publish\Core\MVC\ConfigResolverInterface $configResolver */
protected $configResolver;

/**
* @param \eZ\Publish\Core\MVC\ConfigResolverInterface $configResolver
*/
public function __construct( ConfigResolverInterface $configResolver )
{
$this->configResolver = $configResolver;
}

/**
* @return \EzSystems\MarketingAutomationBundle\EventListener\TrackerListener
*/
public function buildTrackerListener()
{
return new TrackerListener(
$this->configResolver->getParameter( 'TrackingSettings.AutomaticTracking', 'ezma' ) === 'enabled',
$this->configResolver->getParameter( 'TrackingSettings.InstallationId', 'ezma' )
);
}
}
64 changes: 64 additions & 0 deletions EventListener/TrackerListener.php
@@ -0,0 +1,64 @@
<?php
/**
* This file is part of the EzSystemsMarketingAutomationBundle package
*
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
namespace EzSystems\MarketingAutomationBundle\EventListener;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;

/**
* Listens for the Kernel Response Event, and adds, if applicable, the Marketing Automation tracker before the closing
* body HTML tag.
*/
class TrackerListener implements EventSubscriberInterface
{
/** @var string */
private $installationId;

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

/**
* @param bool $isEnabled
* @param string $installationId Marketing Automation installation ID
*/
public function __construct( $isEnabled, $installationId )
{
$this->installationId = $installationId;
$this->isEnabled = $isEnabled;
}

public static function getSubscribedEvents()
{
return array(
KernelEvents::RESPONSE => array( 'onKernelResponse' )
);
}

public function onKernelResponse(FilterResponseEvent $e)
{
if ( !$this->isEnabled ) {
return;
}

$scriptCode = <<<EOT
<script id="__maSrc" type="text/javascript" data-pid="{$this->installationId}">
(function () {
var d=document,t='script',c=d.createElement(t),s=(d.URL.indexOf('https:')==0?'s':''),p;
c.type = 'text/java'+t;
c.src = 'http'+s+'://'+s+'c.cdnma.com/apps/capture.js';
p=d.getElementsByTagName(t)[0];p.parentNode.insertBefore(c,p);
}());
</script>
EOT;
$e->getResponse()->setContent(
str_ireplace( '</body>', $scriptCode . '</body>', $e->getResponse()->getContent() )
);
}

}
5 changes: 5 additions & 0 deletions README.md
Expand Up @@ -28,3 +28,8 @@ See the LICENSE file that ships at the root of this bundle.
2. run `composer update ezsystems/marketing-automation-bundle`.

3. Add `new EzSystems\MarketingAutomationBundle\EzSystemsMarketingAutomationBundle()` to your kernel (`ezpublish/EzPublishKernel.php`).

## Features

### Automatic tracker code integration
The Marketing Automation tracking code will automatically be added before the closing body tag.
13 changes: 10 additions & 3 deletions Resources/config/services.yml
@@ -1,4 +1,11 @@
services:
# ez_systems_marketing_automation.example:
# class: EzSystems\MarketingAutomationBundle\Example
# arguments: [@service_id, "plain_value", %parameter%]
ez_marketing_automation.tracker_listener:
class: EzSystems\MarketingAutomationBundle\EventListener\TrackerListener
factory_service: ez_marketing_automation.legacy_config_resolver_aware_factory
factory_method: buildTrackerListener
tags:
- { name: kernel.event_subscriber }

ez_marketing_automation.legacy_config_resolver_aware_factory:
class: EzSystems\MarketingAutomationBundle\DependencyInjection\Factory\ConfigResolverAwareFactory
arguments: [@ezpublish_legacy.config.resolver]
@@ -0,0 +1,34 @@
<?php
/**
* This file is part of the EzSystemsMarketingAutomationBundle package
*
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
namespace EzSystems\MarketingAutomationBundle\Tests\DependencyInjection\Factory;

use EzSystems\MarketingAutomationBundle\DependencyInjection\Factory\ConfigResolverAwareFactory;
use PHPUnit_Framework_TestCase;

class ConfigResolverAwareFactoryTest extends PHPUnit_Framework_TestCase
{
/** @var \eZ\Publish\Core\MVC\ConfigResolverInterface|\PHPUnit_Framework_MockObject_MockObject $legacyConfigResolver */
private $configResolver;

/** @var \EzSystems\MarketingAutomationBundle\DependencyInjection\Factory\LegacyConfigurationAwareFactory $factory */
private $factory;

public function setUp()
{
$this->configResolver = $this->getMock('eZ\Publish\Core\MVC\ConfigResolverInterface');
$this->factory = new ConfigResolverAwareFactory($this->configResolver);
}

public function testBuildTrackerListener()
{
self::assertInstanceOf(
'EzSystems\MarketingAutomationBundle\EventListener\TrackerListener',
$this->factory->buildTrackerListener()
);
}
}
68 changes: 68 additions & 0 deletions Tests/EventListener/TrackerListenerTest.php
@@ -0,0 +1,68 @@
<?php
/**
* This file is part of the EzSystemsMarketingAutomationBundle package
*
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
namespace EzSystems\MarketingAutomationBundle\Tests\EventListener;

use EzSystems\MarketingAutomationBundle\EventListener\TrackerListener;
use PHPUnit_Framework_TestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;

class TrackerListenerTest extends PHPUnit_Framework_TestCase
{
const INSTALLATION_ID = __CLASS__;

/** @var \EzSystems\MarketingAutomationBundle\EventListener\TrackerListener $listener */
private $listener;

public function setUp()
{
$this->listener = new TrackerListener( self::INSTALLATION_ID );
}

public function testOnKernelResponse()
{
$response = new Response( '<html><body></body></html>');
$e = $this->createFilterResponseEvent($response);
$this->listener->onKernelResponse($e);

self::assertContains(
sprintf('data-pid="%s"', self::INSTALLATION_ID),
$response->getContent()
);
}

public function testOnKernelResponseNoBodyTag()
{
$response = new Response( '<div>');
$e = $this->createFilterResponseEvent($response);
$this->listener->onKernelResponse($e);

self::assertNotContains(
sprintf('data-pid="%s"', self::INSTALLATION_ID),
$response->getContent()
);
}

/**
* @param \Symfony\Component\HttpFoundation\Response $response
* @param int $requestType HttpKernelInterface::MASTER_REQUEST or HttpKernelInterface::SUB_REQUEST
*
* @return \Symfony\Component\HttpKernel\Event\FilterResponseEvent
*/
protected function createFilterResponseEvent( $response, $requestType = HttpKernelInterface::MASTER_REQUEST )
{
return new FilterResponseEvent(
$this->getMock( 'Symfony\Component\HttpKernel\HttpKernelInterface' ),
new Request(),
$requestType,
$response
);
}
}