Shipping Consent Mode v2 in five minutes
A minimal, production-ready setup for GA4 + Google Ads with Consent Mode v2 that you can drop into any Next.js project today.
Since March 2024 Google requires Consent Mode v2 for anyone running Ads in the EEA. The official docs are long and confusing. Here is the shape that actually works.
The defaults
Load gtag. Before sending anything, call gtag('consent', 'default', ...) with ad_storage, ad_user_data, ad_personalization and analytics_storage set to denied. Set functionality_storage and security_storage to granted. Add wait_for_update: 500 so pageview events are queued while the user decides.
gtag('consent', 'default', {
ad_storage: 'denied',
ad_user_data: 'denied',
ad_personalization: 'denied',
analytics_storage: 'denied',
functionality_storage: 'granted',
security_storage: 'granted',
wait_for_update: 500,
});
The opt-in
When the user accepts cookies, call gtag('consent', 'update', {...granted}). Persist the choice in localStorage so returning visitors do not see the banner again.
gtag('consent', 'update', {
ad_storage: 'granted',
ad_user_data: 'granted',
ad_personalization: 'granted',
analytics_storage: 'granted',
});
localStorage.setItem('cookie_consent', 'granted');
Conversion tracking
On the action that counts as a conversion (form submit, sign-up, purchase), send:
gtag('event', 'conversion', {
send_to: 'AW-XXXXXXX/abcDEF123',
});
Under Consent Mode, Google will still log it — just modeled instead of observed — if consent was not granted. You do not lose data.
The full snippet
We open-sourced the exact Analytics component we use on every client project. It is 90 lines, respects reduced motion and SSR, and is battle-tested in production. If you want a copy, reply to our newsletter welcome email and we will send it over.