Sub topic 4
Setting Consent Defaults and Updates
This chapter assumes that you know GA4 properties, events, cookies, and basic gtag.js or Google Tag Manager concepts. You also need a consent management platform (CMP). A CMP displays the consent banner and tells your Google tags which choices the visitor made.
Consent Mode v2 uses four signals:
analytics_storagecontrols analytics cookies and related measurement storage.ad_storagecontrols advertising cookies.ad_user_datacontrols whether user data can be sent to Google for advertising purposes.ad_personalizationcontrols personalized advertising features.
Your implementation has two stages. First, set a default before Google tags load. Then update the state after the visitor makes a choice.
Set a denied default before tags load
A denied default prevents Google tags from assuming consent. Place this code before the Google tag or Google Tag Manager container is loaded.
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
// Must run before any Google tag loads.
gtag('consent', 'default', {
ad_storage: 'denied',
ad_user_data: 'denied',
ad_personalization: 'denied',
analytics_storage: 'denied',
wait_for_update: 500
});The wait_for_update value gives the CMP time to provide a consent choice. The value is in milliseconds. A value of 500 means that Google tags can wait up to half a second for an update.
The default is not the visitor's final choice. It is the state used while the page waits for the CMP. If the CMP already has a saved choice, it should send the update as soon as possible.
Send an update after the visitor chooses
Call gtag('consent', 'update', ...) from the CMP callback. The values should reflect the visitor's actual choices. Do not send granted for every signal unless the visitor agreed to every related purpose.
This example represents a visitor accepting all purposes:
// Call this from your consent banner when the visitor accepts.
function onConsentAccepted() {
gtag('consent', 'update', {
ad_storage: 'granted',
ad_user_data: 'granted',
ad_personalization: 'granted',
analytics_storage: 'granted'
});
}For a visitor who accepts analytics but rejects advertising, send a mixed state:
function onAnalyticsOnlyAccepted() {
gtag('consent', 'update', {
analytics_storage: 'granted',
ad_storage: 'denied',
ad_user_data: 'denied',
ad_personalization: 'denied'
});
}Store the choice according to your CMP's design. On later page views, load the denied default first and then replay the stored choice. This avoids a short period in which tags run without a known state.
Apply stricter regional defaults
Some sites use different defaults by country or region. A regional default can override a general default when both apply. This lets you use a stricter setting in selected regions while using another default elsewhere.
// Stricter default for selected regions.
gtag('consent', 'default', {
ad_storage: 'denied',
ad_user_data: 'denied',
ad_personalization: 'denied',
analytics_storage: 'denied',
region: ['ES', 'DE', 'FR']
});
// Default for everywhere else.
gtag('consent', 'default', {
ad_storage: 'granted',
ad_user_data: 'granted',
ad_personalization: 'granted',
analytics_storage: 'granted'
});Use the country or subdivision codes supported by Google's consent configuration. Test regional behavior from an appropriate location or with your CMP's testing tools. A region rule does not replace the need for an update after the visitor makes a choice.
Common implementation errors
- Loading Google Tag Manager before the default consent command.
- Sending an update only when the visitor accepts, but not when the visitor rejects.
- Treating one consent category as permission for all four signals.
- Forgetting to replay a stored choice on later page views.
- Applying regional rules without testing the general default and regional override together.
Exercise
A visitor accepts analytics cookies but rejects advertising cookies. Which signals should be granted?
Show answer
Grant analyticsstorage. Keep adstorage, aduserdata, and ad_personalization denied.