Skip to content

Commit e8bdf8c

Browse files
committed
Fix event
1 parent d5844e2 commit e8bdf8c

File tree

7 files changed

+130
-22
lines changed

7 files changed

+130
-22
lines changed

Config/config.xml

+5-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99

1010
<services>
1111
<service id="googletamanager.service" class="GoogleTagManager\Service\GoogleTagService">
12-
<argument type="service" id="request_stack"/>
12+
<argument type="service" id="request_stack"/>
13+
<argument type="service" id="thelia.taxEngine"/>
1314
</service>
1415

1516
<service id="googletamanager.tag.listener" class="GoogleTagManager\Listener\GoogleTagListener">
@@ -26,6 +27,9 @@
2627
<tag name="hook.event_listener" event="main.javascript-initialization" method="onMainJsInit"/>
2728
<tag name="hook.event_listener" event="product.bottom" method="onProductBottom"/>
2829
<argument type="service" id="googletamanager.service"/>
30+
<argument type="service" id="event_dispatcher"/>
31+
<argument type="service" id="thelia.taxEngine"/>
32+
<argument type="service" id="request_stack"/>
2933
</hook>
3034
<hook id="googletagmanager.configuration.hook" class="GoogleTagManager\Hook\BackHook" scope="request">
3135
<tag name="hook.event_listener" event="module.configuration" type="back" method="onModuleConfiguration"/>

Config/module.xml

+7-3
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,15 @@
1313
<language>en_US</language>
1414
<language>fr_FR</language>
1515
</languages>
16-
<version>2.2.1</version>
16+
<version>2.2.2</version>
1717
<authors>
1818
<author>
19-
<name>Tom Pradat</name>
20-
<email>tpradat@openstudio.fr</email>
19+
<name>Bertrand Tourlonias</name>
20+
<email>btourlonias@openstudio.fr</email>
21+
</author>
22+
<author>
23+
<name>Nicolas Barbey</name>
24+
<email>nbarbey@openstudio.fr</email>
2125
</author>
2226
</authors>
2327
<type>classic</type>

Controller/ProductDataController.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public function getProductDataWithUrl()
4747
$result = $googleTagService->getProductItem($product, $lang, $currency);
4848
}
4949

50-
return new JsonResponse(json_encode([$result]));
50+
return new JsonResponse(json_encode([$result], JSON_HEX_APOS));
5151
}
5252

5353
public function getCartItem()
@@ -80,6 +80,6 @@ public function getCartItem()
8080

8181
$result = $googleTagService->getProductItem($product, $lang, $currency, $pse, $quantity);
8282

83-
return new JsonResponse(json_encode([$result]));
83+
return new JsonResponse(json_encode([$result], JSON_HEX_APOS));
8484
}
8585
}

Hook/FrontHook.php

+36-7
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,13 @@
1515

1616
use GoogleTagManager\GoogleTagManager;
1717
use GoogleTagManager\Service\GoogleTagService;
18+
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
19+
use Symfony\Component\HttpFoundation\RequestStack;
1820
use Thelia\Core\Event\Hook\HookRenderEvent;
1921
use Thelia\Core\Hook\BaseHook;
22+
use Thelia\Core\HttpFoundation\Session\Session;
2023
use Thelia\Model\LangQuery;
24+
use Thelia\TaxEngine\TaxEngine;
2125

2226
/**
2327
* Class FrontHook
@@ -30,10 +34,21 @@ class FrontHook extends BaseHook
3034
* @var GoogleTagService
3135
*/
3236
private $googleTagService;
33-
34-
public function __construct(GoogleTagService $googleTagService)
37+
private $eventDispatcher;
38+
private $taxEngine;
39+
private $requestStack;
40+
41+
public function __construct(
42+
GoogleTagService $googleTagService,
43+
EventDispatcherInterface $eventDispatcher,
44+
TaxEngine $taxEngine,
45+
RequestStack $requestStack
46+
)
3547
{
3648
$this->googleTagService = $googleTagService;
49+
$this->eventDispatcher = $eventDispatcher;
50+
$this->taxEngine = $taxEngine;
51+
$this->requestStack = $requestStack;
3752
}
3853

3954
public function onMainHeadTop(HookRenderEvent $event)
@@ -58,9 +73,23 @@ public function onMainHeadTop(HookRenderEvent $event)
5873
$this->getRequest()->getSession()->set(GoogleTagManager::GOOGLE_TAG_TRIGGER_LOGIN, null);
5974
}
6075

61-
if ($view === 'order-placed') {
76+
if ($view === 'order-delivery') {
77+
/** @var Session $session */
78+
$session = $this->getRequest()->getSession();
79+
$cart = $session->getSessionCart($this->eventDispatcher);
80+
81+
$event->add($this->render('datalayer/thelia-page-view.html', [
82+
'data' => $this->googleTagService->getCartData($cart->getId(), $this->taxEngine->getDeliveryCountry())
83+
]));
84+
6285
$event->add($this->render('datalayer/thelia-page-view.html', [
63-
'data' => $this->googleTagService->getPurchaseData($this->getRequest()->get('order_id'))
86+
'data' => $this->googleTagService->getCheckOutData($cart->getId(), $this->taxEngine->getDeliveryCountry())
87+
]));
88+
}
89+
90+
if ($view === 'order-placed' && $orderId = $this->getRequest()->get('order_id')) {
91+
$event->add($this->render('datalayer/thelia-page-view.html', [
92+
'data' => $this->googleTagService->getPurchaseData($orderId)
6493
]));
6594
}
6695

@@ -78,9 +107,7 @@ public function onMainHeadTop(HookRenderEvent $event)
78107

79108
public function onMainBodyTop(HookRenderEvent $event)
80109
{
81-
$value = GoogleTagManager::getConfigValue('googletagmanager_gtmId');
82-
83-
if ("" != $value) {
110+
if (!$value = GoogleTagManager::getConfigValue('googletagmanager_gtmId')) {
84111
$event->add("<!-- Google Tag Manager (noscript) -->" .
85112
"<noscript><iframe src='https://www.googletagmanager.com/ns.html?id=" . $value . "' " .
86113
"height='0' width='0' style='display:none;visibility:hidden'></iframe></noscript>" .
@@ -96,6 +123,8 @@ public function onMainJsInit(HookRenderEvent $event)
96123
if (in_array($view, ['category', 'brand', 'search'])) {
97124
$event->add($this->render('datalayer/select-item.html'));
98125
}
126+
127+
//include event listener to handle add to cart event (check README)
99128
$event->add($this->render('datalayer/add-to-cart.html'));
100129
}
101130

Listener/GoogleTagListener.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public function getViewListItem(ShortCodeEvent $event)
6060
]
6161
];
6262

63-
$event->setResult(json_encode($result));
63+
$event->setResult(json_encode($result, JSON_HEX_APOS));
6464

6565
$session->set(GoogleTagManager::GOOGLE_TAG_VIEW_LIST_ITEM, null);
6666
}
@@ -80,7 +80,7 @@ public function getViewItem(ShortCodeEvent $event)
8080
]
8181
];
8282

83-
$event->setResult(json_encode($result));
83+
$event->setResult(json_encode($result, JSON_HEX_APOS));
8484

8585
$session->set(GoogleTagManager::GOOGLE_TAG_VIEW_ITEM, null);
8686
}

Service/GoogleTagService.php

+73-6
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Thelia\Model\Base\CurrencyQuery;
88
use Thelia\Model\BrandQuery;
99
use Thelia\Model\CartItem;
10+
use Thelia\Model\CartQuery;
1011
use Thelia\Model\Category;
1112
use Thelia\Model\CategoryQuery;
1213
use Thelia\Model\ConfigQuery;
@@ -22,17 +23,23 @@
2223
use Thelia\Model\ProductSaleElements;
2324
use Thelia\Model\ProductSaleElementsQuery;
2425
use Thelia\TaxEngine\Calculator;
26+
use Thelia\TaxEngine\TaxEngine;
2527

2628
class GoogleTagService
2729
{
2830
/**
2931
* @var RequestStack
3032
*/
3133
private $requestStack;
34+
/**
35+
* @var TaxEngine
36+
*/
37+
private $taxEngine;
3238

33-
public function __construct(RequestStack $requestStack)
39+
public function __construct(RequestStack $requestStack,TaxEngine $taxEngine)
3440
{
3541
$this->requestStack = $requestStack;
42+
$this->taxEngine = $taxEngine;
3643
}
3744

3845
public function getTheliaPageViewParameters()
@@ -64,15 +71,15 @@ public function getTheliaPageViewParameters()
6471
$result['google_tag_params']['ecomm_category'] = $this->getPageName($view);
6572
}
6673

67-
if (in_array($pageType, ['product', 'cart', 'purchase'])) {
74+
if (in_array($pageType, ['product'])) {
6875
$result['google_tag_params']['ecomm_prodid'] = $this->getPageProductRef($view);
6976
}
7077

7178
if (in_array($pageType, ['cart', 'purchase'])) {
7279
$result['google_tag_params']['ecomm_totalvalue'] = $this->getOrderTotalAmount($view);
7380
}
7481

75-
return json_encode($result);
82+
return json_encode($result, JSON_HEX_APOS );
7683
}
7784

7885
public function getProductItem(
@@ -156,7 +163,7 @@ public function getProductItem(
156163
return $item;
157164
}
158165

159-
public function getProductItems(array $productIds, $itemList = false)
166+
public function getProductItems(array $productIds = null, $itemList = false)
160167
{
161168
$session = $this->requestStack->getCurrentRequest()->getSession();
162169
$products = ProductQuery::create()->filterById($productIds)->find();
@@ -196,7 +203,53 @@ public function getLogInData($authAction)
196203
return json_encode($result);
197204
}
198205

199-
public function getPurchaseData($orderId)
206+
public function getCartData(int $cartId, $addressCountry): string
207+
{
208+
$cart = CartQuery::create()->findPk($cartId);
209+
210+
if (!$cart) {
211+
return json_encode([]);
212+
}
213+
214+
$items = array_map(function (CartItem $cartItem) use ($addressCountry) {
215+
return $this->getProductCartItems($cartItem, $addressCountry);
216+
}, iterator_to_array($cart->getCartItems()));
217+
218+
return json_encode([
219+
'event' => 'view_cart',
220+
'currency' => $cart->getCurrency()->getCode(),
221+
'value' => $cart->getTaxedAmount($addressCountry),
222+
'items' => $items
223+
], JSON_HEX_APOS);
224+
}
225+
226+
public function getCheckOutData(int $cartId, $addressCountry): string
227+
{
228+
$cart = CartQuery::create()->findPk($cartId);
229+
230+
if (!$cart) {
231+
return json_encode([]);
232+
}
233+
234+
/** @var Session $session */
235+
$session = $this->requestStack->getCurrentRequest()->getSession();
236+
237+
$coupons = implode(',',$session->getConsumedCoupons());
238+
239+
$items = array_map(function (CartItem $cartItem) use ($addressCountry) {
240+
return $this->getProductCartItems($cartItem, $addressCountry);
241+
}, iterator_to_array($cart->getCartItems()));
242+
243+
return json_encode([
244+
'event' => 'begin_checkout',
245+
'currency' => $cart->getCurrency()->getCode(),
246+
'value' => $cart->getTaxedAmount($addressCountry),
247+
'coupon' => $coupons,
248+
'items' => $items
249+
], JSON_HEX_APOS);
250+
}
251+
252+
public function getPurchaseData(int $orderId)
200253
{
201254
$order = OrderQuery::create()->findPk($orderId);
202255

@@ -235,7 +288,7 @@ public function getPurchaseData($orderId)
235288
'country' => $invoiceAddress->getCountry()->getIsoalpha2()
236289
]
237290
]
238-
]);
291+
], JSON_HEX_APOS);
239292
}
240293

241294
public function getOrderProductItems(Order $order, Country $country)
@@ -259,6 +312,20 @@ public function getOrderProductItems(Order $order, Country $country)
259312
return $items;
260313
}
261314

315+
public function getProductCartItems(CartItem $cartItem, Country $country)
316+
{
317+
$session = $this->requestStack->getCurrentRequest()->getSession();
318+
319+
/** @var Lang $lang */
320+
$lang = $session->get('thelia.current.lang');
321+
322+
$currency = $session->getCurrency() ?: CurrencyQuery::create()->findOneByByDefault(1);
323+
324+
$product = $cartItem->getProductSaleElements()->getProduct();
325+
326+
return $this->getProductItem($product, $lang, $currency, $cartItem->getProductSaleElements(), $cartItem->getQuantity(), false, true, $country);
327+
}
328+
262329
protected function getCategories(Category $category, $locale, $categories)
263330
{
264331
if ($category->getParent() !== 0) {

templates/frontOffice/default/datalayer/select-item.html

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<script>
2-
const products = document.getElementsByClassName("SingleProduct")
2+
/*document.addEventListener("DOMContentLoaded", function(e) {
3+
4+
const products = document.getElementsByClassName("SingleProduct");
35
46
let processLinkClick = async function(e) {
57
let targetUrl;
@@ -37,4 +39,6 @@
3739
for (let i = 0; i < products.length; i++) {
3840
products[i].addEventListener('click', processLinkClick, false);
3941
}
42+
43+
});*/
4044
</script>

0 commit comments

Comments
 (0)