Fix cookie banner
This commit is contained in:
parent
387c201590
commit
b9adf70ee2
|
|
@ -3,6 +3,9 @@
|
||||||
* Cookie consent banner component.
|
* Cookie consent banner component.
|
||||||
* Displays a GDPR-compliant cookie consent banner at the bottom of the page.
|
* Displays a GDPR-compliant cookie consent banner at the bottom of the page.
|
||||||
* Manages user consent for analytics cookies via localStorage.
|
* 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">
|
<div class="flex-1">
|
||||||
<h2 id="cookie-banner-title" class="font-semibold mb-1">Cookie Consent</h2>
|
<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">
|
<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>
|
<a href="/privacy/" class="link link-primary">Learn more</a>
|
||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
|
|
@ -46,48 +49,23 @@
|
||||||
|
|
||||||
<script is:inline>
|
<script is:inline>
|
||||||
(function() {
|
(function() {
|
||||||
const CONSENT_KEY = 'cookie-consent';
|
var CONSENT_KEY = 'cookie-consent';
|
||||||
const banner = document.getElementById('cookie-banner');
|
var banner = document.getElementById('cookie-banner');
|
||||||
const acceptBtn = document.getElementById('cookie-accept');
|
var acceptBtn = document.getElementById('cookie-accept');
|
||||||
const declineBtn = document.getElementById('cookie-decline');
|
var declineBtn = document.getElementById('cookie-decline');
|
||||||
|
|
||||||
/**
|
function setConsent(granted) {
|
||||||
* Loads Google Analytics scripts dynamically.
|
localStorage.setItem(CONSENT_KEY, granted ? 'granted' : 'denied');
|
||||||
*/
|
|
||||||
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');
|
|
||||||
banner.classList.add('hidden');
|
banner.classList.add('hidden');
|
||||||
if (consented) {
|
if (granted) {
|
||||||
loadGoogleAnalytics();
|
window.dispatchEvent(new CustomEvent('consent-granted'));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Checks stored consent and shows banner if no decision has been made.
|
|
||||||
*/
|
|
||||||
function checkConsent() {
|
function checkConsent() {
|
||||||
const consent = localStorage.getItem(CONSENT_KEY);
|
var consent = localStorage.getItem(CONSENT_KEY);
|
||||||
if (consent === 'granted') {
|
if (consent === 'granted') {
|
||||||
loadGoogleAnalytics();
|
window.dispatchEvent(new CustomEvent('consent-granted'));
|
||||||
} else if (consent === null) {
|
} else if (consent === null) {
|
||||||
banner.classList.remove('hidden');
|
banner.classList.remove('hidden');
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -83,5 +83,21 @@ const seoConfig = {
|
||||||
|
|
||||||
<Footer />
|
<Footer />
|
||||||
<CookieBanner />
|
<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>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue