fix: only collect analytics when consent given

This commit is contained in:
oSumAtrIX 2023-06-21 02:27:21 +02:00
parent 8ea8dcc06c
commit 25fed14a0a
No known key found for this signature in database
GPG Key ID: A9B3094ACDB604B4
2 changed files with 37 additions and 66 deletions

View File

@ -3,14 +3,21 @@
<head>
<meta charset="utf-8" />
<link rel="icon" href="%sveltekit.assets%/logo.svg" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
%sveltekit.head%
</head>
<body>
<div>%sveltekit.body%</div>
</body>
<!-- Google Tag Manager (noscript) -->
<!-- TODO: Is this allowed to be present, even when consent to collect data is not given? -->
<noscript>
<iframe src="https://www.googletagmanager.com/ns.html?id=GTM-MQ6K849" height="0" width="0" style="display:none;visibility:hidden"></iframe>
<iframe
src="https://www.googletagmanager.com/ns.html?id=GTM-MQ6K849"
height="0"
width="0"
style="display: none; visibility: hidden"
></iframe>
</noscript>
</html>

View File

@ -2,7 +2,6 @@
import { writable } from 'svelte/store';
// There might be a better place to put this, but I am not entirely sure...
export const isRestoring = writable(false);
declare const telemetryInit: () => void;
</script>
<script lang="ts">
@ -33,21 +32,20 @@
}
});
let askCookieConsent = false;
let showConsentModal = false;
// store a cookie to remember the user's preference
function setPrivacySetting(allowTelemetry: boolean) {
localStorage.setItem('allowTelemetry', allowTelemetry.toString());
askCookieConsent = false;
if (typeof telemetryInit !== 'undefined') {
telemetryInit();
}
function rememberChoice(allow: boolean) {
localStorage.setItem('analytics', allow.toString());
showConsentModal = false;
if (allow) location.reload();
}
onMount(() => {
if (!localStorage.getItem('allowTelemetry')) {
askCookieConsent = true;
}
// Check if the user has already decided.
const hasDecided = localStorage.getItem('analytics') !== null;
if (!hasDecided) showConsentModal = true;
isRestoring.set(true);
const [unsubscribe, promise] = persistQueryClient({
@ -74,69 +72,36 @@
);
</script>
<!-- telemetry good -->
<svelte:head>
<!-- Google Tag Manager -->
<script>
(function (w, d, s, l, i) {
w[l] = w[l] || [];
w[l].push({ 'gtm.start': new Date().getTime(), event: 'gtm.js' });
var f = d.getElementsByTagName(s)[0],
j = d.createElement(s),
dl = l != 'dataLayer' ? '&l=' + l : '';
j.async = true;
j.src = 'https://www.googletagmanager.com/gtm.js?id=' + i + dl;
f.parentNode.insertBefore(j, f);
})(window, document, 'script', 'dataLayer', 'GTM-MQ6K849');
</script>
<script async src="https://www.googletagmanager.com/gtag/js?id=G-PLH0N9VQL5"></script>
<!-- Sometimes you don't want telemetry -->
<script>
window.dataLayer = window.dataLayer || [];
function gtag() {
dataLayer.push(arguments);
allowAnalytics = localStorage.getItem('analytics') === 'true';
if (allowAnalytics) {
(function (w, d, s, l, i) {
w[l] = w[l] || [];
w[l].push({ 'gtm.start': new Date().getTime(), event: 'gtm.js' });
var f = d.getElementsByTagName(s)[0],
j = d.createElement(s),
dl = l != 'dataLayer' ? '&l=' + l : '';
j.async = true;
j.src = 'https://www.googletagmanager.com/gtm.js?id=' + i + dl;
f.parentNode.insertBefore(j, f);
})(window, document, 'script', 'dataLayer', 'GTM-MQ6K849');
}
function runClarity(c, l, a, r, i, t, y) {
c[a] =
c[a] ||
function () {
(c[a].q = c[a].q || []).push(arguments);
};
t = l.createElement(r);
t.async = 1;
t.src = 'https://www.clarity.ms/tag/' + i;
y = l.getElementsByTagName(r)[0];
y.parentNode.insertBefore(t, y);
}
function telemetryInit() {
allowTelemetry = (localStorage.getItem('allowTelemetry') === 'true');
if (!allowTelemetry) {
gtag('set', 'allow_google_signals', false);
return;
}
gtag('js', new Date());
gtag('config', 'G-PLH0N9VQL5');
runClarity(window, document, 'clarity', 'script', 'hfh8dhfgus');
}
telemetryInit();
</script>
</svelte:head>
<QueryClientProvider client={queryClient}>
<NavHost />
<!-- if cookie consent hasn't been set -->
<Dialogue bind:modalOpen={askCookieConsent} notDismissible>
<Dialogue bind:modalOpen={showConsentModal} notDismissible>
<svelte:fragment slot="title">It's your choice</svelte:fragment>
<svelte:fragment slot="description">
This site uses analytics to understand better how you use it. Opting in is
optional and won't impact your experience.
We use analytics to improve your experience on this site. By clicking "Accept" you allow us to
collect anonymous data about your visit.
</svelte:fragment>
<svelte:fragment slot="buttons">
<Button type="text" on:click={() => setPrivacySetting(false)}>Deny</Button>
<Button type="text" on:click={() => setPrivacySetting(true)}>Accept</Button>
<Button type="text" on:click={() => rememberChoice(false)}>Deny</Button>
<Button type="outlined" on:click={() => rememberChoice(true)}>Accept</Button>
</svelte:fragment>
</Dialogue>
@ -145,6 +110,5 @@
{:else}
<slot />
{/if}
<!-- guhh afn -->
<!-- <Footer> -->
</QueryClientProvider>