Skip to content

Commit

Permalink
Added umami.disabled support
Browse files Browse the repository at this point in the history
  • Loading branch information
supermar1010 committed May 18, 2024
1 parent 5214297 commit 14bcbaa
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 1 deletion.
7 changes: 7 additions & 0 deletions .playground/app.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -47,6 +51,9 @@ function seePreview() {
<button @click="testView">
Run trackView
</button>
<button @click="disableTrackingViaLocalStorage">
Disable tracking via localStorage
</button>
<a
:href="shareUrl"
target="_blank"
Expand Down
1 change: 1 addition & 0 deletions internal/debug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const warnings: Record<ErrorId, ErrorObj> = {
'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.' },
Expand Down
2 changes: 1 addition & 1 deletion internal/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ type ViewPayload = BasicPayload;

type PartialPayload = Omit<BasicPayload, 'website'>;
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;
Expand Down
4 changes: 4 additions & 0 deletions internal/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down

0 comments on commit 14bcbaa

Please sign in to comment.