Skip to content

Update tracker #91

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions app.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,8 @@ declare module '@nuxt/schema' {
*/
domains?: string | string[];
/**
* Whether to ignore browsers' Do Not Track setting.
*
* Setting this to `false` will totally disable tracking
* on browsers that have the DoNotTrack setting turned on.
* @default true
* This option has been deprecated and is
* just here for compatibility.
*/
ignoreDnt?: boolean;
/**
Expand Down
1 change: 0 additions & 1 deletion internal/debug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ interface ErrorObj {
}

const warnings: Record<ErrorId, ErrorObj> = {
'err-dnt': { level: 'info', text: 'Tracking disabled by browser\'s DoNotTrack' },
'err-domain': { level: 'info', text: 'Tracking is disabled for this domain because it is not in the allowed domain config.' },
'err-id': { level: 'error', text: '`id` is missing or incorrectly configured. Check config.' },
'err-host': { level: 'error', text: '`host` is missing or incorrectly configured. Check config.' },
Expand Down
15 changes: 4 additions & 11 deletions internal/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ interface EventData {
[key: string]: string | number | boolean;
}

type EventPayloadPartial =
type EventDetails =
{
name: string;
data?: EventData;
Expand All @@ -21,24 +21,18 @@ type EventPayloadPartial =
event_data?: EventData;
};

type EventPayload = BasicPayload & EventPayloadPartial;
type ViewPayload = BasicPayload;
type EventPayload = BasicPayload & EventDetails;
type ViewPayload = BasicPayload;

type PartialPayload = Omit<BasicPayload, 'website'>;
type PayloadType = 'pageview' | 'event';
type PreflightResult = 'ssr' | 'id' | 'host' | 'domain' | 'dnt' | 'local' | true;
type PreflightResult = 'ssr' | 'id' | 'host' | 'domain' | 'local' | true;

interface ServerPayload {
type: PayloadType;
payload: ViewPayload | EventPayload;
}

interface GetPayloadReturn {
payload: PartialPayload;
pageUrl: string;
pageReferrer: string;
}

export type {
EventData,
PartialPayload,
Expand All @@ -47,5 +41,4 @@ export type {
PayloadType,
ServerPayload,
PreflightResult,
GetPayloadReturn,
};
31 changes: 6 additions & 25 deletions internal/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { useTitle } from '@vueuse/core';

import type {
GetPayloadReturn,
PartialPayload,
PreflightResult,
ServerPayload,
Expand Down Expand Up @@ -33,7 +32,6 @@ const umConfig = computed(() => {
host: _host = '',
id = '',
domains = undefined,
ignoreDnt = true,
ignoreLocalhost: ignoreLocal = false,
autoTrack = true,
customEndpoint: _customEP = undefined,
Expand All @@ -59,7 +57,6 @@ const umConfig = computed(() => {
host,
id: umamiId || id,
domains,
ignoreDnt,
ignoreLocal,
autoTrack,
customEndpoint,
Expand Down Expand Up @@ -104,7 +101,7 @@ const preflight = computed((): PreflightResult => {
if (typeof window === 'undefined')
return 'ssr';

const { ignoreDnt, id, host, ignoreLocal } = umConfig.value;
const { id, host, ignoreLocal } = umConfig.value;

if (!isValidString(id))
return 'id';
Expand All @@ -114,7 +111,6 @@ const preflight = computed((): PreflightResult => {

const {
location: { hostname },
navigator,
} = window;

if (ignoreLocal && hostname === 'localhost')
Expand All @@ -125,19 +121,10 @@ const preflight = computed((): PreflightResult => {
if (domains && !domains.includes(hostname))
return 'domain';

if (!ignoreDnt && [1, '1', 'yes'].includes(
navigator.doNotTrack
// @ts-expect-error `doNotTrack` might not exist on `window`
|| window.doNotTrack
// @ts-expect-error `msDoNotTrack` might not exist on `navigator`
|| navigator.msDoNotTrack,
))
return 'dnt';

return true;
});

const getPayload = computed((): GetPayloadReturn => {
const getPayload = computed((): PartialPayload => {
const {
location: { hostname, href },
screen: { width, height },
Expand All @@ -152,19 +139,13 @@ const getPayload = computed((): GetPayloadReturn => {
const params = new URL(href).searchParams;
const pageRef = referrer || params.get('ref') || '';

const payload: PartialPayload = {
return {
screen: `${width}x${height}`,
language,
hostname,
url: pageUrl,
referrer: pageRef,
title: pageTitle.value || title,
};

return {
payload,
pageReferrer: pageRef,
pageUrl,
url: encodeURI(pageUrl),
referrer: encodeURI(pageRef),
title: encodeURIComponent(pageTitle.value || title),
};
});

Expand Down
1 change: 0 additions & 1 deletion nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ export default defineNuxtConfig({
domains: '',
debug: false,
autoTrack: true,
ignoreDnt: true,
useDirective: false,
customEndpoint: '/',
ignoreLocalhost: false,
Expand Down
10 changes: 5 additions & 5 deletions utils/umami.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@ function trackView(url?: string, referrer?: string): void {
}

const { id: website, version } = umConfig.value;
const { pageReferrer, pageUrl, payload } = getPayload.value;
const payload = getPayload.value;

void collect(
{
type: version === 2 ? 'event' : 'pageview',
payload: {
...payload,
url: isValidString(url) ? url : pageUrl,
referrer: isValidString(referrer) ? referrer : pageReferrer,
website,
...payload,
...(isValidString(referrer) && { referrer: encodeURI(referrer) }),
...(isValidString(url) && { url: encodeURI(url) }),
} satisfies ViewPayload,
},
);
Expand All @@ -54,7 +54,7 @@ function trackEvent(eventName: string, eventData?: EventData) {
}

const { id: website, version } = umConfig.value;
const { payload } = getPayload.value;
const payload = getPayload.value;

let name = eventName;

Expand Down