Skip to content

Commit b64fd16

Browse files
authored
env var to disable cookie notification (#54)
I'm fine with this, let's try to document it in our docs how how to locally stage the site
1 parent f276f16 commit b64fd16

File tree

3 files changed

+45
-24
lines changed

3 files changed

+45
-24
lines changed

.env.example

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
PUBLIC_GOOGLE_ANALYTICS_ID=G-XXXXXXXXXX
1+
PUBLIC_GOOGLE_ANALYTICS_ID=G-XXXXXXXXXX
2+
PUBLIC_ANALYTICS_ENABLED=false

src/lib/components/Analytics.svelte

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,20 @@
22
import { onMount } from "svelte";
33
import { fly } from "svelte/transition";
44
5-
import { PUBLIC_GOOGLE_ANALYTICS_ID } from "$env/static/public";
5+
import { PUBLIC_GOOGLE_ANALYTICS_ID, PUBLIC_ANALYTICS_ENABLED } from "$env/static/public";
66
77
import { m } from '$lib/paraglide/messages.js';
88
99
import Button from "./Button.svelte";
1010
1111
let showBanner = $state(false);
1212
13+
// Don't initialize analytics if disabled
14+
const isAnalyticsEnabled = PUBLIC_ANALYTICS_ENABLED === 'true';
15+
1316
onMount(() => {
17+
if (!isAnalyticsEnabled) return;
18+
1419
const consentStatus = localStorage.getItem("consent_status");
1520
1621
if (consentStatus === null) {
@@ -26,6 +31,8 @@
2631
});
2732
2833
function handleAccept() {
34+
if (!isAnalyticsEnabled) return;
35+
2936
showBanner = false;
3037
localStorage.setItem("consent_status", "granted");
3138
@@ -38,42 +45,48 @@
3845
}
3946
4047
function handleDeny() {
48+
if (!isAnalyticsEnabled) return;
49+
4150
showBanner = false;
4251
localStorage.setItem("consent_status", "denied");
4352
}
4453
4554
export function reopenBanner() {
55+
if (!isAnalyticsEnabled) return;
56+
4657
showBanner = true;
4758
localStorage.removeItem("consent_status");
4859
}
4960
</script>
5061

5162
<svelte:head>
52-
<script>
53-
window.dataLayer = window.dataLayer || [];
63+
{#if isAnalyticsEnabled && PUBLIC_GOOGLE_ANALYTICS_ID}
64+
<script>
65+
window.dataLayer = window.dataLayer || [];
5466
55-
function gtag() {
56-
dataLayer.push(arguments);
57-
}
58-
59-
window.gtag = gtag;
67+
function gtag() {
68+
dataLayer.push(arguments);
69+
}
6070
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;
6872
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}
7487
</svelte:head>
7588

76-
{#if showBanner}
89+
{#if isAnalyticsEnabled && showBanner}
7790
<div transition:fly="{{ x: 20, duration: 300 }}" class="banner">
7891
<p>
7992
{@html m["cookieBanner.message"]() }

src/lib/components/Footer.svelte

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
1-
<script>
1+
<script lang="ts">
2+
import { PUBLIC_ANALYTICS_ENABLED } from "$env/static/public";
3+
24
import { m } from '$lib/paraglide/messages.js';
5+
36
import Button from './Button.svelte';
47
import Socials from './Socials.svelte';
58
import Wrapper from './Wrapper.svelte';
69
import Analytics from './Analytics.svelte';
710
8-
let analytics;
11+
let analytics: Analytics;
12+
13+
const isAnalyticsEnabled = PUBLIC_ANALYTICS_ENABLED === 'true';
914
1015
const buttons = [
1116
{
@@ -36,11 +41,13 @@
3641
</div>
3742
{/each}
3843

44+
{#if isAnalyticsEnabled}
3945
<div>
4046
<Button type="medium" onclick={() => analytics?.reopenBanner()}>
4147
{m["cookieBanner.settings"]()}
4248
</Button>
4349
</div>
50+
{/if}
4451
</div>
4552

4653
<!-- <button onclick={() => setLocale('en')}>en</button> -->

0 commit comments

Comments
 (0)