|
| 1 | +<script> |
| 2 | + import { onMount } from "svelte"; |
| 3 | + import { fly } from "svelte/transition"; |
| 4 | +
|
| 5 | + import { PUBLIC_GOOGLE_ANALYTICS_ID } from "$env/static/public"; |
| 6 | +
|
| 7 | + import { m } from '$lib/paraglide/messages.js'; |
| 8 | +
|
| 9 | + import Button from "./Button.svelte"; |
| 10 | +
|
| 11 | + let showBanner = $state(false); |
| 12 | +
|
| 13 | + onMount(() => { |
| 14 | + const consentStatus = localStorage.getItem("consent_status"); |
| 15 | +
|
| 16 | + if (consentStatus === null) { |
| 17 | + showBanner = true; |
| 18 | + } else if (consentStatus === "granted") { |
| 19 | + window.gtag("consent", "update", { |
| 20 | + analytics_storage: "granted", |
| 21 | + ad_storage: "granted", |
| 22 | + ad_user_data: "granted", |
| 23 | + ad_personalization: "granted", |
| 24 | + }); |
| 25 | + } |
| 26 | + }); |
| 27 | +
|
| 28 | + function handleAccept() { |
| 29 | + showBanner = false; |
| 30 | + localStorage.setItem("consent_status", "granted"); |
| 31 | +
|
| 32 | + window.gtag("consent", "update", { |
| 33 | + analytics_storage: "granted", |
| 34 | + ad_storage: "granted", |
| 35 | + ad_user_data: "granted", |
| 36 | + ad_personalization: "granted", |
| 37 | + }); |
| 38 | + } |
| 39 | +
|
| 40 | + function handleDeny() { |
| 41 | + showBanner = false; |
| 42 | + localStorage.setItem("consent_status", "denied"); |
| 43 | + } |
| 44 | +
|
| 45 | + export function reopenBanner() { |
| 46 | + showBanner = true; |
| 47 | + localStorage.removeItem("consent_status"); |
| 48 | + } |
| 49 | +</script> |
| 50 | + |
| 51 | +<svelte:head> |
| 52 | + <script> |
| 53 | + window.dataLayer = window.dataLayer || []; |
| 54 | +
|
| 55 | + function gtag() { |
| 56 | + dataLayer.push(arguments); |
| 57 | + } |
| 58 | +
|
| 59 | + window.gtag = gtag; |
| 60 | +
|
| 61 | + gtag("consent", "default", { |
| 62 | + analytics_storage: "denied", |
| 63 | + ad_storage: "denied", |
| 64 | + ad_user_data: "denied", |
| 65 | + ad_personalization: "denied", |
| 66 | + }); |
| 67 | + </script> |
| 68 | + |
| 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> |
| 74 | +</svelte:head> |
| 75 | + |
| 76 | +{#if showBanner} |
| 77 | + <div transition:fly="{{ x: 20, duration: 300 }}" class="banner"> |
| 78 | + <p> |
| 79 | + {@html m["cookieBanner.message"]() } |
| 80 | + </p> |
| 81 | + |
| 82 | + <div class="buttons"> |
| 83 | + <Button type="medium" onclick={handleAccept}>{ m["cookieBanner.accept"]() }</Button> |
| 84 | + <Button type="medium" onclick={handleDeny}>{ m["cookieBanner.deny"]() }</Button> |
| 85 | + </div> |
| 86 | + </div> |
| 87 | +{/if} |
| 88 | + |
| 89 | +<style lang="postcss"> |
| 90 | + .banner { |
| 91 | + position: fixed; |
| 92 | + bottom: 1em; |
| 93 | + right: 0; |
| 94 | + width: 370px; |
| 95 | +
|
| 96 | + background-color: #fff; |
| 97 | +
|
| 98 | + padding: 1rem; |
| 99 | +
|
| 100 | + border: solid 1px #ccc; |
| 101 | + border-right-width:0; |
| 102 | + border-top-left-radius: 0.6em; |
| 103 | + border-bottom-left-radius: 0.6em; |
| 104 | + box-shadow: 0 0 1em #0004; |
| 105 | +
|
| 106 | + p { |
| 107 | + margin: 0 0 1em; |
| 108 | + font-size: 1em; |
| 109 | + } |
| 110 | + } |
| 111 | +
|
| 112 | + .buttons { |
| 113 | + display: flex; |
| 114 | + gap: 0.5em; |
| 115 | + } |
| 116 | +</style> |
0 commit comments