DEV Community

IntelligenceX
IntelligenceX

Posted on

Google Consent Mode v2 in React and Next.js: A Practical Developer Guide

If your React or Next.js application uses Google Analytics, Google Ads or Google Tag Manager, you need to understand how consent signals affect Google tag behaviour.

Google Consent Mode v2 provides a way to communicate visitor preferences to supported Google tags.

It does not display a cookie banner or collect consent. Your CMP or custom consent interface must do that separately.

Let's implement the integration step by step.

1. Understand the four important consent signals

Signal Controls
ad_storage Advertising-related storage
analytics_storage Analytics-related storage
ad_user_data User data sent to Google for advertising
ad_personalization Personalised advertising

Google also supports functionality_storage, personalization_storage and security_storage.

2. Choose Basic or Advanced Consent Mode

Basic mode

  • Google tags remain blocked until the visitor interacts with the consent interface.
  • Tags load only when the required consent is granted.
  • No Google tag data is transmitted before the tags are allowed to load.

Advanced mode

  • Google tags load with configured default states.
  • Denied consent can result in cookieless pings from supported tags.
  • Tag behaviour changes when the visitor updates their preferences.

Choose based on your organisation's privacy requirements and measurement strategy.

3. Establish default consent

For a direct gtag.js implementation, set defaults before loading the Google tag.

window.dataLayer = window.dataLayer || [];

function gtag() {
  window.dataLayer.push(arguments);
}

gtag("consent", "default", {
  ad_storage: "denied",
  analytics_storage: "denied",
  ad_user_data: "denied",
  ad_personalization: "denied",
  wait_for_update: 500
});
Enter fullscreen mode Exit fullscreen mode

These are illustrative defaults. Your regional requirements and consent configuration determine the actual values.

4. Next.js App Router implementation

Next.js supports beforeInteractive for critical scripts that need early loading.

An example root layout:

import Script from "next/script";

export default function RootLayout({ children }) {
  return (
    <html lang="en">
      <body>
        {children}

        <Script
          id="consent-defaults"
          strategy="beforeInteractive"
        >
          {`
            window.dataLayer = window.dataLayer || [];
            function gtag(){dataLayer.push(arguments);}

            gtag('consent', 'default', {
              ad_storage: 'denied',
              analytics_storage: 'denied',
              ad_user_data: 'denied',
              ad_personalization: 'denied',
              wait_for_update: 500
            });
          `}
        </Script>
      </body>
    </html>
  );
}
Enter fullscreen mode Exit fullscreen mode

This establishes consent defaults. Install the actual Google tag separately and follow Google's instructions for loading and ordering the scripts.

5. Update consent after user interaction

Suppose a visitor accepts analytics but rejects advertising.

gtag("consent", "update", {
  analytics_storage: "granted",
  ad_storage: "denied",
  ad_user_data: "denied",
  ad_personalization: "denied"
});
Enter fullscreen mode Exit fullscreen mode

In production, these values should come from your CMP's actual saved preferences.

A simplified React adapter:

"use client";

import { useEffect } from "react";

export function ConsentSync({ consent }) {
  useEffect(() => {
    if (!consent) return;

    window.gtag?.("consent", "update", {
      analytics_storage: consent.analytics
        ? "granted" : "denied",
      ad_storage: consent.adStorage
        ? "granted" : "denied",
      ad_user_data: consent.adUserData
        ? "granted" : "denied",
      ad_personalization: consent.adPersonalization
        ? "granted" : "denied"
    });
  }, [consent]);

  return null;
}
Enter fullscreen mode Exit fullscreen mode

This is a simplified adapter. Connect it to your CMP's documented event or state API, and ensure it does not send incomplete initial values.

6. Google Tag Manager implementation

If you use GTM, check whether your CMP provides an official GTM consent template.

A typical setup:

  1. Add the CMP template.
  2. Configure default consent states.
  3. Use the Consent Initialization - All Pages trigger.
  4. Map CMP categories to Google consent types.
  5. Configure the relevant Google tags.
  6. Test with GTM Preview and Tag Assistant.

When developing custom GTM consent templates, Google recommends using setDefaultConsentState and updateConsentState instead of gtag("consent", ...) inside the template.

The reason is processing order: the Tag Manager consent APIs are designed to update consent within GTM's event lifecycle.

7. Handle React SPA navigation

A React application may navigate without reloading the document.

Your implementation should:

  • Initialise consent at application level.
  • Restore saved preferences.
  • Update Google signals immediately after a visitor changes preferences.
  • Avoid duplicate initialisation.
  • Handle virtual page views separately.
  • Respect withdrawal.

Google specifically recommends processing consent updates on the page where the choice is made, before navigation.

Do not wait for the next page load to communicate the change.

8. Test the implementation

Use Google Tag Assistant and browser Developer Tools.

Scenario Expected result
Before consent Defaults are applied
Analytics accepted analytics_storage becomes granted
Advertising rejected Advertising signals remain denied
All optional consent rejected Signals reflect the saved choice
Consent withdrawn Updated states are applied
SPA route change Consent state remains correct

Also check whether your selected Basic or Advanced implementation behaves as expected.

9. Common mistakes

  • Setting defaults after Google tags start processing.
  • Granting all signals after a single category acceptance.
  • Using a normal GTM page-view trigger for consent initialisation.
  • Forgetting saved preferences on return visits.
  • Confusing script loading with consent enforcement.
  • Assuming Google Consent Mode blocks unrelated third-party scripts.

Conclusion

A reliable Consent Mode v2 implementation needs three components:

  1. Correct default consent states.
  2. Accurate updates from the visitor's actual choices.
  3. Testing across page loads and SPA navigation.

A CMP can simplify consent collection and integration, but the developer still needs to verify the resulting tag behaviour.

Explore ConsentX's Google Consent Mode v2 feature and integration documentation.

Official references

Top comments (0)