Postmortem: Google Analytics Consent Race Condition in Next.js
Postmortem: Google Analytics Consent Race Condition in Next.js
How a React hydration timing bug caused GA4 to drop 100% of consenting user data with npa=1 and gcs=G100 — and the…
·Updated on:··
⚡ Next.js Implementation Guides
In-depth Next.js guides covering App Router, RSC, ISR, and deployment. Get code examples, optimization checklists, and prompts to accelerate development.
A critical race condition in Google Analytics consent initialization caused 100% of analytics data to be discarded by Google due to privacy compliance flags. (If you are building the banner itself, see my Next.js GDPR cookie consent banner guide and Google Analytics with Tag Manager setup.) The issue manifested as npa=1 (No Personalized Ads) in all GA4 network requests, even when users had explicitly granted consent via the cookie consent banner. This resulted in:
Complete loss of analytics data from consenting users
SEO impact due to missing user behavior signals
Business intelligence gaps preventing data-driven decisions
Potential revenue loss from inability to track and optimize conversions
The root cause was a timing issue where Google Analytics initialized and sent pageview events before the consent state was read from the cookie, defaulting to "denied" consent mode.
Timeline
Initial State (Unknown - Pre-Incident)
Google Analytics consent implementation using React useEffect hooks
Consent state managed via React component state
Cookie consent banner functioning correctly
Users able to accept/reject analytics
Incident Discovery
Issue Detected: Network logs showed npa=1 on all GA4 requests
Indicator:gcs=G100 (analytics denied, ad storage denied)
Impact: Google discarding all analytics data for privacy compliance
User Report: "Your traffic is being thrown in the trash"
Fix Attempt #1: React State Optimization
Action: Moved consent check earlier in React component lifecycle
Result: Failed - still showing npa=1
Root Cause: React state updates are asynchronous; GA4 scripts loaded simultaneously
Fix Attempt #2: Inline Script in Component
Action: Added inline <Script> tag with consent initialization before gtag loads
Implementation: Used Next.js Script component with strategy="afterInteractive"
Result: Failed - still showing npa=1
Fix Attempt #3: Raw Script Tags in Layouts ✅
Action: Added raw <script> tags directly in server-rendered layouts
Implementation: Script executes immediately on HTML parse, before React hydration
src/app/(non-intl)/layout.tsx - Added raw script tag for immediate consent
src/app/(intl)/[locale]/layout.tsx - Added raw script tag for immediate consent
src/components/analytics/ga4-consent.tsx - Simplified to load scripts only
Complete Fix (Phase 2 - Ad Consent)
src/app/(non-intl)/layout.tsx - Grant all ad consent parameters
src/app/(intl)/[locale]/layout.tsx - Grant all ad consent parameters
src/components/analytics/ga4-consent.tsx - Update dynamic consent with ads
src/lib/cookies/client-cookie-utils.ts - Grant ads in cookie setter
Git History
code
1c38dce fix: grant ad_storage consent when analytics is accepted
8719ac6 fix: move consent initialization to raw script tags in layouts
ad34912 fix: resolve race condition in GA4 consent initialization
This incident highlighted the critical importance of timing in consent mode implementation. What appeared to be a simple "read cookie and update consent" operation revealed itself to be a complex race condition requiring deep understanding of:
Browser script execution order
React/Next.js lifecycle
Google Analytics consent architecture
Privacy-by-default behavior
The fix required moving from React-based asynchronous initialization to synchronous, server-rendered script execution. This ensures consent is set before any tracking occurs, maintaining user privacy while enabling proper analytics when consent is granted.
Key Takeaway: Critical initialization that affects data integrity must happen synchronously and as early as possible in the page load lifecycle. React components are not appropriate for this type of blocking operation.
Document Owner: Development Team
Last Updated: 2025-11-24
Next Review: After 30 days of monitoring post-fix
Root Cause: All scripts with same strategy load simultaneously, no guaranteed order
ad_user_data
ad_personalization
Result: Complete Success
Outcome:gcs=G111 (all consent granted), npa=0 or no npa parameter
React Lifecycle Timing
Initial implementation used useState and useEffect to check consent
React state initialization is asynchronous
GA4 scripts loaded via Next.js Script component
No guaranteed execution order
Script Loading Strategy
Multiple <Script> components with strategy="afterInteractive"
All scripts in this category load simultaneously
Consent initialization script raced with gtag.js and config scripts
gtag('config') often fired before gtag('consent', 'update')
Default Consent Mode
Google Analytics defaults to "denied" for privacy compliance
When pageview fired before consent update, it sent npa=1
Google's servers received "denied" signal and discarded data