Skip to content

Commit 6393d54

Browse files
committed
Add missing event
1 parent c05122c commit 6393d54

File tree

7 files changed

+160
-69
lines changed

7 files changed

+160
-69
lines changed

Config/config.xml

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
<tag name="hook.event_listener" event="product.bottom" method="onProductBottom"/>
2020
<argument type="service" id="googletamanager_service"/>
2121
<argument type="service" id="event_dispatcher"/>
22+
<argument type="service" id="thelia.taxEngine"/>
23+
<argument type="service" id="request_stack"/>
2224
</hook>
2325
<hook id="googletagmanager.configuration.hook" class="GoogleTagManager\Hook\BackHook" scope="request">
2426
<tag name="hook.event_listener" event="module.configuration" type="back" method="onModuleConfiguration"/>

Config/module.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
<language>en_US</language>
1414
<language>fr_FR</language>
1515
</languages>
16-
<version>3.1.0</version>
16+
<version>3.1.1</version>
1717
<authors>
1818
<author>
1919
<name>Tom Pradat</name>

GoogleTagManager.php

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class GoogleTagManager extends BaseModule
2222
/** @var string */
2323
const DOMAIN_NAME = 'googletagmanager';
2424

25+
const GOOGLE_TAG_VIEW_CART = 'google_tag_view_cart';
2526
const GOOGLE_TAG_VIEW_ITEM = 'google_tag_view_item';
2627
const GOOGLE_TAG_VIEW_LIST_ITEM = 'google_tag_view_list_item';
2728
const GOOGLE_TAG_TRIGGER_LOGIN = 'google_tag_trigger_login';

Hook/FrontHook.php

+52-23
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,13 @@
1616
use GoogleTagManager\GoogleTagManager;
1717
use GoogleTagManager\Service\GoogleTagService;
1818
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
19+
use Symfony\Component\HttpFoundation\RequestStack;
1920
use Thelia\Core\Event\Hook\HookRenderEvent;
2021
use Thelia\Core\Hook\BaseHook;
22+
use Thelia\Core\HttpFoundation\Session\Session;
23+
use Thelia\Model\AddressQuery;
2124
use Thelia\Model\LangQuery;
25+
use Thelia\TaxEngine\TaxEngine;
2226

2327
/**
2428
* Class FrontHook
@@ -27,16 +31,27 @@
2731
*/
2832
class FrontHook extends BaseHook
2933
{
30-
public function __construct(private GoogleTagService $googleTagService, EventDispatcherInterface $eventDispatcher)
34+
public function __construct(
35+
private GoogleTagService $googleTagService,
36+
private EventDispatcherInterface $eventDispatcher,
37+
private TaxEngine $taxEngine,
38+
private RequestStack $requestStack
39+
)
3140
{
3241
parent::__construct(null, null, $eventDispatcher);
3342
}
3443

35-
public function onMainHeadTop(HookRenderEvent $event){
36-
$lang = $this->getLang();
44+
public function onMainHeadTop(HookRenderEvent $event)
45+
{
46+
$request = $this->requestStack->getCurrentRequest();
47+
48+
/** @var Session $session */
49+
$session = $request->getSession();
50+
3751
$gtmId = GoogleTagManager::getConfigValue('googletagmanager_gtmId');
38-
if ("" != $gtmId){
39-
$view = $this->request->get('_view');
52+
53+
if ("" != $gtmId) {
54+
$view = $request->get('_view');
4055

4156
$event->add($this->render('datalayer/thelia-page-view.html', ['data' => $this->googleTagService->getTheliaPageViewParameters()]));
4257

@@ -48,64 +63,78 @@ public function onMainHeadTop(HookRenderEvent $event){
4863
$event->add($this->render('datalayer/view-item.html', ['eventName' => 'view_item']));
4964
}
5065

51-
if (null !== $authAction = $this->request->getSession()->get(GoogleTagManager::GOOGLE_TAG_TRIGGER_LOGIN)){
66+
if (null !== $authAction = $session->get(GoogleTagManager::GOOGLE_TAG_TRIGGER_LOGIN)) {
5267
$event->add($this->render('datalayer/thelia-page-view.html', [
5368
'data' => $this->googleTagService->getLogInData($authAction)
5469
]));
55-
$this->request->getSession()->set(GoogleTagManager::GOOGLE_TAG_TRIGGER_LOGIN, null);
70+
$session->set(GoogleTagManager::GOOGLE_TAG_TRIGGER_LOGIN, null);
71+
}
72+
73+
if ($view === 'order-delivery') {
74+
/** @var Session $session */
75+
$session = $request->getSession();
76+
$cart = $session->getSessionCart($this->eventDispatcher);
77+
78+
$event->add($this->render('datalayer/thelia-page-view.html', [
79+
'data' => $this->googleTagService->getCartData($cart->getId(), $this->taxEngine->getDeliveryCountry())
80+
]));
81+
82+
$event->add($this->render('datalayer/thelia-page-view.html', [
83+
'data' => $this->googleTagService->getCheckOutData($cart->getId(), $this->taxEngine->getDeliveryCountry())
84+
]));
5685
}
5786

5887
if ($view === 'order-placed') {
5988
$event->add($this->render('datalayer/thelia-page-view.html', [
60-
'data' => $this->googleTagService->getPurchaseData($this->request->get('order_id'))
89+
'data' => $this->googleTagService->getPurchaseData($session->get('order_id'))
6190
]));
6291
}
6392

6493
$event->add(
65-
"<!-- Google Tag Manager -->".
66-
"<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':".
67-
"new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],".
68-
"j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=".
69-
"'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);".
70-
"})(window,document,'script','dataLayer','".$gtmId."');</script>".
94+
"<!-- Google Tag Manager -->" .
95+
"<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':" .
96+
"new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0]," .
97+
"j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=" .
98+
"'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);" .
99+
"})(window,document,'script','dataLayer','" . $gtmId . "');</script>" .
71100
"<!-- End Google Tag Manager -->"
72101
);
73102
}
74103
}
75104

76105
public function onMainBodyTop(HookRenderEvent $event)
77106
{
78-
$lang = $this->getLang();
79-
$value = GoogleTagManager::getConfigValue('googletagmanager_gtmId');
80-
if ("" != $value){
81-
$event->add("<!-- Google Tag Manager (noscript) -->".
82-
"<noscript><iframe src='https://www.googletagmanager.com/ns.html?id=".$value."' ".
83-
"height='0' width='0' style='display:none;visibility:hidden'></iframe></noscript>".
107+
if (!$value = GoogleTagManager::getConfigValue('googletagmanager_gtmId')) {
108+
$event->add("<!-- Google Tag Manager (noscript) -->" .
109+
"<noscript><iframe src='https://www.googletagmanager.com/ns.html?id=" . $value . "' " .
110+
"height='0' width='0' style='display:none;visibility:hidden'></iframe></noscript>" .
84111
"<!-- End Google Tag Manager (noscript) -->"
85112
);
86113
}
87114
}
88115

89116
public function onMainJsInit(HookRenderEvent $event)
90117
{
91-
$view = $this->request->get('_view');
118+
$view = $this->requestStack->getCurrentRequest()->get('_view');
92119

93120
if (in_array($view, ['category', 'brand', 'search'])) {
94121
$event->add($this->render('datalayer/select-item.html'));
95122
}
123+
124+
//include event listener to handle add to cart event (check README)
96125
$event->add($this->render('datalayer/add-to-cart.html'));
97126
}
98127

99128
public function onProductBottom(HookRenderEvent $event)
100129
{
101130
$productId = $event->getArgument('product');
102-
$this->request->getSession()->set(GoogleTagManager::GOOGLE_TAG_VIEW_ITEM, $productId);
131+
$this->requestStack->getCurrentRequest()->getSession()->set(GoogleTagManager::GOOGLE_TAG_VIEW_ITEM, $productId);
103132
}
104133

105134
protected function getLang()
106135
{
107136
$lang = $this->getRequest()->getSession()->get("thelia.current.lang");
108-
if (null === $lang){
137+
if (null === $lang) {
109138
$lang = LangQuery::create()->filterByByDefault(1)->findOne();
110139
}
111140
return $lang;

Listener/GoogleTagListener.php

+9-19
Original file line numberDiff line numberDiff line change
@@ -7,41 +7,31 @@
77
use ShortCode\Event\ShortCodeEvent;
88
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
99
use Symfony\Component\HttpFoundation\RequestStack;
10-
use Symfony\Component\Security\Http\Event\LoginSuccessEvent;
11-
use Symfony\Contracts\EventDispatcher\Event;
12-
use Thelia\Core\Event\ActionEvent;
1310
use Thelia\Core\Event\Customer\CustomerCreateOrUpdateEvent;
1411
use Thelia\Core\Event\Customer\CustomerLoginEvent;
1512
use Thelia\Core\Event\Loop\LoopExtendsParseResultsEvent;
1613
use Thelia\Core\Event\TheliaEvents;
17-
use Thelia\Core\HttpFoundation\Session\Session;
18-
use Thelia\Model\Category;
19-
use Thelia\Model\CategoryQuery;
20-
use Thelia\Model\ConfigQuery;
21-
use Thelia\Model\Currency;
22-
use Thelia\Model\Lang;
23-
use Thelia\Model\Product;
24-
use Thelia\Model\ProductQuery;
25-
use Thelia\TaxEngine\Calculator;
2614

2715
class GoogleTagListener implements EventSubscriberInterface
2816
{
2917
public function __construct(
3018
private GoogleTagService $googleTagService,
31-
private RequestStack $requestStack
32-
) {}
19+
private RequestStack $requestStack
20+
)
21+
{
22+
}
3323

3424
public static function getSubscribedEvents()
3525
{
3626
return [
3727
GoogleTagManager::GOOGLE_TAG_VIEW_LIST_ITEM => ['getViewListItem', 128],
3828
GoogleTagManager::GOOGLE_TAG_VIEW_ITEM => ['getViewItem', 128],
29+
TheliaEvents::CUSTOMER_LOGIN => ['triggerLoginEvent', 128],
30+
TheliaEvents::CUSTOMER_CREATEACCOUNT => ['triggerRegisterEvent', 128],
3931
TheliaEvents::getLoopExtendsEvent(
4032
TheliaEvents::LOOP_EXTENDS_PARSE_RESULTS,
4133
'product'
42-
) => ['trackProducts', 128],
43-
TheliaEvents::CUSTOMER_LOGIN => ['triggerLoginEvent', 128],
44-
TheliaEvents::CUSTOMER_CREATEACCOUNT => ['triggerRegisterEvent', 128],
34+
) => ['trackProducts', 128]
4535
];
4636
}
4737

@@ -91,12 +81,12 @@ public function trackProducts(LoopExtendsParseResultsEvent $event)
9181
$request = $this->requestStack->getCurrentRequest();
9282
$session = $request->getSession();
9383

94-
if (!in_array($request->get('_view'), ['product', 'category', 'brand', 'search'])){
84+
if (!in_array($request->get('_view'), ['product', 'category', 'brand', 'search'])) {
9585
$session->set(GoogleTagManager::GOOGLE_TAG_VIEW_LIST_ITEM, null);
9686
return;
9787
}
9888

99-
foreach ($event->getLoopResult() as $product){
89+
foreach ($event->getLoopResult() as $product) {
10090
$products[] = $product->get('ID');
10191
}
10292

0 commit comments

Comments
 (0)