From e2133b0a471fd5b11679a38bf26069d180c06d31 Mon Sep 17 00:00:00 2001 From: Kiran K Date: Wed, 18 Nov 2026 17:25:29 +0530 Subject: [PATCH 1/8] Resolve the Fingerprint visitorId without POSTing to /track/identity Verification moves server-side in dubinc/platform#1054: a deferred job looks up Fingerprint events by linked_id, so the script no longer needs to send the eventId or wait for Dub's response before suffixing dub_id. Dropping the request shortens the window before an OAuth redirect. --- packages/script/src/identity-agent.js | 43 +---------- packages/script/src/identity-agent.test.js | 84 +--------------------- 2 files changed, 6 insertions(+), 121 deletions(-) diff --git a/packages/script/src/identity-agent.js b/packages/script/src/identity-agent.js index d6810ad..36bc99c 100644 --- a/packages/script/src/identity-agent.js +++ b/packages/script/src/identity-agent.js @@ -4,58 +4,21 @@ const FINGERPRINT_PUBLIC_KEY = process.env.DUB_FINGERPRINT_PUBLIC_KEY; // Dub's Fingerprint custom subdomain, so ad blockers don't stop the agent; Fingerprint's default stays as the fallback const FINGERPRINT_ENDPOINTS = ['https://identity.dub.co']; -// Loaded on demand by the identity extension; resolves the visitorId once Dub has accepted or rejected the identification +// Loaded on demand by the identity extension; resolves the visitorId once Fingerprint has identified the click async function identify({ apiHost, clickId }) { if (!FINGERPRINT_PUBLIC_KEY) { return null; } // Needs Sealed Client Results off for the workspace, so visitor_id comes back in plaintext - const { visitor_id: visitorId, event_id: eventId } = await start({ + const { visitor_id: visitorId } = await start({ apiKey: FINGERPRINT_PUBLIC_KEY, endpoints: FINGERPRINT_ENDPOINTS, }).get({ linkedId: clickId, }); - if (!visitorId || !eventId) { - return null; - } - - try { - const response = await fetch(`${apiHost}/track/identity`, { - method: 'POST', - headers: { - 'Content-Type': 'application/json', - }, - body: JSON.stringify({ - eventId, - }), - }); - - if (response.ok) { - return visitorId; - } - - console.warn( - `[dubAnalytics] trackIdentity failed with status ${response.status}.`, - ); - - // A 4xx won't succeed on retry, so the click is done; a 5xx or 429 is left for the next page load - if ( - response.status >= 400 && - response.status < 500 && - response.status !== 429 - ) { - return visitorId; - } - - return null; - } catch (error) { - console.warn('[dubAnalytics] trackIdentity failed.', error); - - return null; - } + return visitorId || null; } window._dubIdentity = { diff --git a/packages/script/src/identity-agent.test.js b/packages/script/src/identity-agent.test.js index 5882c26..9f1d54d 100644 --- a/packages/script/src/identity-agent.test.js +++ b/packages/script/src/identity-agent.test.js @@ -32,21 +32,12 @@ function identify() { describe('identity-agent.js', () => { beforeEach(() => { vi.stubGlobal('fetch', fetchMock); - vi.spyOn(console, 'warn').mockImplementation(() => undefined); get.mockResolvedValue({ event_id: EVENT_ID, visitor_id: VISITOR_ID, sealed_result: null, }); - - fetchMock.mockResolvedValue( - new Response( - JSON.stringify({ - clickIdentificationId: 'cid_1', - }), - ), - ); }); afterEach(() => { @@ -54,7 +45,7 @@ describe('identity-agent.js', () => { vi.clearAllMocks(); }); - it('identifies with linkedId set to the clickId and posts only the eventId', async () => { + it('identifies with linkedId set to the clickId and resolves the visitorId without calling Dub', async () => { await expect(identify()).resolves.toBe(VISITOR_ID); expect(start).toHaveBeenCalledWith({ @@ -64,21 +55,10 @@ describe('identity-agent.js', () => { expect(get).toHaveBeenCalledWith({ linkedId: CLICK_ID, }); - expect(fetchMock).toHaveBeenCalledWith( - 'https://api.example.com/track/identity', - { - method: 'POST', - headers: { - 'Content-Type': 'application/json', - }, - body: JSON.stringify({ - eventId: EVENT_ID, - }), - }, - ); + expect(fetchMock).not.toHaveBeenCalled(); }); - it('skips the request without a visitor_id, as when Sealed Client Results are on', async () => { + it('resolves null without a visitor_id, as when Sealed Client Results are on', async () => { get.mockResolvedValue({ event_id: EVENT_ID, sealed_result: { @@ -87,63 +67,5 @@ describe('identity-agent.js', () => { }); await expect(identify()).resolves.toBeNull(); - - expect(fetchMock).not.toHaveBeenCalled(); - }); - - it('waits for the request before resolving the visitorId', async () => { - let respond = (_response) => undefined; - - fetchMock.mockReturnValue( - new Promise((resolve) => { - respond = resolve; - }), - ); - - const pending = identify(); - let settled = false; - pending.then(() => { - settled = true; - }); - - await new Promise((resolve) => setTimeout(resolve, 0)); - expect(settled).toBe(false); - - respond(new Response('{}')); - - await expect(pending).resolves.toBe(VISITOR_ID); - }); - - it('resolves the visitorId after a 4xx, since retrying would fail again', async () => { - fetchMock.mockResolvedValue( - new Response('Bad Request', { - status: 400, - }), - ); - - await expect(identify()).resolves.toBe(VISITOR_ID); - - expect(console.warn).toHaveBeenCalledWith( - '[dubAnalytics] trackIdentity failed with status 400.', - ); - }); - - it.each([429, 500, 503])( - 'resolves null after a %i so the next page load retries', - async (status) => { - fetchMock.mockResolvedValue( - new Response('Error', { - status, - }), - ); - - await expect(identify()).resolves.toBeNull(); - }, - ); - - it('resolves null when the request fails to send', async () => { - fetchMock.mockRejectedValue(new TypeError('Failed to fetch')); - - await expect(identify()).resolves.toBeNull(); }); }); From 6d6e47976e5bba01d9854c73085313e749a6af01 Mon Sep 17 00:00:00 2001 From: Kiran K Date: Wed, 18 Nov 2026 17:25:37 +0530 Subject: [PATCH 2/8] Stop passing apiHost to the identity agent The agent no longer calls the Dub API, so the identity extension has no use for API_HOST. --- packages/script/src/extensions/identity.js | 2 -- packages/script/src/extensions/identity.test.js | 1 - packages/script/src/identity-agent.js | 2 +- packages/script/src/identity-agent.test.js | 1 - 4 files changed, 1 insertion(+), 5 deletions(-) diff --git a/packages/script/src/extensions/identity.js b/packages/script/src/extensions/identity.js index a1cdf66..1ab6236 100644 --- a/packages/script/src/extensions/identity.js +++ b/packages/script/src/extensions/identity.js @@ -1,6 +1,5 @@ const initIdentity = () => { const { - a: API_HOST, c: cookieManager, i: DUB_ID_VAR, pc: DUB_PARTNER_COOKIE, @@ -126,7 +125,6 @@ const initIdentity = () => { const identity = await loadIdentityAgent(); const visitorId = await identity.identify({ - apiHost: API_HOST, clickId, }); diff --git a/packages/script/src/extensions/identity.test.js b/packages/script/src/extensions/identity.test.js index a13c754..c11c7a5 100644 --- a/packages/script/src/extensions/identity.test.js +++ b/packages/script/src/extensions/identity.test.js @@ -142,7 +142,6 @@ describe('identity extension', () => { 'https://www.dubcdn.com/analytics/identity-agent.js', ]); expect(identify).toHaveBeenCalledWith({ - apiHost: 'https://api.example.com', clickId: CLICK_ID, }); }); diff --git a/packages/script/src/identity-agent.js b/packages/script/src/identity-agent.js index 36bc99c..539bf66 100644 --- a/packages/script/src/identity-agent.js +++ b/packages/script/src/identity-agent.js @@ -5,7 +5,7 @@ const FINGERPRINT_PUBLIC_KEY = process.env.DUB_FINGERPRINT_PUBLIC_KEY; const FINGERPRINT_ENDPOINTS = ['https://identity.dub.co']; // Loaded on demand by the identity extension; resolves the visitorId once Fingerprint has identified the click -async function identify({ apiHost, clickId }) { +async function identify({ clickId }) { if (!FINGERPRINT_PUBLIC_KEY) { return null; } diff --git a/packages/script/src/identity-agent.test.js b/packages/script/src/identity-agent.test.js index 9f1d54d..37b0cab 100644 --- a/packages/script/src/identity-agent.test.js +++ b/packages/script/src/identity-agent.test.js @@ -24,7 +24,6 @@ const fetchMock = vi.fn(); function identify() { return window._dubIdentity.identify({ - apiHost: 'https://api.example.com', clickId: CLICK_ID, }); } From 268383fc246378cd9e9c6e1ce39d2c7cc024d376 Mon Sep 17 00:00:00 2001 From: Kiran K Date: Wed, 18 Nov 2026 17:30:55 +0530 Subject: [PATCH 3/8] Identify from visitor_id alone in the identity agent test --- packages/script/src/identity-agent.test.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/packages/script/src/identity-agent.test.js b/packages/script/src/identity-agent.test.js index 37b0cab..11951c3 100644 --- a/packages/script/src/identity-agent.test.js +++ b/packages/script/src/identity-agent.test.js @@ -33,9 +33,7 @@ describe('identity-agent.js', () => { vi.stubGlobal('fetch', fetchMock); get.mockResolvedValue({ - event_id: EVENT_ID, visitor_id: VISITOR_ID, - sealed_result: null, }); }); From 49ade6d842a6c151609947657c5a99bdb5fc782f Mon Sep 17 00:00:00 2001 From: Kiran K Date: Wed, 18 Nov 2026 17:48:11 +0530 Subject: [PATCH 4/8] Keep passing apiHost to the identity agent A self-hosted or proxied identity-agent.js can lag behind the script. An older agent still POSTs to ${apiHost}/track/identity, and without apiHost that becomes a relative request to the merchant's own origin. --- packages/script/src/extensions/identity.js | 2 ++ packages/script/src/extensions/identity.test.js | 1 + 2 files changed, 3 insertions(+) diff --git a/packages/script/src/extensions/identity.js b/packages/script/src/extensions/identity.js index 1ab6236..a1cdf66 100644 --- a/packages/script/src/extensions/identity.js +++ b/packages/script/src/extensions/identity.js @@ -1,5 +1,6 @@ const initIdentity = () => { const { + a: API_HOST, c: cookieManager, i: DUB_ID_VAR, pc: DUB_PARTNER_COOKIE, @@ -125,6 +126,7 @@ const initIdentity = () => { const identity = await loadIdentityAgent(); const visitorId = await identity.identify({ + apiHost: API_HOST, clickId, }); diff --git a/packages/script/src/extensions/identity.test.js b/packages/script/src/extensions/identity.test.js index c11c7a5..a13c754 100644 --- a/packages/script/src/extensions/identity.test.js +++ b/packages/script/src/extensions/identity.test.js @@ -142,6 +142,7 @@ describe('identity extension', () => { 'https://www.dubcdn.com/analytics/identity-agent.js', ]); expect(identify).toHaveBeenCalledWith({ + apiHost: 'https://api.example.com', clickId: CLICK_ID, }); }); From acc98bf361a8abe25f311dbb56e2930fee7e2443 Mon Sep 17 00:00:00 2001 From: Kiran K Date: Wed, 18 Nov 2026 17:48:36 +0530 Subject: [PATCH 5/8] Cover a failed Fingerprint call leaving dub_id bare for a retry --- .../script/src/extensions/identity.test.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/packages/script/src/extensions/identity.test.js b/packages/script/src/extensions/identity.test.js index a13c754..a370bf1 100644 --- a/packages/script/src/extensions/identity.test.js +++ b/packages/script/src/extensions/identity.test.js @@ -402,6 +402,25 @@ describe('identity extension', () => { expect(getCookie('dub_id')).toBe(CLICK_ID); }); + it('keeps dub_id unchanged and retries when Fingerprint fails', async () => { + identify.mockRejectedValueOnce(new Error('Failed to fetch')); + setPartnerClick(); + + runScript(); + await expect(window.dubAnalytics.trackIdentity()).resolves.toBeUndefined(); + + expect(getCookie('dub_id')).toBe(CLICK_ID); + expect(console.warn).toHaveBeenCalledWith( + '[dubAnalytics] trackIdentity failed.', + expect.any(Error), + ); + + await window.dubAnalytics.trackIdentity(); + + expect(identify).toHaveBeenCalledTimes(2); + expect(getCookie('dub_id')).toBe(IDENTIFIED_CLICK_ID); + }); + it('never throws when identity-agent.js fails to load', async () => { document.head.appendChild.mockImplementation((node) => { document.head.append(node); From 04424f6576b245196e7fa9553cb5652a0c717e95 Mon Sep 17 00:00:00 2001 From: Kiran K Date: Wed, 18 Nov 2026 17:48:42 +0530 Subject: [PATCH 6/8] Name the identity agent test after the fetch it checks --- packages/script/src/identity-agent.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/script/src/identity-agent.test.js b/packages/script/src/identity-agent.test.js index 11951c3..36ecc42 100644 --- a/packages/script/src/identity-agent.test.js +++ b/packages/script/src/identity-agent.test.js @@ -42,7 +42,7 @@ describe('identity-agent.js', () => { vi.clearAllMocks(); }); - it('identifies with linkedId set to the clickId and resolves the visitorId without calling Dub', async () => { + it('identifies with linkedId set to the clickId and resolves the visitorId without a fetch request', async () => { await expect(identify()).resolves.toBe(VISITOR_ID); expect(start).toHaveBeenCalledWith({ From 27f99a7a288ab0b76ba3ea774358287311a4746c Mon Sep 17 00:00:00 2001 From: Kiran K Date: Wed, 18 Nov 2026 17:48:43 +0530 Subject: [PATCH 7/8] Say the identity agent resolves once Fingerprint identifies the device --- packages/script/src/identity-agent.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/script/src/identity-agent.js b/packages/script/src/identity-agent.js index 539bf66..90aa953 100644 --- a/packages/script/src/identity-agent.js +++ b/packages/script/src/identity-agent.js @@ -4,7 +4,7 @@ const FINGERPRINT_PUBLIC_KEY = process.env.DUB_FINGERPRINT_PUBLIC_KEY; // Dub's Fingerprint custom subdomain, so ad blockers don't stop the agent; Fingerprint's default stays as the fallback const FINGERPRINT_ENDPOINTS = ['https://identity.dub.co']; -// Loaded on demand by the identity extension; resolves the visitorId once Fingerprint has identified the click +// Loaded on demand by the identity extension; resolves the visitorId once Fingerprint has identified the device async function identify({ clickId }) { if (!FINGERPRINT_PUBLIC_KEY) { return null; From dbf84758cc13ba6acec6867e15762539dc164b62 Mon Sep 17 00:00:00 2001 From: Kiran K Date: Wed, 18 Nov 2026 18:15:03 +0530 Subject: [PATCH 8/8] Cover a Fingerprint result with an event_id in the identity agent test --- packages/script/src/identity-agent.test.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/packages/script/src/identity-agent.test.js b/packages/script/src/identity-agent.test.js index 36ecc42..0bd56dc 100644 --- a/packages/script/src/identity-agent.test.js +++ b/packages/script/src/identity-agent.test.js @@ -55,6 +55,17 @@ describe('identity-agent.js', () => { expect(fetchMock).not.toHaveBeenCalled(); }); + it('resolves the visitorId without a fetch request when the result also has an event_id', async () => { + get.mockResolvedValue({ + event_id: EVENT_ID, + visitor_id: VISITOR_ID, + }); + + await expect(identify()).resolves.toBe(VISITOR_ID); + + expect(fetchMock).not.toHaveBeenCalled(); + }); + it('resolves null without a visitor_id, as when Sealed Client Results are on', async () => { get.mockResolvedValue({ event_id: EVENT_ID,