Skip to content

Commit d8d96df

Browse files
andrewboldiclaude
andcommitted
fix: track SPA navigations in Google Analytics
GA only fires on initial page load by default. Added $effect in layout to send page_view events on client-side route changes (/ ↔ /play). Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent aedb39a commit d8d96df

2 files changed

Lines changed: 25 additions & 0 deletions

File tree

src/app.d.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// See https://svelte.dev/docs/kit/types#app.d.ts
2+
// for information about these interfaces
3+
declare global {
4+
namespace App {
5+
// interface Error {}
6+
// interface Locals {}
7+
// interface PageData {}
8+
// interface PageState {}
9+
// interface Platform {}
10+
}
11+
12+
// Google Analytics gtag
13+
function gtag(...args: unknown[]): void;
14+
}
15+
16+
export {};

src/routes/+layout.svelte

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,18 @@
44
import 'katex/dist/katex.min.css';
55
import '../app.css';
66
import { settings } from '$lib/stores/settings';
7+
import { page } from '$app/state';
78
89
let { children } = $props();
910
11+
// Track SPA navigations in Google Analytics
12+
$effect(() => {
13+
const path = page.url.pathname;
14+
if (typeof gtag === 'function') {
15+
gtag('config', 'G-P98FJ92R7Z', { page_path: path });
16+
}
17+
});
18+
1019
function hexToRgb(hex: string): string {
1120
const r = parseInt(hex.slice(1, 3), 16);
1221
const g = parseInt(hex.slice(3, 5), 16);

0 commit comments

Comments
 (0)