Fix cookie banner

This commit is contained in:
engboris 2025-12-08 22:54:35 +01:00
parent 387c201590
commit b9adf70ee2
2 changed files with 30 additions and 36 deletions

View File

@ -3,6 +3,9 @@
* Cookie consent banner component.
* Displays a GDPR-compliant cookie consent banner at the bottom of the page.
* Manages user consent for analytics cookies via localStorage.
*
* Note: Analytics loading is handled separately in Layout.astro to avoid
* ad blockers blocking this script due to analytics-related keywords.
*/
---
@ -19,7 +22,7 @@
<div class="flex-1">
<h2 id="cookie-banner-title" class="font-semibold mb-1">Cookie Consent</h2>
<p id="cookie-banner-description" class="text-sm text-base-content/80">
We use cookies to analyze site traffic with Google Analytics.
We use cookies to analyze site traffic.
<a href="/privacy/" class="link link-primary">Learn more</a>
</p>
</div>
@ -46,48 +49,23 @@
<script is:inline>
(function() {
const CONSENT_KEY = 'cookie-consent';
const banner = document.getElementById('cookie-banner');
const acceptBtn = document.getElementById('cookie-accept');
const declineBtn = document.getElementById('cookie-decline');
var CONSENT_KEY = 'cookie-consent';
var banner = document.getElementById('cookie-banner');
var acceptBtn = document.getElementById('cookie-accept');
var declineBtn = document.getElementById('cookie-decline');
/**
* Loads Google Analytics scripts dynamically.
*/
function loadGoogleAnalytics() {
if (document.getElementById('ga-script')) return;
const script = document.createElement('script');
script.id = 'ga-script';
script.async = true;
script.src = 'https://www.googletagmanager.com/gtag/js?id=G-7308Y1HGM9';
document.head.appendChild(script);
window.dataLayer = window.dataLayer || [];
function gtag() { dataLayer.push(arguments); }
gtag('js', new Date());
gtag('config', 'G-7308Y1HGM9');
}
/**
* Saves consent preference and handles banner visibility.
* @param {boolean} consented - Whether the user consented to cookies.
*/
function setConsent(consented) {
localStorage.setItem(CONSENT_KEY, consented ? 'granted' : 'denied');
function setConsent(granted) {
localStorage.setItem(CONSENT_KEY, granted ? 'granted' : 'denied');
banner.classList.add('hidden');
if (consented) {
loadGoogleAnalytics();
if (granted) {
window.dispatchEvent(new CustomEvent('consent-granted'));
}
}
/**
* Checks stored consent and shows banner if no decision has been made.
*/
function checkConsent() {
const consent = localStorage.getItem(CONSENT_KEY);
var consent = localStorage.getItem(CONSENT_KEY);
if (consent === 'granted') {
loadGoogleAnalytics();
window.dispatchEvent(new CustomEvent('consent-granted'));
} else if (consent === null) {
banner.classList.remove('hidden');
}

View File

@ -83,5 +83,21 @@ const seoConfig = {
<Footer />
<CookieBanner />
<!-- Analytics loader (separate script to avoid ad blockers blocking the consent banner) -->
<script is:inline>
window.addEventListener('consent-granted', function() {
if (document.getElementById('ga-script')) return;
var s = document.createElement('script');
s.id = 'ga-script';
s.async = true;
s.src = 'https://www.googletagmanager.com/gtag/js?id=G-7308Y1HGM9';
document.head.appendChild(s);
window.dataLayer = window.dataLayer || [];
function gtag() { dataLayer.push(arguments); }
gtag('js', new Date());
gtag('config', 'G-7308Y1HGM9');
});
</script>
</body>
</html>