This page is for developers that maintain Google tags on a website and want to integrate consent mode. For an introduction to consent mode, read Consent mode overview.
How you enable and use consent mode depends on your implementation for obtaining consent and which tagging platform (Google Tag Manager (GTM) or the Google tag) you use:
- Consent Management Platforms (CMPs) that support Google consent mode
provide:
- Tag Manager templates in the Community Template Gallery that you should use to create tags for managing consent.
- JavaScript code for sites using gtag.js to manage consent.
- For custom implementations and CMPs that do not support consent mode:
- If you use GTM, we recommend creating your own template using Tag Manager consent APIs. The following example is available to reference as a starting point.
- If you use gtag.js, you will have to manually add consent code to each page of your site as direct commands or in a custom HTML snippet.
This article outlines best practices and provides API examples. See related links for more information.
Before you begin
Take the following into consideration before implementing consent mode:
It is best practice to scope the default consent settings to the regions where you are surfacing consent banners to your visitors. This helps with preserving measurement in regions where consent banners are required and Google tags adjust their behavior accordingly. You also prevent any loss of measurement where there are no consent banners or consent banners don't apply. See Region-specific behavior.
If you use a CMP, consent update commands must be configured to target visitors from the same regions specified in the default consent command. This will give users the opportunity to update their consent state if it is set to denied by default.
When writing your own templates or Custom HTML tags, any commands executed in callbacks or commands that use
gtag()
are not guaranteed to be available before the next trigger fires. To ensure that consent information is available as quickly as possible, use (or create) a tag template that uses the Tag Manager Consent APIs to set consent states.
Tracking consent state
The default consent state should immediately be set on page load according to the defaults your organization requires. The CMP or custom consent management solution should then prompt the visitor to grant or deny consent for the applicable consent types. Because consent mode does not store consent choices, it is also imperative that the consent management solution issue a consent mode update command based on the user's consent choices as early as possible on every page after consent is given.
gtag.js
The following sections provide examples of using gtag.js to:
Set consent defaults
We recommend setting a default value for each consent type you are using. The consent state values in this article are only examples. You are responsible for making sure that default consent mode is set for each of your measurement products to match your organization's policy.
To adjust the default measurement capabilities, call the gtag('consent',
'default', ...)
command on every page of your site before any commands that
send measurement data (such as config
or event
). For example, to deny
multiple consent types by default, specify the following consent
parameters:
gtag('consent', 'default', {
'ad_storage': 'denied',
'ad_user_data': 'denied',
'ad_personalization': 'denied',
'analytics_storage': 'denied'
});
Update consent state
When users specify consent, or change their consent choice, update
the consent status using the update
command. Since consent mode doesn't save
consent choices, update the consent status as soon as a user interacts with
your consent management solution.
After a user grants consent, persist their choice and call the update
command accordingly on subsequent pages.
In the example below, only the ad_storage
value was changed. If
analytics_storage
was set to denied
, it would still be denied after this
call. It is up to you to ensure the correct values are set for all consent
types. For full details on supported types, read the API reference.
The following code example shows how to update the consent status to
granted
when user agrees to allow advertising cookies:
<script>
function consentGrantedAdStorage() {
gtag('consent', 'update', {
'ad_storage': 'granted'
});
}
</script>
<!-- Invoke your consent function when a user interacts with your banner -->
<body>
...
<button onclick="consentGrantedAdStorage()">Yes</button>
...
</body>
Integrate with asynchronous consent management platforms
If your CMP loads asynchronously, it might not always run before your Google
Tags. To handle such situations, specify wait_for_update
along with a
millisecond value to control how long to wait before data is sent.
For example, to deny ad_storage
on a particular page by default, but to
allow your CMP to update consent status, use wait_for_update
. In the
following code, ad_storage
defaults to denied
, and the consent tool is
given 500 milliseconds to call gtag('consent', 'update', ...)
before tags
fire:
gtag('consent', 'default', {
'ad_storage': 'denied',
'wait_for_update': 500
});
Tag Manager
For Tag Manager implementations, we recommend using a consent mode template from the Community Template Gallery to manage consent. See Create a consent mode template for information on creating your own template using Tag Manager consent APIs.
Notes:
Consent mode implementations on sites that leverage GTM for tagging should use the GTM-specific APIs for managing consent states,
setDefaultConsentState
andupdateConsentState
. ThegtagSet
API can be used to optionally set theads_data_redaction
and URL passthrough settings as appropriate.The
gtag('consent','update',...)
method should not be used instead ofupdateConsentState
because it would be queued after all other pending messages and may not be processed before the next event begins. For more information, see How data layer information is processed.
Implementation example
The following example sets multiple consent mode parameters to denied
by default. After a user
indicates their consent choices, the relevant parameters are updated to
granted
.
gtag.js
The order of the code here is vital. If your consent code is called out of order, consent defaults will not work. Depending on business requirements, specifics may vary, but in general, code should run in the following order:
Load the Google tag. This is your default snippet code. The default snippet should be updated (see below) to include a call to
gtag('consent', 'default', ...)
.Load your consent solution. If your consent solution loads asynchronously, see Integrate with asynchronous consent management platforms for how to make sure this happens in the correct order.
If not handled by your consent solution, call
gtag('consent', 'update', ...)
after the user indicates consent.
<script>
// Define dataLayer and the gtag function.
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
// Set default consent to 'denied' as a placeholder
// Determine actual values based on your own requirements
gtag('consent', 'default', {
'ad_storage': 'denied',
'ad_user_data': 'denied',
'ad_personalization': 'denied',
'analytics_storage': 'denied'
});
</script>
<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=TAG_ID">
</script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'TAG_ID');
</script>
<!-- Create one update function for each consent parameter -->
<script>
function consentGrantedAdStorage() {
gtag('consent', 'update', {
'ad_storage': 'granted'
});
}
</script>
<!-- Invoke your consent functions when a user interacts with your banner -->
<body>
...
<button onclick="consentGrantedAdStorage">Yes</button>
...
</body>
Tag Manager
For sites using Tag Manager, we recommend using a CMP to handle updates of visitor consent choices. CMPs provide templates in the Community Template Gallery to create a tag for managing consent mode.
If using a template is not possible, you can instead update the code on your page as follows. The order of the code here is vital. If your consent code is called out of order, consent defaults will not work.
<script>
// Define dataLayer and the gtag function.
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
// Set default consent to 'denied' as a placeholder
// Determine actual values based on your own requirements
gtag('consent', 'default', {
'ad_storage': 'denied',
'ad_user_data': 'denied',
'ad_personalization': 'denied',
'analytics_storage': 'denied'
});
</script>
<!-- Google Tag Manager -->
<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','GTM-XXXXXX');</script>
<!-- End Google Tag Manager -->
<!-- Create one update function for each consent parameter -->
<script>
function consentGrantedAdStorage() {
gtag('consent', 'update', {
'ad_storage': 'granted'
});
}
</script>
<!-- Invoke your consent functions when a user interacts with your banner -->
<body>
...
<button onclick="consentGrantedAdStorage()">Yes</button>
...
</body>
Upgrade to consent mode v2
As a part of Google's ongoing commitment to a privacy-centric digital advertising
ecosystem, we are strengthening the enforcement of our EU user consent policy.
Consent mode users need to send two new parameters in
addition to ad_storage
and analytics_storage
:
Field Name | Allowed Values | Description |
---|---|---|
ad_user_data
|
'granted' | 'denied'
|
Sets consent for sending user data related to advertising to Google. |
ad_personalization
|
'granted' | 'denied'
|
Sets consent for personalized advertising. |
Advanced consent features
Advanced consent features include the ability to:
- Set behavior for a geographic region.
- Pass ad click, client ID, and session ID information in URLs when users have not granted consent for cookies.
- Fully redact (remove) ad information when users deny consent for ad cookies.
Region-specific behavior
To change the default behavior of your tags for users from certain regions, specify a region in your consent command. By providing a region value, you can fine-tune defaults based on your users' geographic locations. See Geographical IDs for more information on identifying regions.
gtag.js
The following example sets analytics_storage
to denied
for users from
Spain and Alaska, and sets ad_storage
to denied
for all users.
gtag('consent', 'default', {
'analytics_storage': 'denied',
'region': ['ES', 'US-AK']
});
gtag('consent', 'default', {
'ad_storage': 'denied'
});
Tag Manager
If you are using a template to create your tag, it may have the controls to set region-specific behaviour. If you are building a template tag on your own, see Create a consent mode template for more information on setting region-specific behavior.
Most specific parameter takes precedence
If two default consent commands occur on the same page with values for a
region and subregion, the one with a more specific region will take effect.
For example, if you have ad_storage
set to granted
for the region US and
ad_storage
set to denied
for the region US-CA, a visitor from California
will have the more specific US-CA setting take effect. For this example,
that would mean a visitor from US-CA would have ad_storage
set to
denied
.
Region | ad_storage |
Behavior |
---|---|---|
US | 'granted' |
Applies to users in the US that are not in CA |
US-CA | 'denied' |
Applies to users US-CA |
Unspecified | 'granted' |
Uses the default value of 'granted' . In the example, applies to visitors that aren't in the US or in US-CA
|
Pass through ad click, client ID, and session ID information in URLs
When a user lands on your website after clicking an ad, information about the ad may be appended to your landing page URLs as a query parameter. In order to improve conversion accuracy, this information is usually stored in first-party cookies on your domain.
However, if ad_storage
is set to denied
, this information will not be stored
locally. To improve ad click measurement quality when ad_storage
is denied
,
you can optionally elect to pass information about ad clicks through URL
parameters across pages using URL passthrough.
Similarly, if analytics_storage
is set to denied
, URL passthrough can be
used to send event and session-based analytics (including conversions) without
cookies across pages.
The following conditions must be met in order to use URL passthrough:
- Your Google tag is consent-aware and present on the page.
- The advertiser has enabled the URL passthrough feature.
- Consent mode is implemented on the page.
- The outgoing link refers to the same domain as the current page's domain.
A GCLID or DCLID is present in the URL (Google Ads and Floodlight tags only)
gtag.js
To enable this capability, set the url_passthrough
parameter to true
:
gtag('set', 'url_passthrough', true);
Tag Manager
If you are using a template to create your tag, it may have the controls to
set URL passthrough. If you are building a template tag on your own, see
Create a consent mode template for more information on setting URL
passthrough using the gtagSet
custom template API.
Or, you can use the following options to set it in Conversion Linker and/or analytics tags
For Google Ads and Floodlight tags:
To enable this capability, create (or use an existing) conversion linker tag and ensure Enable linking on all page URLs is checked. See basic setup for instructions on how to create a conversion linker tag.
For Google Analytics tags:
- In Tag Manager, navigate to Fields to Set:
- For Google Analytics: GA4 Configuration tags, select Tag Configuration > Fields to Set.
- For Google Analytics: Universal Analytics tags, use a Google Analytics settings variable and select Variable Configuration > More Settings > Fields to Set.
- When the Fields to Set section is expanded, click Add Row.
- For Field Name, enter the correct value:
- For Google Analytics: GA4 Configuration tags, enter
url_passthrough
. - For Google Analytics: Universal Analytics tags using the Google
Analytics settings variables, enter
urlPassthrough
.
- For Google Analytics: GA4 Configuration tags, enter
- For Value, enter 'true'.
- Save the tag and publish.
Alternatively, you can set the url_passthrough
parameter to true
on
every page of your site before the GTM install snippet.
window.dataLayer = window.dataLayer || [];
function gtag(){window.dataLayer.push(arguments);}
gtag('set', 'url_passthrough', true);
When using URL passthrough, a few query parameters may be appended to links as users navigate through pages on your website:
gclid
dclid
gclsrc
_gl
wbraid
For best results, ensure that:
- Redirects on your site pass all the above query parameters.
- Your analytics tools ignore these parameters in page URLs.
- These parameters do not interfere with your site behavior.
Redact ads data
When ad_storage
is denied
, new cookies will not be set for advertising
purposes. Additionally, third-party cookies previously set on google.com and
doubleclick.net will not be used except for spam and fraud purposes. Data sent
to Google will still include the full page URL, including any ad click
information in the URL parameters.
gtag.js
To further redact your ads data when ad_storage
is denied
, set
ads_data_redaction
to true
.
gtag('set', 'ads_data_redaction', true);
When ads_data_redaction
is true
and ad_storage
is denied
, ad click
identifiers sent in network requests by Google Ads and Floodlight tags will
be redacted. Network requests will also be sent through a cookieless domain.
Tag Manager
If you are using a template to create your tag, it may have the controls to further redact ads data. If you are building a template tag on your own, see Create a consent mode template for more information on redacting ads data.
Google Tag Manager consent configuration features
Tag Manager includes several features that work together to help you manage how tags behave in response to consent settings. Tag Manager features a consent initialization trigger, tag settings for consent management, and a Consent Overview page. Several third-party consent management providers have built integrations with consent mode into their products. Learn more about Tag Manager consent features.
Verify consent settings
To verify and debug your consent mode configuration we recommend using Tag Assistant. Tag Assistant lets you see whether and how consent state is being set and updated. Tag Assistant supports the following:
- gtag consent commands
- A tag created from a Tag Manager Consent Mode template
- Transparency and Consent Framework (TCF)
Learn more about Tag Assistant consent mode debugging.
If you don't use a consent mode template, you can analyze your consent settings with the browser's developer tools.
To analyze your consent settings:
In the Elements tab, type
dataLayer
in the search bar. Confirm that:- The
default
command comes before all other Google events. - Consent states are set according to the user's interaction and are set
with the
update
command. - Tags fire only when the required consent settings are met.
- Both
ads_data_redaction
andurl_passthrough
are set according to the settings the user provided.
- In the Network tab:
- Check for the
gcs=
parameter on URLs for consent status. Thegcs
parameter has the following format:gcs=G1 [ad_storage][analytics_storage]
. - Check for the correct
gcs=
parameter values based on the provided consent. The value forad_storage
andanalytics_storage
will be one of the following:
Value Meaning G100 Consent is denied for both ad_storage
andanalytics_storage
.G110 Consent is granted for ad_storage
and denied foranalytics_storage
.G101 Consent is denied for ad_storage
and granted foranalytics_storage
.G111 Consent for both ad_storage
andanalytics_storage
is granted.G1-- The site did not require consent for ad_storage
oranalytics_storage
.- Verify that cookieless domains (for example googlesyndication.com) are
used when
ads_data_redaction
is set totrue
. - Verify that
gclid
/dclid
is appended to outgoing URLs whenurl_passthrough
is set totrue
and that the_gl
linker parameter is present (for examplehttps://www.example.com/?_gl=1*abcde5*
).
- The
Legacy tag controls
If you use legacy tags, such as ga.js, analytics.js, or conversion.js, you should update to gtag.js or Google Tag Manager.
To learn more about other legacy tag's privacy controls, see the following documentation: