-
Notifications
You must be signed in to change notification settings - Fork 2
/
TrackerListener.php
64 lines (56 loc) · 1.88 KB
/
TrackerListener.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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() )
);
}
}