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