Prompt
How do I set up a cookie banner that blocks tags until consent is given?
Latest observation
To set up a cookie banner that blocks tags until consent is given, you generally want a consent management setup that does two things:
- Shows a banner before any non-essential cookies/tags load
- Prevents tracking scripts from firing until the user opts in
The exact setup depends on your stack, but the common pattern is:
1) Classify your tags
Split your scripts into categories:
- Strictly necessary: required for site functionality
- Analytics: GA4, Matomo, etc.
- Marketing/ads: Google Ads, Meta Pixel, etc.
- Preferences: language, layout, etc.
Only the necessary ones should load by default.
2) Use a Consent Management Platform or consent mode
You can do this with:
- Google Tag Manager + Consent Mode
- A CMP like OneTrust, Cookiebot, Quantcast, iubenda, etc.
- A custom implementation if you only have a few tags
If you use Google tags, Consent Mode v2 is usually the recommended approach.
3) Block scripts before consent
If using Google Tag Manager
Set your tags to require consent:
- In GTM, open each tag
- Go to Consent settings
- Require the appropriate consent types, such as:
analytics_storagead_storagead_user_dataad_personalization
Then configure the site so consent is denied by default until the user chooses.
Example default consent state
Before the user acts, set:
- analytics: denied
- ads: denied
- personalization: denied
Then update on user choice.
4) Add the banner UI
Your banner should:
- appear on first visit
- explain what cookies are used for
- offer at least:
- Accept all
- Reject non-essential
- Customize settings
Make sure “Reject” is as easy as “Accept” in regions where that’s required.
5) Update consent when the user responds
When the user clicks accept/reject, store the choice and notify your tag system.
If using Google consent mode, the flow is:
- Set default consent to denied
- On banner action, call consent update with granted/denied values
- GTM/tags then fire only when allowed
6) Do not load tracking tags prematurely
A common mistake is including the analytics script directly in the page <head>. Instead:
- Load it only after consent
- Or load it in a consent-aware mode that suppresses cookies until allowed
For example, if you’re using GTM, load GTM with a consent default state and let GTM handle firing.
7) Store consent preferences
Save the user’s choice in:
- a cookie
- localStorage
- your CMP’s storage
Then on future visits:
- read the stored preference
- apply it before any non-essential tag runs
8) Test carefully
Verify that:
- no analytics/ad requests happen before consent
- rejection truly blocks tags
- consent changes update behavior immediately
- scripts remain blocked after page refresh until consent is changed
Use browser dev tools:
- Network tab
- Application > Cookies / Local Storage
- Tag Assistant / CMP preview tools
Simple implementation pattern
Before consent
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('consent', 'default', {
'analytics_storage': 'denied',
'ad_storage': 'denied',
'ad_user_data': 'denied',
'ad_personalization': 'denied'
});
</script>
After user accepts analytics
<script>
gtag('consent', 'update', {
'analytics_storage': 'granted'
});
</script>
After user rejects all non-essential
<script>
gtag('consent', 'update', {
'analytics_storage': 'denied',
'ad_storage': 'denied',
'ad_user_data': 'denied',
'ad_personalization': 'denied'
});
</script>
Best practice recommendation
If your site uses Google Analytics, Ads, or GTM, the cleanest path is:
- implement a cookie banner/CMP
- set default consent to denied
- update consent on user action
- let GTM fire tags based on consent
This is safer and easier to maintain than manually conditionally loading every tag.
If you want, I can give you:
- a plain HTML/JavaScript example,
- a Google Tag Manager setup, or
- a WordPress / Shopify / React-specific implementation.