Google Consent Mode
Mirror consent into Google's dataLayer signals.
Google Consent Mode v2 is Google's way of letting tags run in a limited mode without consent. For this, it expects the consent state to be provided to it.
libconsent can automatically handle that for you, mirroring the consent state from your platform into the signals Google expects.
Set up
Pass the consentMode prop onto any <CookieService> that should run in Consent Mode. The adapter will handle the rest.
<CookieService id="google-tag-manager" consentMode>
<GoogleTagManager gtmId="GTM-XXXX" />
</CookieService>This will signal Google to only run the restricted mode while the user has not consented to the relevant categories, and allows it to fully run once consent is given. Never activate this option on a service that does not support Consent Mode, as it will load the full tracking even without consent!
The mapping
| Category | Signals |
|---|---|
analytics | analytics_storage |
marketing | ad_storage, ad_user_data, ad_personalization |
All four go out on every push, since Google reads a missing signal as denied.
Sending the defaults from <head>
The provider pushes consent default as early as React allows, which is still
after hydration begins. If a Google tag can start before that, send the defaults
from the document instead:
import { consentModeSnippet } from "@libconsent/core";
<head>
<script dangerouslySetInnerHTML={{ __html: consentModeSnippet() }} />
</head>;Sending default twice is harmless, so keeping both is fine.
On by default
The ConsentProvider, by default, writes the signals unless the platform already does. If you want to turn it off, pass consentMode={false}:
<ConsentProvider adapter={..} consentMode={false}>