Skip to content
Open
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
33 changes: 32 additions & 1 deletion view/frontend/templates/hyva_checkout/data-layer.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,38 @@ $beginCheckoutPayload = $beginCheckoutEvent->get();
googleTagManager2Pusher(event.detail, 'Hyva Checkout');
}, {passive: true});

googleTagManager2Pusher(<?= /* @noEscape */ json_encode($beginCheckoutPayload) ?>, 'Hyva Checkout');
const beginCheckoutPayload = <?= /* @noEscape */ json_encode($beginCheckoutPayload) ?>;

function userDataAlreadyPushed() {
if (!Array.isArray(window.dataLayer)) {
return false;
}
return window.dataLayer.some(function (entry) {
return entry && entry.event === 'trytagging_user_data';
});
}

function pushBeginCheckout() {
googleTagManager2Pusher(beginCheckoutPayload, 'Hyva Checkout');
}

function waitForUserData(maxWaitMs) {
const start = Date.now();
const interval = setInterval(function () {
if (userDataAlreadyPushed() || Date.now() - start >= maxWaitMs) {
clearInterval(interval);
pushBeginCheckout();
}
}, 50);
}

if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', function () {
waitForUserData(2000);
});
} else {
waitForUserData(2000);
}
})();
</script><?php if (isset($hyvaCsp) && $hyvaCsp): ?><?php $hyvaCsp->registerInlineScript() ?><?php endif; ?>
</div>