feat: replace iubenda with an own cookie banner and privacy pages, analytics only after consent
All checks were successful
Deploy to OVH VPS / deploy (push) Successful in 1m9s

This commit is contained in:
Lorenzo Iovino 2026-09-29 00:40:12 +02:00
parent 43cb271077
commit 0e4d4617ab
6 changed files with 259 additions and 124 deletions

View file

@ -32,74 +32,41 @@ const fullImageUrl = image
: `${siteUrl}${image}`
: `${siteUrl}${defaultImage}`;
const fullCanonicalUrl = canonicalUrl || `${siteUrl}${Astro.url.pathname}`;
import CookieBanner from "../components/CookieBanner.astro";
---
<!doctype html>
<html lang="en">
<head>
<script is:inline type="text/javascript">
var _iub = _iub || [];
_iub.csConfiguration = {
askConsentAtCookiePolicyUpdate: true,
cookiePolicyInOtherWindow: true,
enableFadp: true,
fadpApplies: true,
floatingPreferencesButtonDisplay: "anchored-center-right",
lang: "en",
perPurposeConsent: true,
siteId: 3452245,
whitelabel: false,
cookiePolicyId: 98687046,
banner: {
acceptButtonCaptionColor: "#FFFFFF",
acceptButtonColor: "#0073CE",
acceptButtonDisplay: true,
backgroundColor: "#FFFFFF",
closeButtonDisplay: false,
customizeButtonCaptionColor: "#4D4D4D",
customizeButtonColor: "#DADADA",
customizeButtonDisplay: true,
explicitWithdrawal: true,
listPurposes: true,
position: "float-bottom-left",
rejectButtonCaptionColor: "#FFFFFF",
rejectButtonColor: "#0073CE",
rejectButtonDisplay: true,
showPurposesToggles: true,
showTitle: false,
textColor: "#000000",
},
// Let the dark mode toast wait until the cookie banner is gone
callback: {
onBannerShown: function () {
window.__iubBannerOpen = true;
},
onPreferenceExpressed: function () {
window.__iubBannerOpen = false;
window.dispatchEvent(new Event("iub-consent"));
},
onReady: function () {
window.__iubReady = true;
window.dispatchEvent(new Event("iub-ready"));
},
},
};
</script>
<script
is:inline
type="text/javascript"
src="//cdn.iubenda.com/cs/iubenda_cs.js"
charset="UTF-8"
async></script>
<script is:inline async src="https://www.googletagmanager.com/gtag/js?id=G-SPLW5RY3HD"></script>
<script is:inline>
// Google Analytics only after consent (CookieBanner). The choice lasts
// 6 months, then the banner asks again.
window.getCookieConsent = function () {
try {
var value = localStorage.getItem("cookieConsent");
var date = Number(localStorage.getItem("cookieConsentDate") || 0);
if (!value || Date.now() - date > 180 * 24 * 60 * 60 * 1000) return null;
return value;
} catch (e) {
return null;
}
};
window.dataLayer = window.dataLayer || [];
function gtag() {
dataLayer.push(arguments);
}
gtag("js", new Date());
gtag("config", "G-SPLW5RY3HD");
window.loadAnalytics = function () {
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-SPLW5RY3HD";
document.head.appendChild(script);
gtag("js", new Date());
gtag("config", "G-SPLW5RY3HD");
};
if (window.getCookieConsent() === "accepted") window.loadAnalytics();
</script>
<meta charset="UTF-8" />
@ -240,6 +207,8 @@ const fullCanonicalUrl = canonicalUrl || `${siteUrl}${Astro.url.pathname}`;
<body class="bg-white dark:bg-gray-900 min-h-screen transition-colors duration-200">
<slot />
<CookieBanner />
<!-- Dark Mode Suggestion Toast -->
<div
id="dark-mode-toast"
@ -324,11 +293,16 @@ const fullCanonicalUrl = canonicalUrl || `${siteUrl}${Astro.url.pathname}`;
const hasBeenDismissed = localStorage.getItem(TOAST_DISMISSED_KEY);
// Only show toast if user is in light mode and hasn't dismissed it
const bannerOpen = () =>
window.__iubBannerOpen || !!document.getElementById("iubenda-cs-banner");
// Never on top of the cookie banner: if it's open, wait for the answer
const bannerOpen = () => {
const banner = document.getElementById("cookie-banner");
return !!banner && !banner.classList.contains("hidden");
};
const showToast = () => {
if (bannerOpen()) {
window.addEventListener("iub-consent", () => setTimeout(showToast, 1500), { once: true });
window.addEventListener("cookie-consent", () => setTimeout(showToast, 1500), {
once: true,
});
return;
}
const isDarkMode = document.documentElement.classList.contains("dark");
@ -336,27 +310,7 @@ const fullCanonicalUrl = canonicalUrl || `${siteUrl}${Astro.url.pathname}`;
toast.classList.remove("hidden");
}
};
// Never on top of the cookie banner: wait for iubenda, and if the banner
// is open wait for the visitor's answer. If iubenda never loads
// (blocked), show it anyway after a few seconds.
let scheduled = false;
const scheduleToast = (delay) => {
if (scheduled) return;
scheduled = true;
setTimeout(showToast, delay);
};
const afterIubenda = () => {
scheduleToast(2000);
};
if (window.__iubReady) {
afterIubenda();
} else {
window.addEventListener("iub-ready", afterIubenda, { once: true });
setTimeout(() => {
if (!window.__iubReady) scheduleToast(0);
}, 6000);
}
setTimeout(showToast, 2000);
// Enable dark mode
if (tryButton) {