Skip to content

Commit

Permalink
fix: ga4 not working
Browse files Browse the repository at this point in the history
- passing env variable into script block of <svelte:head> have issue.
using onMount instead
  • Loading branch information
songlim327 committed Aug 15, 2024
1 parent 3294f4c commit 40184c3
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions src/routes/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<script lang="ts">
import { onMount } from 'svelte';
import { toggleMode } from 'mode-watcher';
import { createQuery } from '@tanstack/svelte-query';
import { page } from '$app/stores';
Expand Down Expand Up @@ -73,6 +74,20 @@
const openLink = (url: string) => {
window.open(url, '_blank');
};
// Google tag (gtag.js), apparently have issue passing variable in <svelte:head> script block
onMount(() => {
const script = document.createElement('script');
script.innerHTML = `
window.dataLayer = window.dataLayer || [];
function gtag() { dataLayer.push(arguments); }
gtag('js', new Date());
gtag('config', '${PUBLIC_GA4}');
`;
document.head.appendChild(script);
});
</script>

<svelte:head>
Expand All @@ -92,15 +107,6 @@

<!-- Google tag (gtag.js) -->
<script async src={`https://www.googletagmanager.com/gtag/js?id=${PUBLIC_GA4}`}></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag() {
dataLayer.push(arguments);
}
gtag('js', new Date());
gtag('config', PUBLIC_GA4);
</script>

<title>{name}</title>
<meta name="description" content={description} />
Expand Down

0 comments on commit 40184c3

Please sign in to comment.