How to use it
- Enter your Google tag ID if you have one: a
G-ID from Google Analytics, or aGTM-ID from Google Tag Manager. Pick when consent should be denied and which tag mode you want. - Edit the wording, button labels, position and colours. The preview updates as you type.
- Press Copy code and paste the whole block into the
<head>of every page, above any other Google tag. Remove your old tag snippet for the same ID, because this block loads it. - Add a link such as
<a href="#" data-cookie-settings>Cookie settings</a>to your footer so visitors can change their mind. You can also callopenCookieSettings()from your own script. - Test it: open a private window, check the banner appears, press Reject all and confirm no analytics requests are sent, then clear the site’s data and try Accept all.
When to use it
- Adding a consent banner to a site that uses Google Analytics 4, so analytics only starts after a visitor agrees.
- Sending the Consent Mode v2 signals to Google Tag Manager or gtag.js without installing a plugin.
- Trying out banner wording, colours and positions before handing the result to a developer.
- Testing how Basic and Advanced Consent Mode change what is sent to Google.
How the generated banner works
Google Consent Mode lets a website tell Google’s tags what each visitor has agreed to. The tags then adjust: they set or skip cookies and stop or limit what they send. Consent Mode v2 has four signals. analytics_storage covers analytics cookies, ad_storage covers advertising cookies, ad_user_data covers sending advertising-related user data to Google, and ad_personalization covers personalised advertising. Each is either granted or denied.
The snippet does four jobs, in this order:
- It sets the default state before any Google tag runs. The default is
denied, ordeniedonly in the EEA, UK and Switzerland if you chose that option. - It checks whether this visitor already made a choice in this browser, and if so applies it straight away and skips the banner.
- Otherwise it shows the banner. A button press stores the choice in the browser’s local storage for the number of days you set, and sends a consent update to Google.
- It loads Google’s tag, immediately in Advanced mode, or only once the visitor has accepted something in Basic mode.
In Basic mode Google’s tag does not load until a visitor accepts, so nothing is sent to Google before that. In Advanced mode the tag loads at once with consent denied and sends cookieless pings, which Google can use for modelling. Advanced mode keeps more measurement data but contacts Google before the visitor has answered, so ask your own advisers which fits your situation.
Worked example. You enter G-ABC123XYZ9, keep “Deny by default for every visitor” and Basic mode, and add no advertising choice. A first-time visitor sees the banner and the snippet has already run this:
gtag('consent', 'default', {
analytics_storage: 'denied', ad_storage: 'denied',
ad_user_data: 'denied', ad_personalization: 'denied'
});
No Google script has been requested yet. The visitor presses Accept all. The snippet stores the choice, sends gtag('consent', 'update', { analytics_storage: 'granted', … }) with the three advertising signals still denied, and now loads gtag/js?id=G-ABC123XYZ9. On the next visit the stored choice is found, the default is set to granted for analytics straight away, and there is no banner. If the visitor presses Reject all instead, every signal stays denied, Google’s tag is never requested, and any Google cookies already present are cleared.
Good practice for the wording and buttons
Regulators in the EU and UK generally expect consent to be a clear, positive action, expect that non-essential cookies are not set before it, and expect that refusing is as easy as accepting. That is why the banner gives Accept all and Reject all the same size and weight on the same screen. Say plainly what the cookies are for, link to your privacy policy, and do not pre-tick optional boxes: in the preferences panel every optional box starts unticked.
What this does not do
This is a template, not legal advice, and it is not a complete consent management platform. It does not scan your site for cookies, keep proof of consent on a server, translate itself, or block scripts other than Google’s. If you serve Google ads in the EEA, the UK or Switzerland, Google requires a consent management platform that Google has certified, so use one of those instead.
| Signal | What it controls | Set to granted when the visitor |
|---|---|---|
| analytics_storage | Analytics cookies and similar storage | accepts all, or ticks Analytics |
| ad_storage | Advertising cookies and similar storage | accepts all, or ticks Advertising (if you add that choice) |
| ad_user_data | Sending advertising-related user data to Google | accepts all, or ticks Advertising (if you add that choice) |
| ad_personalization | Personalised advertising | accepts all, or ticks Advertising (if you add that choice) |
Frequently asked questions
What is Google Consent Mode v2?
It is a way for a website to tell Google’s tags whether each visitor has agreed to analytics and advertising storage. Version 2 added two signals, ad_user_data and ad_personalization, to the original ad_storage and analytics_storage. The tags then change what they store and send.
What is the difference between Basic and Advanced mode?
In Basic mode Google’s tag does not load until the visitor accepts, so nothing is sent beforehand. In Advanced mode the tag loads straight away with consent denied and sends cookieless pings, which Google can use for modelling. Advanced keeps more data but contacts Google before the visitor has answered.
Do I need a cookie banner?
It depends on where your visitors are and which cookies you set. In the EU and UK, non-essential cookies, including most analytics cookies, generally need consent first. Other places have different rules. This is a general answer, not legal advice, so check the rules that apply to you.
Where do I paste the code, and does it work with Google Tag Manager?
Paste the whole block into the head of every page, above any other Google tag, and remove your old tag snippet for the same ID. Enter a GTM- container ID and the snippet loads Tag Manager for you; the consent signals are available to the tags inside it.
How can visitors change their choice later?
Add a link or button with the attribute data-cookie-settings, or call openCookieSettings() from your own script. It reopens the preferences panel with the visitor’s current choices, and a new choice is sent to Google straight away.
Does this tool send my settings or tag ID anywhere?
No. The code is built in your browser and nothing you enter is uploaded or logged. The banner you publish runs on your own site, and it only contacts Google if you added a tag ID and the visitor’s choice allows it.
Sources and further reading
- Google: Manage consent settings (web) (opens in a new tab) – the four consent types, default and update commands, and region-specific defaults
- Google: Consent mode overview (opens in a new tab) – how Basic and Advanced modes differ
- Google AdSense Help: Consent management requirements for the EEA, UK and Switzerland (opens in a new tab) – why Google ads there need a Google-certified consent management platform
- ICO: Cookies and similar technologies (opens in a new tab) – UK rules on consent before non-essential cookies are set
- EDPB: Guidelines 05/2020 on consent under Regulation 2016/679 (opens in a new tab) – what counts as valid consent under the GDPR