Sub topic 11
Set Consent Defaults Before the Tag Sends Data
This chapter covers consent mode basics for the Google tag. It assumes you have a GA4 property with a web data stream, you know your Measurement ID, and you have the Google tag installed using one of the methods from earlier chapters. You should be comfortable reading a small JavaScript snippet and knowing where the <head> of your page is.
By the end, you should be able to set a default consent state that runs before the tag sends data, update it when a visitor makes a choice, and check that it works.
Why consent defaults matter
Privacy rules such as the GDPR and ePrivacy rules in the European Economic Area, and similar laws elsewhere, can require you to get a visitor's permission before you store or read data on their device. Analytics cookies are a common example.
The Google tag does not decide what the law requires of you. It can, however, be told what the visitor has agreed to. That information is called the consent state. When the tag knows the state, it changes how it behaves.
The key point is timing. The tag starts working as soon as it loads. If it runs before it knows the visitor's choice, it may set cookies or send data too early. A consent default solves this. It is a starting state you declare before the config call, so the tag respects it from the first page load.
This chapter shows the technical mechanism. It is not legal advice. Ask a qualified person what your site must do in each region where you have visitors.
What consent mode is
Consent mode is a set of signals that your site passes to Google tags. Each signal says whether a certain kind of storage or data use is allowed. The signals are called consent types. Each one has one of two values: granted or denied.
The four consent types used in this chapter are:
| Consent type | What it controls |
|---|---|
| `analytics_storage` | Whether the tag may read and write analytics cookies, such as the cookies that identify a returning visitor. |
| `ad_storage` | Whether the tag may store advertising cookies. |
| `ad_user_data` | Whether user data may be sent to Google for advertising purposes. |
| `ad_personalization` | Whether data may be used for personalized advertising, such as remarketing. |
A site that only uses GA4 for measurement still sets all four. Other Google products may share the same tag, and you may link advertising products later. Setting all four from the start avoids a gap.
When a consent type is denied, Google tags change what they do. Tags that respect consent mode do not use cookies for that purpose. Depending on the setup, the tag can still send cookieless pings. These are signals that contain no persistent identifier. Google can use them to model some of the behavior that cannot be observed directly. The exact behavior depends on your property settings and the volume of data, so treat modeling as an addition to your reports, not a way to recover all missing data.
The sequence: default, then update
Consent works as two calls to gtag.
- Default. You declare the starting state. This runs on every page load, before the tag sends anything.
- Update. After the visitor makes a choice in your consent banner, you send a new state. It replaces the matching values in the default.
The order matters. If the config call runs before the default, the tag starts without knowing the rules.
Write the default
The following code sets a default that denies everything. It is the same pattern from the topic's code examples, with the config call placed after it.
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'
});
// The gtag('config', 'G-XXXXXXXXXX') call comes after this default.The first two lines create the dataLayer array and the gtag function. If your page already has them, you do not need to repeat them, but the function must exist before you call it.
Place it in the page
In a manual installation, put the default in its own <script> block before the Google tag loads its configuration. A complete page head looks like this:
<head>
<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'
});
</script>
<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-XXXXXXXXXX"></script>
<script>
gtag('js', new Date());
gtag('config', 'G-XXXXXXXXXX');
</script>
</head>The default is queued in dataLayer first. When gtag.js loads, it reads the queue in order, so it sees the default before the config command.
Defaults for specific regions
Some sites deny by default only in certain regions and grant elsewhere. You can pass a region list of ISO 3166-2 codes. A command with a region applies to visitors in those regions. A command without a region is the fallback.
// Fallback for all other visitors
gtag('consent', 'default', {
ad_storage: 'granted',
ad_user_data: 'granted',
ad_personalization: 'granted',
analytics_storage: 'granted'
});
// Stricter default for the listed regions
gtag('consent', 'default', {
ad_storage: 'denied',
ad_user_data: 'denied',
ad_personalization: 'denied',
analytics_storage: 'denied',
region: ['ES', 'DE', 'FR']
});Use this only if you have decided, with proper advice, that different rules apply by region. A single strict default for everyone is simpler and safer when you are unsure.
Update after the visitor chooses
Your consent banner collects the visitor's choice. When it does, call gtag('consent', 'update', ...) with only the values that changed.
function onAnalyticsAccepted() {
gtag('consent', 'update', {
analytics_storage: 'granted'
});
}
function onAllRejected() {
gtag('consent', 'update', {
ad_storage: 'denied',
ad_user_data: 'denied',
ad_personalization: 'denied',
analytics_storage: 'denied'
});
}Two practical points:
- On later page views, the visitor has already made a choice. Your banner code must read the stored choice and call
updateagain on every page load. The default always runs first and starts asdenied, so the update has to follow it each time. - The update should run as soon as you know the choice. A short delay between the default and the update is normal. A long one means more events are sent under the denied state.
Wait for the banner on fast pages
If the banner loads slowly, the tag may send the first page view before the update arrives. The wait_for_update setting tells Google tags to hold briefly for an update, in milliseconds.
gtag('consent', 'default', {
ad_storage: 'denied',
ad_user_data: 'denied',
ad_personalization: 'denied',
analytics_storage: 'denied',
wait_for_update: 500
});Keep the value small. Five hundred milliseconds is a common choice. It helps when your banner reads a saved choice asynchronously, and it does not replace the need to call update.
Consent banners and platform integrations
Most sites do not write the banner themselves. There are three common cases.
- A consent management platform (CMP). Many CMPs integrate with Google consent mode. They set the default and send updates for you. Check that the CMP's default runs before your Google tag. Look at its documentation for a Google consent mode option and turn it on.
- A website platform plugin. Some platform integrations include consent settings, or they work with a consent plugin. Check the plugin's settings for how it orders the default and the tag. If your plugin injects the tag, you may not be able to put your own script in front of it. In that case, use the plugin's consent option rather than editing the page.
- Google Tag Manager. Tag Manager has a consent overview and built-in consent settings for tags. A CMP template can set the default in the very first trigger,
Consent Initialization - All Pages. That trigger fires before other tags, which gives the correct order.
Whichever route you use, do not set the default twice from two sources. Two conflicting defaults make the final state hard to predict. Pick one owner for consent, either your own code or the CMP.
Verify the consent state
Do not assume that the default is working. Check it.
- Open your site in a browser with the banner not yet answered. Use a private window so earlier choices do not interfere.
- Open Tag Assistant and connect to your site. Look at the consent section for the page. It shows the default state and any later update.
- Alternatively, open the browser's network tab and filter for
collect. Requests sent under denied analytics storage carry a consent parameter. Agcsvalue starting withG10generally means denied, andG11generally means granted. Treat this as a diagnostic hint, not as a contract. - Accept analytics in the banner and confirm that an update appears and that later requests show the granted state.
- Add
debug_mode: trueto the config call, as covered earlier in the DebugView chapter, so you can see the events in DebugView while you test.
gtag('config', 'G-XXXXXXXXXX', {
debug_mode: true
});Remove debug_mode when you finish testing.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Cookies are set before the visitor answers | The `config` call runs before the default, or the default is missing | Move the default above the Google tag, or fire it on `Consent Initialization` in Tag Manager |
| Data never reaches granted state | The banner never calls `update`, or it uses a different `gtag` function | Call `gtag('consent', 'update', ...)` from the banner handler |
| Returning visitors are counted as denied | The saved choice is not replayed on each page load | Read the stored choice and call `update` on every page |
| Conflicting states in Tag Assistant | A CMP and your own code both set a default | Keep one source for the default |
| Fewer users than expected | Many visitors deny analytics storage | Expect this. Review modeled data settings and your banner design |
| A typo such as `analytics_storages` | Unknown keys are ignored silently | Check the spelling of each consent type |
What consent mode does not do
- It does not remove your duty to explain data use. You still need a clear privacy notice and a banner that gives a real choice.
- It does not block other scripts on your page. It only informs Google tags. Other vendors need their own controls.
- It does not guarantee full recovery of missing data. Modeled data is an estimate.
Exercises
Exercise
Your page has gtag('config', 'G-XXXXXXXXXX') in the head, and a consent default placed in the footer. Visitors report that analytics cookies appear before they answer the banner. What is wrong, and how do you fix it?
Show answer
The default runs after the config call, so the tag starts without knowing the denied state. Move the gtag('consent', 'default', ...) script above the Google tag in the head, so it is queued in dataLayer before config.
Exercise
A visitor accepts analytics on Monday. On Tuesday, they return, and Tag Assistant shows analytics_storage as denied for the whole visit. The banner stores the choice in a cookie. What is the most likely cause?
Show answer
The default always starts as denied, and the banner code does not call gtag('consent', 'update', { analytics_storage: 'granted' }) on the new page load. Read the stored choice on every page and call update after the default. Optionally use wait_for_update so the first events wait for it.
Exercise
Name the four consent types in this chapter and say which one decides whether GA4 may use analytics cookies.
Show answer
The types are ad_storage, ad_user_data, ad_personalization, and analytics_storage. analytics_storage controls analytics cookies.