Prompt
I'm building a cookie banner that won't break analytics or ad tracking
Latest observation
Here’s a practical way to build a cookie banner that is privacy-compliant while minimizing damage to analytics and ad tracking.
1) Core principle: don’t load non-essential tags until consent
To avoid “breaking” tracking in a messy way, structure your site so:
- Essential cookies/scripts load immediately
- Analytics load only after analytics consent
- Advertising / remarketing / personalization load only after ad consent
- If consent is denied, those tags should not fire at all
This is usually done with:
- Google Tag Manager Consent Mode
- A consent management platform (CMP) or your own consent layer
- Conditional script loading
2) Recommended consent categories
Keep categories simple and understandable:
- Necessary — always on
- Analytics — optional
- Marketing / Advertising — optional
- Preferences — optional if you use them
- Functional — optional, depending on jurisdiction and use case
Avoid pre-ticked boxes. Make Accept all, Reject all, and Manage preferences equally visible.
3) Best practice for analytics
If you want analytics to still be useful even when users decline:
Option A: Consent Mode / cookieless pings
Use a system that sends limited, non-cookie-based signals until consent is granted.
For example:
- Google Consent Mode v2
- Similar consent-aware modes for other vendors
This helps preserve aggregated measurement without setting analytics cookies before consent.
Option B: Delay analytics until opt-in
Simpler, but you’ll lose data from users who reject analytics.
Option C: Server-side analytics
Can reduce client-side cookie reliance, but still needs careful legal review and consent logic.
4) Best practice for ad tracking
Ad tracking is the most sensitive. Usually:
- Don’t load ad pixels, remarketing tags, or conversion scripts until ad consent is granted
- If using Google Ads / Meta / TikTok / LinkedIn, configure their consent integration properly
- If consent is denied, they should not set advertising identifiers or cookies
5) Implementation pattern
A clean pattern is:
- Page loads with only essential scripts
- Banner shows with consent choices
- User choice is stored in a consent cookie/local storage
- Based on the choice:
- Load analytics tag
- Load ad tag
- Or keep them blocked
- Respect changes later via a “Cookie settings” link in the footer
6) Important technical details
Use a real consent state
Store something like:
necessary: trueanalytics: granted/deniedmarketing: granted/denied
Don’t just hide the banner
You need an actual enforcement mechanism:
- Prevent tags from loading
- Prevent cookies from being set
- Prevent network calls where possible
Add a way to withdraw consent
Users must be able to:
- Change preferences
- Withdraw consent as easily as they gave it
7) If you use Google tools
If you use GA4 / Google Ads / GTM, set up:
- Google Consent Mode v2
- Consent signals for:
ad_storageanalytics_storagead_user_dataad_personalization
This is the standard approach for preserving some measurement without violating consent requirements.
8) Don’t forget these UI/legal basics
- No dark patterns
- Clear language
- Link to privacy/cookie policy
- Explain what each category does
- Provide “Reject all” at the same level as “Accept all”
- Keep a consent log if required by your compliance process
9) Simple loading logic example
Conceptually:
if (consent.analytics) {
loadAnalytics();
}
if (consent.marketing) {
loadAdPixels();
}
And on first visit:
consent.analyticsandconsent.marketingshould default to false until the user chooses
10) Practical recommendation
If you want the least pain:
- Use a CMP
- Use Google Consent Mode v2 if you’re in the Google ecosystem
- Block all non-essential tags by default
- Add a consent banner with Accept / Reject / Settings
- Re-evaluate tracking setup with legal/compliance input
If you want, I can help you with either:
- a vanilla JS cookie banner implementation,
- a Google Tag Manager + Consent Mode setup, or
- a React/Vue component for this.