Skip to content

feat(analytics): add client-side gtag.js to root layout#60

Merged
Woody4618 merged 1 commit into
solana-developers:mainfrom
hoodieshq:feat/client-gtag
May 25, 2026
Merged

feat(analytics): add client-side gtag.js to root layout#60
Woody4618 merged 1 commit into
solana-developers:mainfrom
hoodieshq:feat/client-gtag

Conversation

@askov
Copy link
Copy Markdown
Contributor

@askov askov commented May 25, 2026

Summary

  • Render the GA4 gtag.js snippet from app/layout.tsx via next/script, so page views and client interactions land in the GA4 property the server-side Measurement Protocol code (refactor: split /api/request into focused modules, add tests, add analytics #57) already writes to.
  • Driven by NEXT_PUBLIC_GA4_MEASUREMENT_ID — when unset, the <Script> tags don't render at all, so the bundle stays clean in environments without analytics.
  • Document the new var in .env.example (with a note to keep it in sync with the server-side GA4_MEASUREMENT_ID — same GA4 property), and add it to ProcessEnv in types.d.ts.

Why next/script and not raw <script> tags

next/script manages load timing (strategy="afterInteractive") and dedupes across client-side navigations. Drop-in raw <script> tags inside JSX work but bypass Next's optimizations and can fire multiple times when re-rendering.

Why a separate env var from GA4_MEASUREMENT_ID

The server-side var is referenced in API routes (Node.js runtime) and stays private to the server bundle. The client needs the NEXT_PUBLIC_ prefix to be inlined at build time. They're conventionally the same value (same GA4 property), but kept as two vars so the team can:

  • Disable client tracking without disabling server tracking, or vice versa
  • Point preview deployments at a separate GA4 property without affecting the server-side Measurement Protocol writes

Documented in .env.example so the sync expectation is explicit.

What's NOT changed

Server-side analytics in lib/analytics.ts is untouched. The related waitUntil fix lives in #59 — independent concern, independent PR.

Test plan

  • npx tsc --noEmit — clean
  • npx vitest run — all 111 tests pass
  • Set NEXT_PUBLIC_GA4_MEASUREMENT_ID=G-XXXX in a preview deployment; verify the two <script> tags appear in the page source and that page views show up in GA4 Realtime within ~30 sec
  • Confirm with the var unset, no GA4 script tags appear in the rendered HTML

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.
@vercel
Copy link
Copy Markdown

vercel Bot commented May 25, 2026

@askov is attempting to deploy a commit to the Solana Foundation Team on Vercel.

A member of the Team first needs to authorize it.

@Woody4618 Woody4618 merged commit f3c9982 into solana-developers:main May 25, 2026
1 of 2 checks passed
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.

2 participants