From b4ae0f6ed1c928a78e17bccec43eea1d39071261 Mon Sep 17 00:00:00 2001 From: indykoning <15870933+indykoning@users.noreply.github.com> Date: Thu, 7 Sep 2023 15:06:03 +0200 Subject: [PATCH] Ensure datalayer providers are imported before sending pageview events (#8) --- resources/js/gtm.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/resources/js/gtm.js b/resources/js/gtm.js index 45c8d79..7e3a395 100644 --- a/resources/js/gtm.js +++ b/resources/js/gtm.js @@ -1,6 +1,6 @@ window.removeTrailingZeros = (price) => parseFloat(parseFloat(price).toString()); -(async () => { +let dataLayersPromise = (async () => { // This async function is in order to work around "ERROR: Top-level await is not available" when building for older browsers. window.dataLayers = { ua: window.config.gtm['send-ua-events'] ? await import('./datalayer/ua.js') : undefined, @@ -15,6 +15,7 @@ document.addEventListener('turbo:load', async (event) => { if (window.config.gtm['clear-on-load']) { window.dataLayer = [] } + await dataLayersPromise let url = new URL(event.detail.url);