Skip to content

Add Google Analytics (GA4) for production hosts (#145) - #142

Merged
Luen merged 2 commits into
mainfrom
feat/google-analytics
Nov 4, 2026
Merged

Luen merged 2 commits into
mainfrom
feat/google-analytics

Conversation

@Luen

@Luen Luen commented Nov 4, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

  • Load GA4 Measurement ID G-VZF5DR37YH (shared Physioshark stream) via GoogleAnalytics.svelte only when the request host is data.physioshark.org or physioshark.org
  • Send SPA page_view events on navigation; update CSP for gtag / Analytics hosts
  • Document the setup in AGENTS.md and docs/DEPLOY.md

Test plan

  • pnpm lint and pnpm check pass
  • Localhost / fixture preview does not load gtag (view page source / Network)
  • After deploy to data.physioshark.org, GA Realtime shows hits; client navigations increment page views
  • Filter Explorations by Page hostname if separating data site vs physioshark.org

Closes #145

Loads gtag (G-VZF5DR37YH) only for data.physioshark.org / physioshark.org, updates CSP, and records SPA navigations.

Co-authored-by: Cursor <cursoragent@cursor.com>
@Luen Luen changed the title Add Google Analytics (GA4) for production hosts Add Google Analytics (GA4) for production hosts (#145) Nov 4, 2026
The site's CSP only allows SvelteKit's nonce'd inline scripts, so the
inline gtag snippet in <svelte:head> was blocked on data.physioshark.org:
window.gtag never existed and no hits were sent. The command queue is now
created in the component (first afterNavigate), before the allowed
gtag.js script drains it. Checked on a production build served as
data.physioshark.org: the only scripts are SvelteKit's nonce'd ones and
gtag.js; on localhost GA isn't loaded.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Luen

Luen commented Nov 4, 2026

Copy link
Copy Markdown
Collaborator Author

One fix pushed in 03190d1: as written, GA would have sent nothing.

The site's CSP (mode: 'auto') only lets SvelteKit's own nonce'd inline scripts run. The gtag setup was an inline <script> in <svelte:head> with no nonce. A production build served as data.physioshark.org sent script-src 'self' https://www.googletagmanager.com 'nonce-…', and that snippet was the only inline script without the nonce. Browsers block it, so window.gtag never existed and afterNavigate returned early on every page.

The command queue (dataLayer, gtag('js'), gtag('config')) is now created from the component's bundled code on the first afterNavigate, before the allowed gtag.js script drains it. Only gtag.js stays in <svelte:head>.

Checked on a production build:

  • Served as data.physioshark.org, the page loads SvelteKit's nonce'd scripts and gtag.js only.
  • On localhost, GA isn't loaded.
  • pnpm lint, pnpm check and the Playwright suite (33 passed).

I didn't load it in a browser on the production host, so as not to send a test hit to the live property. The "GA Realtime shows hits" step in the test plan is still the real check after deploy.

The rest looks good: hosts are limited to production, there are SPA page views, and the CSP and docs are updated.

@Luen
Luen merged commit 5022497 into main Nov 4, 2026
5 checks passed
@Luen
Luen deleted the feat/google-analytics branch November 4, 2026 05:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

add in Google Analytics

1 participant