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

Load Google Analytics in web worker using Partytown #218

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ vite.config.ts.timestamp-*
# Local Netlify folder
.netlify
/static/pagefind
/static/~partytown
.env
1 change: 1 addition & 0 deletions .vscode/ltex.dictionary.en-US.txt
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,4 @@ Roboto
Saira
impersonification
Partiful
Partytown
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ pnpm run dev
# Open http://localhost:37572
```

## Debugging

The website uses the Partytown library, which can clutter network requests in DevTools with entries for `proxytown`. To exclude those requests, you can use the filter `-url:proxytown`.

## Deployment

The contents of the repository are continuously deployed to Netlify. You can track the deployment status [here](https://app.netlify.com/sites/pauseai/deploys).
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"vite": "^5.4.8"
},
"dependencies": {
"@builder.io/partytown": "^0.10.2",
"@fontsource/roboto-slab": "^5.1.0",
"@fontsource/saira-condensed": "^5.1.0",
"@pagefind/default-ui": "^1.1.1",
Expand Down
18 changes: 10 additions & 8 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

72 changes: 37 additions & 35 deletions src/app.html
Original file line number Diff line number Diff line change
@@ -1,38 +1,40 @@
<!doctype html>
<html lang="en">

<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
<link rel="icon" href="%sveltekit.assets%/favicon.png" />
<link rel="alternate" type="application/atom+xml" href="/rss.xml" />

<script type="module">
const theme = localStorage.getItem('color-scheme')

theme
? document.documentElement.setAttribute('color-scheme', theme)
: localStorage.setItem('color-scheme', 'dark')
</script>

<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-VRJ4J53FC8"></script>
<script>
window.dataLayer ??= []
function gtag() {
dataLayer.push(arguments)
}

gtag('js', new Date())
gtag('config', 'G-VRJ4J53FC8')
gtag('config', 'AW-16494962941')
</script>

%sveltekit.head%
</head>

<body data-sveltekit-preload-data="hover">
<div style="display: contents">%sveltekit.body%</div>
</body>

<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
<link rel="icon" href="%sveltekit.assets%/favicon.png" />
<link rel="alternate" type="application/atom+xml" href="/rss.xml" />

<script type="module">
const theme = localStorage.getItem('color-scheme')

theme
? document.documentElement.setAttribute('color-scheme', theme)
: localStorage.setItem('color-scheme', 'dark')
</script>

<!-- Google tag (gtag.js) -->
<script
type="text/partytown"
async
src="https://www.googletagmanager.com/gtag/js?id=G-VRJ4J53FC8"
></script>
<script type="text/partytown">
window.dataLayer ??= []
window.gtag = function() {
dataLayer.push(arguments)
}

gtag('js', new Date())
gtag('config', 'G-VRJ4J53FC8')
gtag('config', 'AW-16494962941')
</script>

%sveltekit.head%
</head>

<body data-sveltekit-preload-data="hover">
<div style="display: contents">%sveltekit.body%</div>
</body>
</html>
10 changes: 10 additions & 0 deletions src/routes/+layout.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<script lang="ts">
import { Toaster } from 'svelte-french-toast'
import { ProgressBar } from '@prgm/sveltekit-progress-bar'
import { partytownSnippet } from '@builder.io/partytown/integration'

import Footer from './footer.svelte'
import Header from './header.svelte'
Expand All @@ -19,6 +20,15 @@
$: hero = data.url == '/'
</script>

<svelte:head>
<script>
partytown = {
forward: ['dataLayer.push', 'gtag']
}
</script>
{@html '<script>' + partytownSnippet() + '</script>'}
</svelte:head>

<h2 style="width: 0; height: 0; margin: 0; padding: 0; visibility: hidden;" data-pagefind-ignore>
(Top)
</h2>
Expand Down
11 changes: 10 additions & 1 deletion vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
import { partytownVite } from '@builder.io/partytown/utils'
import { join } from 'path'
import { sveltekit } from '@sveltejs/kit/vite'

const config = {
server: {
port: 37572
},
plugins: [sveltekit()]
plugins: [
// Partytown first because SvelteKit copies /static in same hook
partytownVite({
// Place in /static as /build is wiped by adapter which always runs last
dest: join(__dirname, 'static', '~partytown')
}),
sveltekit()
]
}

export default config