|
2 | 2 | import { onMount } from "svelte"; |
3 | 3 | import { fly } from "svelte/transition"; |
4 | 4 |
|
5 | | - import { PUBLIC_GOOGLE_ANALYTICS_ID } from "$env/static/public"; |
| 5 | + import { PUBLIC_GOOGLE_ANALYTICS_ID, PUBLIC_ANALYTICS_ENABLED } from "$env/static/public"; |
6 | 6 |
|
7 | 7 | import { m } from '$lib/paraglide/messages.js'; |
8 | 8 |
|
9 | 9 | import Button from "./Button.svelte"; |
10 | 10 |
|
11 | 11 | let showBanner = $state(false); |
12 | 12 |
|
| 13 | + // Don't initialize analytics if disabled |
| 14 | + const isAnalyticsEnabled = PUBLIC_ANALYTICS_ENABLED === 'true'; |
| 15 | +
|
13 | 16 | onMount(() => { |
| 17 | + if (!isAnalyticsEnabled) return; |
| 18 | +
|
14 | 19 | const consentStatus = localStorage.getItem("consent_status"); |
15 | 20 |
|
16 | 21 | if (consentStatus === null) { |
|
26 | 31 | }); |
27 | 32 |
|
28 | 33 | function handleAccept() { |
| 34 | + if (!isAnalyticsEnabled) return; |
| 35 | +
|
29 | 36 | showBanner = false; |
30 | 37 | localStorage.setItem("consent_status", "granted"); |
31 | 38 |
|
|
38 | 45 | } |
39 | 46 |
|
40 | 47 | function handleDeny() { |
| 48 | + if (!isAnalyticsEnabled) return; |
| 49 | +
|
41 | 50 | showBanner = false; |
42 | 51 | localStorage.setItem("consent_status", "denied"); |
43 | 52 | } |
44 | 53 |
|
45 | 54 | export function reopenBanner() { |
| 55 | + if (!isAnalyticsEnabled) return; |
| 56 | +
|
46 | 57 | showBanner = true; |
47 | 58 | localStorage.removeItem("consent_status"); |
48 | 59 | } |
49 | 60 | </script> |
50 | 61 |
|
51 | 62 | <svelte:head> |
52 | | - <script> |
53 | | - window.dataLayer = window.dataLayer || []; |
| 63 | + {#if isAnalyticsEnabled && PUBLIC_GOOGLE_ANALYTICS_ID} |
| 64 | + <script> |
| 65 | + window.dataLayer = window.dataLayer || []; |
54 | 66 |
|
55 | | - function gtag() { |
56 | | - dataLayer.push(arguments); |
57 | | - } |
58 | | -
|
59 | | - window.gtag = gtag; |
| 67 | + function gtag() { |
| 68 | + dataLayer.push(arguments); |
| 69 | + } |
60 | 70 |
|
61 | | - gtag("consent", "default", { |
62 | | - analytics_storage: "denied", |
63 | | - ad_storage: "denied", |
64 | | - ad_user_data: "denied", |
65 | | - ad_personalization: "denied", |
66 | | - }); |
67 | | - </script> |
| 71 | + window.gtag = gtag; |
68 | 72 |
|
69 | | - <script async src={`https://www.googletagmanager.com/gtag/js?id=${PUBLIC_GOOGLE_ANALYTICS_ID}`}></script> |
70 | | - <script> |
71 | | - gtag("js", new Date()); |
72 | | - gtag("config", "{PUBLIC_GOOGLE_ANALYTICS_ID}"); |
73 | | - </script> |
| 73 | + gtag("consent", "default", { |
| 74 | + analytics_storage: "denied", |
| 75 | + ad_storage: "denied", |
| 76 | + ad_user_data: "denied", |
| 77 | + ad_personalization: "denied", |
| 78 | + }); |
| 79 | + </script> |
| 80 | + |
| 81 | + <script async src={`https://www.googletagmanager.com/gtag/js?id=${PUBLIC_GOOGLE_ANALYTICS_ID}`}></script> |
| 82 | + <script> |
| 83 | + gtag("js", new Date()); |
| 84 | + gtag("config", "{PUBLIC_GOOGLE_ANALYTICS_ID}"); |
| 85 | + </script> |
| 86 | + {/if} |
74 | 87 | </svelte:head> |
75 | 88 |
|
76 | | -{#if showBanner} |
| 89 | +{#if isAnalyticsEnabled && showBanner} |
77 | 90 | <div transition:fly="{{ x: 20, duration: 300 }}" class="banner"> |
78 | 91 | <p> |
79 | 92 | {@html m["cookieBanner.message"]() } |
|
0 commit comments