feat(analytics): add client-side gtag.js to root layout#60
Merged
Conversation
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.
|
@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
approved these changes
May 25, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
gtag.jssnippet fromapp/layout.tsxvianext/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.NEXT_PUBLIC_GA4_MEASUREMENT_ID— when unset, the<Script>tags don't render at all, so the bundle stays clean in environments without analytics..env.example(with a note to keep it in sync with the server-sideGA4_MEASUREMENT_ID— same GA4 property), and add it toProcessEnvintypes.d.ts.Why
next/scriptand not raw<script>tagsnext/scriptmanages 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_IDThe 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:Documented in
.env.exampleso the sync expectation is explicit.What's NOT changed
Server-side analytics in
lib/analytics.tsis untouched. The relatedwaitUntilfix lives in #59 — independent concern, independent PR.Test plan
npx tsc --noEmit— cleannpx vitest run— all 111 tests passNEXT_PUBLIC_GA4_MEASUREMENT_ID=G-XXXXin 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