From 14bcbaa115a223167f1541541590e5b522e3f908 Mon Sep 17 00:00:00 2001 From: supermar1010 Date: Sat, 18 May 2024 09:49:50 +0200 Subject: [PATCH] Added umami.disabled support --- .playground/app.vue | 7 +++++++ internal/debug.ts | 1 + internal/types.ts | 2 +- internal/utils.ts | 4 ++++ 4 files changed, 13 insertions(+), 1 deletion(-) diff --git a/.playground/app.vue b/.playground/app.vue index d59946e..86c2e6d 100644 --- a/.playground/app.vue +++ b/.playground/app.vue @@ -12,6 +12,10 @@ function testEvent() { umTrackEvent('event-test-2', { type: 'click', position: 'left' }); } +function disableTrackingViaLocalStorage(){ + localStorage.setItem('umami.disabled', '1') +} + const tt = ref('TT'); const tc = ref(0); @@ -47,6 +51,9 @@ function seePreview() { + = { 'err-id': { level: 'error', text: '`id` is missing or incorrectly configured. Check Umami config.' }, 'err-host': { level: 'error', text: '`host` is missing or incorrectly configured. Check Umami config.' }, 'err-local': { level: 'info', text: 'Tracking disabled on localhost' }, + 'err-local-storage': { level: 'info', text: 'Tracking disabled via local-storage' }, 'err-collect': { level: 'error', text: 'Uhm... Something went wrong and I have no clue.' }, 'err-directive': { level: 'error', text: 'Invalid v-umami directive value. Expected string or object with {key:value} pairs. See https://github.com/ijkml/nuxt-umami#available-methods' }, 'err-event-name': { level: 'warn', text: 'An Umami track event was fired without a name. `#unknown-event` will be used as event name.' }, diff --git a/internal/types.ts b/internal/types.ts index 1218af0..519218b 100644 --- a/internal/types.ts +++ b/internal/types.ts @@ -95,7 +95,7 @@ type ViewPayload = BasicPayload; type PartialPayload = Omit; type PayloadType = 'pageview' | 'event'; -type PreflightResult = 'ssr' | 'id' | 'host' | 'domain' | 'local' | true; +type PreflightResult = 'ssr' | 'id' | 'host' | 'domain' | 'local' | 'local-storage' | true; interface ServerPayload { type: PayloadType; diff --git a/internal/utils.ts b/internal/utils.ts index 16d7977..132fc46 100644 --- a/internal/utils.ts +++ b/internal/utils.ts @@ -109,6 +109,10 @@ const preflight = computed((): PreflightResult => { if (ignoreLocal && hostname === 'localhost') return 'local'; + // Disable tracking when umami.disabled=1 in localStorage + if (localStorage.getItem('umami.disabled') === '1') + return 'local-storage'; + const domains = domainList.value; if (domains && !domains.includes(hostname))