Skip to content

Restore analytics bootstrapping in Next apps#963

Merged
PavelMakarchuk merged 1 commit intomainfrom
fix/restore-next-analytics
Apr 15, 2026
Merged

Restore analytics bootstrapping in Next apps#963
PavelMakarchuk merged 1 commit intomainfrom
fix/restore-next-analytics

Conversation

@PavelMakarchuk
Copy link
Copy Markdown
Contributor

@PavelMakarchuk PavelMakarchuk commented Apr 15, 2026

Summary

Restore GA4 and Vercel analytics bootstrapping in the Next.js website and calculator app layouts.

Root cause

The legacy Vite apps loaded GA and mounted Vercel Analytics, but the migrated Next app layouts did not. That left window.gtag undefined on migrated routes and caused analytics to drop sharply once traffic shifted to those apps.

Changes

  • load gtag.js in website/src/app/layout.tsx
  • initialize window.dataLayer and window.gtag in website/src/app/layout.tsx
  • mount @vercel/analytics and @vercel/speed-insights in website/src/app/layout.tsx
  • load gtag.js in calculator-app/src/app/layout.tsx
  • initialize window.dataLayer and window.gtag in calculator-app/src/app/layout.tsx
  • mount @vercel/analytics and @vercel/speed-insights in calculator-app/src/app/layout.tsx

Verification

  • ran local Next dev servers via bun run dev
  • verified served HTML for http://localhost:3002/us includes GA4 bootstrap and Vercel analytics clients
  • verified served HTML for http://localhost:3002/us/pe84 includes GA4 bootstrap and the migrated AppClient route bundle used for tool_engaged
  • verified served HTML for http://localhost:3003/us/simulations includes GA4 bootstrap and Vercel analytics clients

Notes

  • local calculator routes still surface an unrelated existing SSR issue: No QueryClient set, use QueryClientProvider to set one
  • that issue is separate from this analytics fix and does not change the restored analytics bootstrapping

@vercel
Copy link
Copy Markdown

vercel bot commented Apr 15, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
policyengine-app-v2 Ready Ready Preview, Comment Apr 15, 2026 2:53pm
policyengine-calculator Ready Ready Preview, Comment Apr 15, 2026 2:53pm
policyengine-calculator-next Ready Ready Preview, Comment Apr 15, 2026 2:53pm
policyengine-website Ready Ready Preview, Comment Apr 15, 2026 2:53pm

Request Review

Copy link
Copy Markdown

@policyengine policyengine bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR correctly restores analytics bootstrapping in the Next.js apps. The implementation is solid and matches the legacy Vite app pattern:

What's correct:

  • ✅ GA4 measurement ID (G-2YHG89FY0N) matches the legacy HTML files
  • ✅ Uses Next.js Script component with proper strategy (afterInteractive)
  • ✅ Assigns window.gtag globally for downstream code compatibility
  • ✅ Analytics and SpeedInsights components properly mounted in body
  • ✅ Dependencies (@vercel/analytics, @vercel/speed-insights) available in root package.json
  • ✅ Both calculator-app and website layouts updated consistently

Verification:
The legacy Vite apps (app/calculator.html, app/website.html) have the same GA4 bootstrap script, and the main entry files (main.calculator.tsx, main.website.tsx) mount Analytics/SpeedInsights components. This PR correctly ports that pattern to the Next.js layouts.

No issues found. The implementation will restore window.gtag and analytics tracking on migrated routes.

@PavelMakarchuk PavelMakarchuk merged commit 00723c7 into main Apr 15, 2026
13 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.

1 participant