Prompt
How do I make sure my cookie banner actually blocks Google Analytics until consent is given?
Latest observation
To make sure your cookie banner actually blocks Google Analytics until consent, you need more than just displaying a banner — you need to prevent any GA scripts, tags, or network requests from loading before consent.
What to do
1. Don’t load Google Analytics by default
If you use gtag.js, analytics.js, Google Tag Manager, or any plugin that injects GA, make sure it is not included in the page head unconditionally.
Bad example:
<script async src="https://www.googletagmanager.com/gtag/js?id=G-XXXXXXX"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-XXXXXXX');
</script>
This loads GA immediately, before consent.
2. Load analytics only after consent
Wait until the user accepts analytics cookies, then inject the script dynamically.
Example:
<script>
function loadGoogleAnalytics() {
if (window.gaLoaded) return;
window.gaLoaded = true;
var script = document.createElement('script');
script.async = true;
script.src = 'https://www.googletagmanager.com/gtag/js?id=G-XXXXXXX';
document.head.appendChild(script);
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
window.gtag = gtag;
gtag('js', new Date());
gtag('config', 'G-XXXXXXX');
}
function onConsentAccepted() {
localStorage.setItem('analytics_consent', 'yes');
loadGoogleAnalytics();
}
if (localStorage.getItem('analytics_consent') === 'yes') {
loadGoogleAnalytics();
}
</script>
3. Block consent until the user chooses
Your banner should not just say “By continuing, you agree.” It should have:
- Reject or Decline
- Accept analytics
- Ideally granular controls if required by your jurisdiction
4. If using Google Tag Manager, use consent mode correctly
If GTM is in use, it can still fire tags unless configured properly. Use:
- Google Consent Mode
- Default analytics consent = denied
- Only update consent to granted after user acceptance
Example:
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('consent', 'default', {
'analytics_storage': 'denied'
});
</script>
Then after consent:
<script>
gtag('consent', 'update', {
'analytics_storage': 'granted'
});
</script>
5. Verify it’s really blocked
Use browser DevTools:
- Open Network
- Reload the page before consent
- Search for:
googletagmanager.comgoogle-analytics.comgtag/js
- There should be no GA requests before consent
Also check:
- Cookies set by GA (
_ga,_gid,_gat) - Local/session storage
- Any iframe or pixel that may be related to analytics
6. Watch out for third-party plugins and embedded tools
Your site may still leak analytics through:
- WordPress plugins
- Shopify apps
- CMS theme settings
- Tag managers inserted by a marketing plugin
- Embedded YouTube/Meta/Hotjar widgets
Make sure all tracking sources are behind consent logic, not just the banner.
Best-practice checklist
- GA scripts are not hardcoded in the page before consent
- Consent defaults to denied
- Analytics code loads only after opt-in
- Banner has an actual reject option
- No GA cookies appear before consent
- Verified in DevTools network tab
- Third-party plugins are also controlled
If you want, I can give you a copy-paste implementation for:
- plain HTML/JavaScript
- Google Tag Manager
- React / Next.js
- WordPress
- Shopify