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

@ -10,7 +10,14 @@
<body> <body>
<div>%sveltekit.body%</div> <div>%sveltekit.body%</div>
</body> </body>
<!-- Google Tag Manager (noscript) -->
<!-- TODO: Is this allowed to be present, even when consent to collect data is not given? -->
<noscript> <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> </noscript>
</html> </html>

View File

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