diff --git a/.env.example b/.env.example index 4ae8806..911b74e 100644 --- a/.env.example +++ b/.env.example @@ -1 +1,2 @@ -PUBLIC_GOOGLE_ANALYTICS_ID=G-XXXXXXXXXX \ No newline at end of file +PUBLIC_GOOGLE_ANALYTICS_ID=G-XXXXXXXXXX +PUBLIC_ANALYTICS_ENABLED=false \ No newline at end of file diff --git a/src/lib/components/Analytics.svelte b/src/lib/components/Analytics.svelte index a17fd8b..695f0f7 100644 --- a/src/lib/components/Analytics.svelte +++ b/src/lib/components/Analytics.svelte @@ -2,7 +2,7 @@ import { onMount } from "svelte"; import { fly } from "svelte/transition"; - import { PUBLIC_GOOGLE_ANALYTICS_ID } from "$env/static/public"; + import { PUBLIC_GOOGLE_ANALYTICS_ID, PUBLIC_ANALYTICS_ENABLED } from "$env/static/public"; import { m } from '$lib/paraglide/messages.js'; @@ -10,7 +10,12 @@ let showBanner = $state(false); + // Don't initialize analytics if disabled + const isAnalyticsEnabled = PUBLIC_ANALYTICS_ENABLED === 'true'; + onMount(() => { + if (!isAnalyticsEnabled) return; + const consentStatus = localStorage.getItem("consent_status"); if (consentStatus === null) { @@ -26,6 +31,8 @@ }); function handleAccept() { + if (!isAnalyticsEnabled) return; + showBanner = false; localStorage.setItem("consent_status", "granted"); @@ -38,42 +45,48 @@ } function handleDeny() { + if (!isAnalyticsEnabled) return; + showBanner = false; localStorage.setItem("consent_status", "denied"); } export function reopenBanner() { + if (!isAnalyticsEnabled) return; + showBanner = true; localStorage.removeItem("consent_status"); } - + window.gtag = gtag; - - + gtag("consent", "default", { + analytics_storage: "denied", + ad_storage: "denied", + ad_user_data: "denied", + ad_personalization: "denied", + }); + + + + + {/if} -{#if showBanner} +{#if isAnalyticsEnabled && showBanner}