Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs(SvelteKit): fix Google Tag Manager example #570

Merged
merged 1 commit into from
Mar 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading