// Get the GA code from the URL query string const script = document.currentScript; const src = script?.src; const url = new URL(src); const googleAnalyticsCode = url?.searchParams.get('id'); const bgColor = `#${url?.searchParams.get('bg') || '50e3c2'}`; document.addEventListener("DOMContentLoaded", function () { // Load the CSS const link = document.createElement('link'); link.rel = 'stylesheet'; link.href = 'cookie-banner/css/cookie-banner-styles.css'; document.head.appendChild(link); // Load the cookie banner HTML fetch("./cookie-banner/cookie-banner.html") .then(response => { return response.text() }) .then(data => { document.body.insertAdjacentHTML("beforeend", data); }).then(() => { const cookieBanner = document.getElementById("cookie-banner-wrapper"); const acceptButton = document.getElementById("cookie-banner-accept-button"); const rejectButton = document.getElementById("cookie-banner-reject-button"); const cookiePolicyLink = document.getElementById("cookie-policy-link"); acceptButton.style.backgroundColor = bgColor; rejectButton.style.backgroundColor = bgColor; cookiePolicyLink.style.color = bgColor; // If localStorage is null, it's likely first visit const showCookieBanner = localStorage.getItem("cookiesAccepted") === null; const cookiesAccepted = localStorage.getItem('cookiesAccepted') === "true"; if (showCookieBanner) { cookieBanner.style.display = "block"; } if(cookiesAccepted) { window.dataLayer = window.dataLayer || []; function gtag() { dataLayer.push(arguments); } gtag('js', new Date()); gtag('config', googleAnalyticsCode); } // Button event handlers acceptButton.addEventListener("click", function () { localStorage.setItem("cookiesAccepted", "true"); // Reload the page to init GA and record a pageview location.reload(); }); rejectButton.addEventListener("click", function () { localStorage.setItem("cookiesAccepted", "false"); cookieBanner.style.display = "none"; }); }); });