Implementing a Compliant GDPR & CCPA Cookie Consent Banner with Google Consent Mode v2
Google Consent Mode v2 requires dispatching a default consent state before tag execution (`gtag("consent", "default", {...})`) and updating granted parameters (`analytics_storage`, `ad_storage`, `ad_user_data`, `ad_personalization`) via `gtag("consent", "update", {...})` upon user acceptance.
We use essential cookies and Google Consent Mode v2 signals to measure site performance responsibly.
Step 1: Initialize Consent Mode v2 Defaults in Document Head
Declare default denied states before any analytics or third-party ad scripts are parsed.
<!-- Google Tag Manager & Consent Mode v2 -->
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){ dataLayer.push(arguments); }
(function() {
try {
var saved = JSON.parse(localStorage.getItem('user_cookie_consent'));
if (saved) {
gtag('consent', 'default', {
'analytics_storage': saved.analytics ? 'granted' : 'denied',
'ad_storage': saved.marketing ? 'granted' : 'denied',
'ad_user_data': saved.marketing ? 'granted' : 'denied',
'ad_personalization': saved.marketing ? 'granted' : 'denied'
});
} else {
gtag('consent', 'default', {
'analytics_storage': 'denied',
'ad_storage': 'denied',
'ad_user_data': 'denied',
'ad_personalization': 'denied',
'wait_for_update': 500
});
}
} catch(e) {}
})();
gtag('js', new Date());
</script>
Step 2: Build the Floating Privacy Consent Banner
Create the floating dialog with Quick Accept, Reject Non-Essential, and Custom Preferences options.
<aside class="cookie-banner" id="cookieBanner" role="dialog" aria-modal="true" aria-labelledby="consentTitle">
<div class="banner-content">
<h3 id="consentTitle">🍪 Cookie & Privacy Preferences</h3>
<p>We use essential cookies to maintain security and optional analytics cookies to enhance performance. Learn more in our <a href="/privacy/">Privacy Policy</a>.</p>
</div>
<div class="banner-actions">
<button type="button" class="btn-reject" id="btnReject">Reject Optional</button>
<button type="button" class="btn-customize" id="btnCustomize">Preferences</button>
<button type="button" class="btn-accept" id="btnAccept">Accept All</button>
</div>
</aside>
Step 3: Store Preferences and Dispatch Consent Mode Updates
Save choice payload into localStorage and notify Google Tag Manager via gtag consent update.
const banner = document.getElementById('cookieBanner');
function updateConsent(analytics, marketing) {
const payload = {
analytics_storage: analytics ? 'granted' : 'denied',
ad_storage: marketing ? 'granted' : 'denied',
ad_user_data: marketing ? 'granted' : 'denied',
ad_personalization: marketing ? 'granted' : 'denied'
};
if (typeof gtag === 'function') {
gtag('consent', 'update', payload);
}
localStorage.setItem('user_cookie_consent', JSON.stringify({
analytics, marketing, timestamp: Date.now()
}));
banner.classList.add('is-hidden');
}
document.getElementById('btnAccept').addEventListener('click', () => updateConsent(true, true));
document.getElementById('btnReject').addEventListener('click', () => updateConsent(false, false));
Next-Level Addons: Elevating Your Implementation
Take this technique further by combining it with complementary design token studios and animation tools:
Here's my golden rule when implementing this in production: test across multiple devices and browsers early. Keep your DOM hierarchy shallow, optimize assets with modern formats (like WebP/AVIF), and verify keyboard navigation for accessibility.
Frequently Asked Questions
Practical answers to common implementation questions, browser quirks, and performance tips.
What is the penalty for not implementing Google Consent Mode v2?
Google Ads accounts operating in the European Economic Area (EEA) without Consent Mode v2 lose audience measurement, remarketing capability, and automated conversion bidding.
What are the 4 key parameters in Consent Mode v2?
The parameters are `analytics_storage` (Google Analytics 4), `ad_storage` (ad cookies), `ad_user_data` (sending user data to Google for ads), and `ad_personalization` (remarketing).
Can strictly necessary cookies be rejected by visitors?
No, under GDPR and ePrivacy regulations, strictly necessary cookies (authentication sessions, CSRF protection, shopping carts) do not require prior consent.
How long should cookie consent persistence last in localStorage?
European data protection authorities generally recommend expiring consent records after 6 to 12 months, prompting returning visitors to renew preferences.
Does this banner require external consent management platforms (CMPs)?
No, this implementation is 100% zero-dependency vanilla JS and CSS, eliminating heavy CMP scripts that degrade Core Web Vitals.