|
14 | 14 |
|
15 | 15 |
|
16 | 16 | use GoogleTagManager\GoogleTagManager;
|
| 17 | +use GoogleTagManager\Service\GoogleTagService; |
17 | 18 | use Thelia\Core\Event\Hook\HookRenderEvent;
|
18 | 19 | use Thelia\Core\Hook\BaseHook;
|
19 |
| -use Thelia\Model\Base\LangQuery; |
20 |
| -use Thelia\Model\ConfigQuery; |
| 20 | +use Thelia\Model\LangQuery; |
21 | 21 |
|
22 | 22 | /**
|
23 | 23 | * Class FrontHook
|
|
26 | 26 | */
|
27 | 27 | class FrontHook extends BaseHook
|
28 | 28 | {
|
29 |
| - public function onMainHeadTop(HookRenderEvent $event){ |
30 |
| - $lang = $this->getLang(); |
31 |
| - $value = GoogleTagManager::getConfigValue('googletagmanager_gtmId', null, $lang->getLocale()); |
32 |
| - if ("" != $value){ |
| 29 | + /** |
| 30 | + * @var GoogleTagService |
| 31 | + */ |
| 32 | + private $googleTagService; |
| 33 | + |
| 34 | + public function __construct(GoogleTagService $googleTagService) |
| 35 | + { |
| 36 | + $this->googleTagService = $googleTagService; |
| 37 | + } |
| 38 | + |
| 39 | + public function onMainHeadTop(HookRenderEvent $event) |
| 40 | + { |
| 41 | + if ($gtmId = GoogleTagManager::getConfigValue('googletagmanager_gtmId')) { |
| 42 | + $view = $this->request->get('_view'); |
| 43 | + |
| 44 | + $event->add($this->render('datalayer/thelia-page-view.html', ['data' => $this->googleTagService->getTheliaPageViewParameters()])); |
| 45 | + |
| 46 | + if (in_array($view, ['category', 'brand', 'search'])) { |
| 47 | + $event->add($this->render('datalayer/view-item-list.html', ['eventName' => 'view_item_list'])); |
| 48 | + } |
| 49 | + |
| 50 | + if ($view === 'product') { |
| 51 | + $event->add($this->render('datalayer/view-item.html', ['eventName' => 'view_item'])); |
| 52 | + } |
| 53 | + |
| 54 | + if (null !== $authAction = $this->request->getSession()->get(GoogleTagManager::GOOGLE_TAG_TRIGGER_LOGIN)) { |
| 55 | + $event->add($this->render('datalayer/thelia-page-view.html', [ |
| 56 | + 'data' => $this->googleTagService->getLogInData($authAction) |
| 57 | + ])); |
| 58 | + $this->request->getSession()->set(GoogleTagManager::GOOGLE_TAG_TRIGGER_LOGIN, null); |
| 59 | + } |
| 60 | + |
| 61 | + if ($view === 'order-placed') { |
| 62 | + $event->add($this->render('datalayer/thelia-page-view.html', [ |
| 63 | + 'data' => $this->googleTagService->getPurchaseData($this->request->get('order_id')) |
| 64 | + ])); |
| 65 | + } |
| 66 | + |
33 | 67 | $event->add(
|
34 |
| - "<!-- Google Tag Manager -->". |
35 |
| - "<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':". |
36 |
| - "new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],". |
37 |
| - "j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=". |
38 |
| - "'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);". |
39 |
| - "})(window,document,'script','dataLayer','".$value."');</script>". |
| 68 | + "<!-- Google Tag Manager -->" . |
| 69 | + "<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':" . |
| 70 | + "new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0]," . |
| 71 | + "j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=" . |
| 72 | + "'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);" . |
| 73 | + "})(window,document,'script','dataLayer','" . $gtmId . "');</script>" . |
40 | 74 | "<!-- End Google Tag Manager -->"
|
41 | 75 | );
|
42 | 76 | }
|
43 | 77 | }
|
44 |
| - public function onMainBodyTop(HookRenderEvent $event){ |
45 |
| - $lang = $this->getLang(); |
46 |
| - $value = GoogleTagManager::getConfigValue('googletagmanager_gtmId', null, $lang->getLocale()); |
47 |
| - if ("" != $value){ |
48 |
| - $event->add("<!-- Google Tag Manager (noscript) -->". |
49 |
| - "<noscript><iframe src='https://www.googletagmanager.com/ns.html?id=".$value."' ". |
50 |
| - "height='0' width='0' style='display:none;visibility:hidden'></iframe></noscript>". |
| 78 | + |
| 79 | + public function onMainBodyTop(HookRenderEvent $event) |
| 80 | + { |
| 81 | + $value = GoogleTagManager::getConfigValue('googletagmanager_gtmId'); |
| 82 | + |
| 83 | + if ("" != $value) { |
| 84 | + $event->add("<!-- Google Tag Manager (noscript) -->" . |
| 85 | + "<noscript><iframe src='https://www.googletagmanager.com/ns.html?id=" . $value . "' " . |
| 86 | + "height='0' width='0' style='display:none;visibility:hidden'></iframe></noscript>" . |
51 | 87 | "<!-- End Google Tag Manager (noscript) -->"
|
52 | 88 | );
|
53 | 89 | }
|
54 | 90 | }
|
55 | 91 |
|
| 92 | + public function onMainJsInit(HookRenderEvent $event) |
| 93 | + { |
| 94 | + $view = $this->request->get('_view'); |
| 95 | + |
| 96 | + if (in_array($view, ['category', 'brand', 'search'])) { |
| 97 | + $event->add($this->render('datalayer/select-item.html')); |
| 98 | + } |
| 99 | + $event->add($this->render('datalayer/add-to-cart.html')); |
| 100 | + } |
| 101 | + |
| 102 | + public function onProductBottom(HookRenderEvent $event) |
| 103 | + { |
| 104 | + $productId = $event->getArgument('product'); |
| 105 | + $this->request->getSession()->set(GoogleTagManager::GOOGLE_TAG_VIEW_ITEM, $productId); |
| 106 | + } |
| 107 | + |
56 | 108 | protected function getLang()
|
57 | 109 | {
|
58 | 110 | $lang = $this->getRequest()->getSession()->get("thelia.current.lang");
|
59 |
| - if (null === $lang){ |
| 111 | + if (null === $lang) { |
60 | 112 | $lang = LangQuery::create()->filterByByDefault(1)->findOne();
|
61 | 113 | }
|
62 | 114 | return $lang;
|
|
0 commit comments