Introduction
Updated for 2026 compliance practices.
For website owners using React, implementing Google Consent Mode v2 is a critical step toward aligning with GDPR requirements for user consent. This React consent mode v2 setup guide provides a practical, technical walkthrough to help you configure consent signals correctly, avoid common pitfalls, and verify your implementation using GDPRChecker’s scanning tools. While this guide focuses on the technical aspects, it does not constitute legal advice; you should consult with a qualified privacy professional to ensure full compliance with applicable regulations.
Google Consent Mode v2 introduces two new consent signals—ad_user_data and ad_personalization—in addition to the existing analytics_storage and ad_storage. These signals allow you to control how Google tags behave based on user consent choices, enabling a more granular approach to data collection. In a React application, integrating Consent Mode requires careful handling of state management, tag loading, and banner interactions. This guide will walk you through the entire process, from understanding the requirements to validating your setup with GDPRChecker.
What React Consent Mode v2 Setup Means for Website Owners
React consent mode v2 setup guide is a practical compliance topic for website owners validating consent, tags, and disclosures. For React developers and site operators, this means ensuring that your application communicates user consent preferences to Google services before any tags fire. Unlike traditional websites where consent mode might be implemented via a simple script in the <head>, React’s dynamic rendering and state-driven architecture demand a more integrated approach. You need to synchronize your consent management platform (CMP) with React’s state, conditionally load Google tags, and update consent states without full page reloads.
The core challenge is that Google tags (such as Google Analytics 4, Google Ads, or Floodlight) must respect the user’s consent decision from the very first page load. If your React app loads these tags before the consent state is determined, you risk sending data without proper consent, which can lead to compliance gaps. Consent Mode v2 addresses this by allowing tags to adjust their behavior based on the consent signals, but only if the implementation is correct. For website owners, this setup is not just a technical checkbox; it’s a foundational step in building trust with users and demonstrating accountability under GDPR.
Requirements and Compliance Expectations
Before diving into the code, it’s essential to understand the compliance landscape. Under GDPR, consent must be freely given, specific, informed, and unambiguous. This means your React application must present a cookie banner or consent widget that clearly explains what data is collected and for what purposes, and it must allow users to make granular choices. Google Consent Mode v2 does not replace a CMP; rather, it works alongside it to translate user choices into signals that Google tags understand.
Key requirements include: - Prior consent: No Google tags that set cookies or collect personal data should fire before the user has made a choice, unless they are configured to behave in a consent-aware manner (e.g., using Consent Mode’s default state). - Granular control: Users must be able to accept or reject specific purposes, such as analytics or advertising. Your React state must reflect these choices accurately. - Respect for withdrawal: Users must be able to change their consent preferences at any time, and your app must update the consent signals accordingly. - Documentation: You should maintain records of consent, which is typically handled by your CMP, but your technical implementation must support this.
From a technical standpoint, Google requires that Consent Mode v2 be implemented for continued use of certain advertising features in the European Economic Area (EEA). The official Google documentation (see Google Consent Mode) outlines the necessary signals and default behaviors. Additionally, the European Data Protection Board (EDPB) provides guidance on valid consent mechanisms, and resources like GDPR.eu offer overviews of the regulation.
How to Implement React Consent Mode v2 Step by Step
Implementing Consent Mode v2 in a React application involves several steps, from setting up your CMP to integrating with Google Tag Manager (GTM) or gtag.js. Below is a detailed, step-by-step approach.
Step 1: Choose and Configure a Consent Management Platform
First, you need a CMP that supports Google Consent Mode v2. While this guide does not endorse any specific CMP, ensure that yours can fire a callback or update a global variable when the user makes a consent choice. Many CMPs provide a JavaScript API that you can use to read the current consent state. In React, you’ll typically store this state in a context or a state management library like Redux or Zustand.
Example of initializing a CMP and reading consent:
// Inside your React app, after CMP script loads
const [consent, setConsent] = useState({
ad_storage: 'denied',
ad_user_data: 'denied',
ad_personalization: 'denied',
analytics_storage: 'denied',
});
useEffect(() => {
// Assuming your CMP exposes a function to get consent
window.getConsentState = (state) => {
setConsent(state);
};
}, []);Step 2: Set Default Consent State
Before any tags load, you must set the default consent state. In a React app, this can be done by including a small inline script in your index.html that sets window.dataLayer or calls gtag('consent', 'default', {...}). However, because React controls the DOM, you need to ensure this script runs before React hydrates. Place it in the <head> of your HTML template.
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('consent', 'default', {
'ad_storage': 'denied',
'ad_user_data': 'denied',
'ad_personalization': 'denied',
'analytics_storage': 'denied',
'wait_for_update': 500,
});
</script>The wait_for_update parameter tells Google tags to wait for an update signal before firing, which is useful if your CMP loads asynchronously. Set this to a reasonable value (e.g., 500 milliseconds) to balance user experience and compliance.
Step 3: Load Google Tags Conditionally
In React, you should load Google Tag Manager or gtag.js only after the default consent state is set. You can use a custom hook or a component that injects the script dynamically. For GTM, you might do:
useEffect(() => {
const script = document.createElement('script');
script.src = 'https://www.googletagmanager.com/gtag/js?id=GA_MEASUREMENT_ID';
script.async = true;
document.head.appendChild(script);
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'GA_MEASUREMENT_ID');
}, []);Ensure this runs after the default consent script. If you’re using GTM, you can load the GTM container similarly, but be aware that GTM will respect the consent signals if they are set before it loads.
Step 4: Update Consent State on User Action
When the user interacts with your consent banner (e.g., clicks “Accept All” or “Reject All”), your CMP should trigger a callback. In React, update your consent state and call gtag('consent', 'update', {...}) with the new values.
const handleConsentUpdate = (newConsent) => {
setConsent(newConsent);
gtag('consent', 'update', {
'ad_storage': newConsent.ad_storage,
'ad_user_data': newConsent.ad_user_data,
'ad_personalization': newConsent.ad_personalization,
'analytics_storage': newConsent.analytics_storage,
});
};If you’re using GTM, you can push the update to the dataLayer instead, and GTM will handle the rest. The key is that the update must happen immediately after the user’s choice.
Step 5: Handle Page Navigation and State Persistence
React’s single-page application (SPA) nature means that page navigations do not trigger full reloads. You must ensure that consent state persists across route changes. Store the consent preferences in localStorage or a cookie, and reapply them on each route change. Additionally, if you’re using Google Analytics 4 with Consent Mode, you may need to send a new page view event on route changes, which will automatically respect the current consent state.
// Example with React Router
useEffect(() => {
const savedConsent = localStorage.getItem('userConsent');
if (savedConsent) {
const parsed = JSON.parse(savedConsent);
setConsent(parsed);
gtag('consent', 'update', parsed);
}
}, []);
// On route change
useEffect(() => {
gtag('config', 'GA_MEASUREMENT_ID', { page_path: location.pathname });
}, [location]);Step 6: Test with Google’s Tag Assistant
Before moving to validation with GDPRChecker, use Google’s Tag Assistant or the browser’s developer tools to verify that consent signals are being sent correctly. Check the network tab for requests to Google domains and ensure that when consent is denied, no cookies are set, and requests are modified accordingly (e.g., with gcs=G100 parameter).
Common Mistakes and How to Avoid Them
Even experienced developers can make mistakes when implementing React consent mode v2. Here are some of the most common pitfalls and how to steer clear of them.
Mistake 1: Loading Tags Before Default Consent
If your Google tags load before the default consent script, they may fire with full data collection capabilities, ignoring the consent state. To avoid this, place the default consent script as high as possible in the <head>, and ensure that your React app does not load any Google scripts until after that script has executed. In a React context, this often means using a synchronous script tag in the HTML template rather than dynamically injecting it via JavaScript.
Mistake 2: Not Handling Reject-Flow Properly
Many implementations focus on the “Accept All” path but neglect the “Reject All” or granular rejection flows. When a user rejects all, you must update the consent state to 'denied' for all signals. Failure to do so can result in continued data collection, which is a serious compliance gap. Test the reject flow thoroughly, and ensure that your CMP correctly communicates the denied state to your React app.
Mistake 3: Ignoring SPA Navigation
In a traditional multi-page website, each page load triggers a fresh consent check. In a React SPA, you must manually reapply consent on route changes. If you forget to do this, Google tags might revert to default behavior or lose the consent state. Implement a route change listener that re-sends the consent update or reconfigures tags.
Mistake 4: Overlooking wait_for_update
The wait_for_update parameter is crucial for asynchronous CMPs. If you set it too low, tags might fire before the user’s choice is registered. If you set it too high, you may delay tag loading unnecessarily. A value between 300 and 500 milliseconds is typically sufficient, but you should test with your specific CMP’s load time.
Mistake 5: Relying Solely on Client-Side State
Consent preferences should be persisted in a way that survives page refreshes and browser restarts. Using only React state without localStorage or a cookie means that if the user refreshes the page, they might see the banner again or lose their settings. Always sync consent to persistent storage and rehydrate on app initialization.
How to Validate with GDPRChecker
After implementing React consent mode v2, validation is essential to ensure that your setup works as intended and that no compliance gaps remain. GDPRChecker scans help verify pre-consent network requests, banner behavior, and disclosure gaps after changes. Here’s how to use GDPRChecker to validate your implementation:
- Run a Pre-Consent Scan: Use GDPRChecker to scan your React site before any consent is given. The scanner will check for network requests to third-party domains, including Google, and flag any that occur before consent. This helps you confirm that your default consent state is effectively blocking data collection.
- Test Consent Interactions: Interact with your consent banner (accept all, reject all, granular choices) and run scans after each action. GDPRChecker will verify that the appropriate consent signals are being sent and that tag behavior changes accordingly.
- Check for Disclosure Gaps: The scanner can identify missing or incomplete disclosures in your cookie banner or privacy policy. Ensure that your banner clearly lists all purposes and that your privacy policy is up to date.
- Validate Across Pages: Since React is an SPA, run scans on multiple routes to ensure that consent state persists and that no new, unexpected requests appear on navigation.
- Schedule Regular Scans: Compliance is not a one-time task. Set up recurring scans with GDPRChecker to catch any regressions after updates to your app, CMP, or Google tags.
For a deeper dive into related topics, see our guides on Google Analytics GDPR compliance and the Google Consent Mode v2 guide. If you’re unsure whether you need a CMP, read Do I need a CMP if I do not run Google Ads?.
Implementation Checklist
Use this checklist to ensure your React consent mode v2 setup is complete and compliant:
- Default consent state set to 'denied' for all signals in the HTML
<head>. - Google tags (gtag.js or GTM) loaded only after default consent script.
- CMP integrated and configured to fire consent update callbacks.
- React state synchronized with CMP consent choices.
- Consent update function calls
gtag('consent', 'update', {...})with correct values. - Consent preferences persisted in localStorage or a cookie.
- On app initialization, rehydrate consent state from storage and reapply.
- Route change listener re-sends consent update or reconfigures tags.
- Reject-all flow tested and verified to set all signals to 'denied'.
- Granular consent choices (e.g., only analytics) correctly reflected in signals.
- Pre-consent network requests verified with GDPRChecker scan.
- Cookie banner and privacy policy disclosures reviewed for completeness.
FAQ
What is React consent mode v2 setup guide?
React consent mode v2 setup guide is a practical compliance topic for website owners validating consent, tags, and disclosures. It provides step-by-step instructions for integrating Google Consent Mode v2 into a React application, ensuring that user consent choices are respected by Google tags.
Do I need React consent mode v2 setup guide for GDPR?
If your React site uses Google services like Analytics or Ads and targets users in the EEA, implementing Consent Mode v2 is a key technical measure for GDPR compliance. This guide helps you configure it correctly to avoid unauthorized data collection.
How do I implement React consent mode v2 setup guide?
Implementation involves setting default consent states, conditionally loading Google tags, updating consent on user action, and handling SPA navigation. Detailed steps are provided in this guide, along with code examples and common pitfalls.
How can I verify React consent mode v2 setup guide with a scanner?
GDPRChecker scans can verify your setup by checking pre-consent network requests, banner behavior, and disclosure gaps. Run scans before and after consent interactions to ensure tags behave as expected.
What are common React consent mode v2 setup guide mistakes?
Common mistakes include loading tags before default consent, neglecting reject-flow testing, ignoring SPA navigation state, misconfiguring wait_for_update, and failing to persist consent. This guide explains how to avoid each.
Conclusion
Implementing React consent mode v2 is a nuanced but essential task for GDPR compliance. By following this React consent mode v2 setup guide, you can ensure that your React application correctly communicates user consent to Google services, minimizing the risk of non-compliance. Remember to validate your implementation with GDPRChecker’s scanning tools to catch any gaps. For further reading, explore our guides on Consent Mode v2 vs Google Certified CMP, the Google Consent Mode v2 checker, and cookie banner requirements. Start your scan today at GDPRChecker to close the consent mode gap and build a more privacy-respecting website.
Next step
Run a GDPRChecker scan to validate consent behavior, trackers, and disclosures after you implement the checklist above.
This guide is technical implementation guidance for website owners. It is not legal advice.
Copyright and editorial notice
© GDPRChecker
This original AI-assisted editorial draft was selected, reviewed, and published by GDPRChecker. All rights are reserved where protected by applicable law. Do not reproduce the article without permission.