Skip to content

Commit

Permalink
docs(SvelteKit): fix Google Tag Manager example (#570)
Browse files Browse the repository at this point in the history
  • Loading branch information
a4vg authored Mar 18, 2024
1 parent 67b2829 commit 0258d26
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions docs/sveltekit.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,11 @@ Adapting from [the HTML integration guide](https://partytown.builder.io/html)

## 3. Then add 3rd party scripts

This is where we use partytown to add those scripts (note `type="text/partytown"` below). This example shows Google Tag Manager. Putting it together with the previous changes, our `+layout.svelte` looks like:
This is where we use partytown to add those scripts (note `type="text/partytown"` below). If your script declares global functions or variables, make sure they are explicitly declared with `window` and forwarded to the web worker.

This example shows Google Tag Manager. Note `window.gtag = function()` instead of `function gtag()`.

Putting it together with the previous changes, our `+layout.svelte` looks like:

```svelte
// src/routes/+layout.svelte
Expand All @@ -65,7 +69,7 @@ This is where we use partytown to add those scripts (note `type="text/partytown"
<script>
// Forward the necessary functions to the web worker layer
partytown = {
forward: ['dataLayer.push']
forward: ['dataLayer.push', 'gtag']
};
</script>
Expand All @@ -74,7 +78,7 @@ This is where we use partytown to add those scripts (note `type="text/partytown"
<script type="text/partytown" src="https://www.googletagmanager.com/gtag/js?id=G-ZX7H2KPXNZ"></script>
<script type="text/partytown">
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
window.gtag = function(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-ZX7H2KPXNZ');
</script>
Expand Down

0 comments on commit 0258d26

Please sign in to comment.