Commit f3c9982
authored
feat(analytics): add client-side gtag.js to root layout (#60)
Render the GA4 gtag.js snippet from app/layout.tsx so page views and
client interactions land in the same GA4 property the server-side
Measurement Protocol code already writes to. Complements the
server-side trackEvent path in lib/analytics.ts — together they cover
both surface areas (page views from the client, airdrop outcomes from
the API route) without double-counting.
Uses next/script with strategy="afterInteractive" rather than raw
<script> tags so Next.js manages load timing and dedup across
navigations.
Driven by NEXT_PUBLIC_GA4_MEASUREMENT_ID. When the var is unset (e.g.
local dev without analytics, or any environment we don't want tracking
on), the GA4 <Script> tags don't render at all. The ID is public by
design — every visitor receives it in the gtag.js src URL — so the
NEXT_PUBLIC_ prefix is safe.
Document the new env var in .env.example with a note to keep it in
sync with the existing server-side GA4_MEASUREMENT_ID (same GA4
property), and add it to ProcessEnv in types.d.ts.1 parent 3318dcf commit f3c9982
3 files changed
Lines changed: 36 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
116 | 116 | | |
117 | 117 | | |
118 | 118 | | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
| 3 | + | |
3 | 4 | | |
4 | 5 | | |
5 | 6 | | |
| |||
26 | 27 | | |
27 | 28 | | |
28 | 29 | | |
| 30 | + | |
| 31 | + | |
29 | 32 | | |
30 | 33 | | |
31 | 34 | | |
| |||
36 | 39 | | |
37 | 40 | | |
38 | 41 | | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
39 | 59 | | |
40 | 60 | | |
41 | 61 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
43 | 43 | | |
44 | 44 | | |
45 | 45 | | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
46 | 54 | | |
47 | 55 | | |
48 | 56 | | |
| |||
0 commit comments