Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions MageWire/Checkout.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@

class Checkout extends Component
{
protected $listeners = [
'shipping_method_selected' => 'triggerShippingMethod',
'payment_method_selected' => 'triggerPaymentMethod',
];
private CheckoutSession $checkoutSession;
private BeginCheckout $beginCheckout;
private AddShippingInfo $addShippingInfo;
Expand All @@ -31,6 +27,17 @@ public function __construct(
$this->addPaymentInfo = $addPaymentInfo;
}

public function boot(): void
{
$parent = get_parent_class($this);
if ($parent && method_exists($parent, 'boot')) {
parent::boot();
}

$this->listeners['shipping_method_selected'] = 'triggerShippingMethod';
$this->listeners['payment_method_selected'] = 'triggerPaymentMethod';
}

public function triggerBeginCheckout()
{
$this->dispatchBrowserEvent('ga:trigger-event', $this->beginCheckout->get());
Expand Down
6 changes: 4 additions & 2 deletions view/frontend/layout/hyva_checkout_index_index.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="main">
<block template="Tagging_GTM::hyva_checkout/data-layer.phtml"
ifconfig="googletagmanager2/settings/enabled">
<block name="yireo_googletagmanager2.hyva_checkout.data-layer.component"
template="Tagging_GTM::hyva_checkout/data-layer.phtml"
ifconfig="GTM/settings/enabled"
after="Tagging_GTM.pusher-script">
<arguments>
<argument name="magewire" xsi:type="object">Tagging\GTM\MageWire\Checkout</argument>
<argument name="begin_checkout_event" xsi:type="object">Tagging\GTM\DataLayer\Event\BeginCheckout</argument>
Expand Down
22 changes: 7 additions & 15 deletions view/frontend/templates/hyva_checkout/data-layer.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,18 @@ if (false === $magewire->isHyvaCheckoutEnabled()) {
}

$beginCheckoutEvent = $block->getData('begin_checkout_event');
$beginCheckoutPayload = $beginCheckoutEvent->get();
?>
<div>
<div class="hidden">
<script>
function initYireoGoogleTagManagerInHyvaCheckout() {
(function () {
"use strict";

function triggerEvent(event) {
window.addEventListener('ga:trigger-event', function (event) {
googleTagManager2Pusher(event.detail, 'Hyva Checkout');
}
}, {passive: true});

triggerEvent(new CustomEvent('ga:trigger-event', {detail: <?= json_encode($beginCheckoutEvent->get()) ?>}));

return {
triggerEvent
}
}
googleTagManager2Pusher(<?= /* @noEscape */ json_encode($beginCheckoutPayload) ?>, 'Hyva Checkout');
})();
</script><?php if (isset($hyvaCsp) && $hyvaCsp): ?><?php $hyvaCsp->registerInlineScript() ?><?php endif; ?>
<div id="ga-trigger"
x-data="initYireoGoogleTagManagerInHyvaCheckout()"
class="hidden"
@ga:trigger-event.window="triggerEvent($event)"
></div>
</div>