Skip to content

Commit

Permalink
chore: split gtm/ga code import
Browse files Browse the repository at this point in the history
  • Loading branch information
robsongajunior committed Dec 2, 2024
1 parent 2cac3ee commit fd5a5c5
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 84 deletions.
6 changes: 4 additions & 2 deletions src/components/html/trd3.astro
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
---
import Clarity from '~/components/trd3/clarity/index.astro';
import Hubspot from '~/components/trd3/hubspot/index.astro';
import GoogleTagManagerScript from '~/components/trd3/google-tag-manager/script.astro';
import GoogleAnalytics from '~/components/trd3/google/analytics.astro';
import GoogleTagManager from '~/components/trd3/google/tagmanager.astro';
---

<GoogleTagManagerScript />
<GoogleAnalytics />
<GoogleTagManager />
<Clarity />
<Hubspot />
82 changes: 0 additions & 82 deletions src/components/trd3/google-tag-manager/script.astro

This file was deleted.

29 changes: 29 additions & 0 deletions src/components/trd3/google/analytics.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<script is:inline defer type="text/javascript">
(function () {
if (
window.location.origin !== "https://www.azion.com" ||
window.location.search.match(/trd3=false/)
) {
console.log("Google GTAG: disabled");
return;
}

window.dataLayer = window.dataLayer || [];

function gtag() {
dataLayer.push(arguments);
}

gtag("js", new Date());
gtag("config", "G-LNFV74DS0K");

function load() {
let script = document.createElement("script");
script.async = true;
script.src = "https://www.googletagmanager.com/gtag/js?id=G-LNFV74DS0K";
document.head.appendChild(script);
}

setTimeout(load, window.innerWidth >= 1024 ? 2500 : 5000);
})();
</script>
46 changes: 46 additions & 0 deletions src/components/trd3/google/tagmanager.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<script is:inline defer type="text/javascript">
(function () {
window.dataLayer = window.dataLayer || [];

function loadScript() {
if (
window.location.origin !== "https://www.azion.com" ||
window.location.search.match(/trd3=false/)
) {
console.log("Google Tag Manager: disabled");
return;
}

(function (w, l) {
w[l] = w[l] || [];
w[l].push({
"gtm.start": new Date().getTime(),
event: "gtm.js",
});
})(window, "dataLayer");

let script = document.createElement("script");
script.async = true;
script.src = "https://www.googletagmanager.com/gtm.js?id=GTM-NCNDRN2";
document.head.appendChild(script);
}

loaded = false;
window.addEventListener("load", () => {
window.addEventListener("scroll", () => {
if (window.scrollY < 100) return;
if (loaded) return;

loadScript();
loaded = true;
});

window.addEventListener("mouseover", () => {
if (loaded) return;

loadScript();
loaded = true;
});
});
})();
</script>
3 changes: 3 additions & 0 deletions src/layouts/BaseLayout.astro
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import HeadCommon from '~/components/HeadCommon.astro';
import HeadSEO from '~/components/HeadSEO.astro';
import LeftSidebar from '~/components/LeftSidebar/LeftSidebar.astro';
import SidebarPanelMenu from '~/components/sidebarpanelmenu/SidebarPanelMenu.vue';
import GoogleTagManagerIframe from '~/components/trd3/google/tagmanager-iframe.astro';
import Trd3 from '~/components/html/trd3.astro';
// @ts-nocheck
Expand Down Expand Up @@ -176,6 +177,8 @@ const translatedPages = await (async (c) => {
</head>

<body class="surface-ground">
<GoogleTagManagerIframe />

<script is:inline>
const setTheme = (theme) => {
const toggleTheme = () => {
Expand Down

0 comments on commit fd5a5c5

Please sign in to comment.