Merge pull request #4421 from bm1016bm-svg/codex/support-data-distinct-id

Support distinct ID tracker attribute
This commit is contained in:
Mike Cao authored and GitHub committed 2026-09-10 20:30:50 -07:00
commit b7031eaa16
2 files changed
+44 -1

No files matched your search

+38
View File
@@ -0,0 +1,38 @@
import { afterEach, expect, test, vi } from 'vitest';
afterEach(() => {
delete (window as Window & { umami?: unknown }).umami;
delete (document as Document & { currentScript?: HTMLScriptElement }).currentScript;
delete (document as Document & { readyState?: DocumentReadyState }).readyState;
vi.unstubAllGlobals();
vi.resetModules();
});
test('identifies data-distinct-id before the initial page view', async () => {
const script = document.createElement('script');
script.src = 'https://analytics.example.com/script.js';
script.dataset.websiteId = 'website-id';
script.dataset.distinctId = 'visitor-id';
Object.defineProperties(document, {
currentScript: { configurable: true, value: script },
readyState: { configurable: true, value: 'complete' },
});
const fetchMock = vi.fn().mockResolvedValue({ json: vi.fn().mockResolvedValue({}) });
vi.stubGlobal('fetch', fetchMock);
await import('./index');
await vi.waitFor(() => expect(fetchMock).toHaveBeenCalledTimes(2));
const requests = fetchMock.mock.calls.map(([, init]) => JSON.parse(init.body));
expect(requests[0]).toMatchObject({
type: 'identify',
payload: { id: 'visitor-id', website: 'website-id' },
});
expect(requests[1]).toMatchObject({
type: 'event',
payload: { id: 'visitor-id', website: 'website-id' },
});
});
+6 -1
View File
@@ -241,6 +241,7 @@ type MetricEntry = PerformanceEntry & {
const website = config('website-id');
const hostUrl = config('host-url');
const beforeSend = config('before-send');
const distinctId = config('distinct-id') || undefined;
const tag = config('tag') || undefined;
const autoTrack = config('auto-track') !== _false;
const dnt = config('do-not-track') === _true;
@@ -641,9 +642,13 @@ type MetricEntry = PerformanceEntry & {
let initialized = false;
let disabled = false;
let cache: string | undefined;
let identity: string | undefined;
let identity = distinctId;
let flushPerformance: (() => void) | undefined;
if (distinctId) {
void identify(distinctId);
}
if (autoTrack && !trackingDisabled()) {
if (document.readyState === 'complete') {
init();