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); diff --git a/packages/script/src/identity-agent.js b/packages/script/src/identity-agent.js index d6810ad..90aa953 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 -async function identify({ apiHost, clickId }) { +// 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; } // 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..0bd56dc 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, }); } @@ -32,21 +31,10 @@ 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 +42,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 a fetch request', async () => { await expect(identify()).resolves.toBe(VISITOR_ID); expect(start).toHaveBeenCalledWith({ @@ -64,86 +52,28 @@ 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 the visitorId without a fetch request when the result also has an event_id', async () => { get.mockResolvedValue({ event_id: EVENT_ID, - sealed_result: { - base64: () => 'noXc7Q==', - }, + visitor_id: VISITOR_ID, }); - await expect(identify()).resolves.toBeNull(); + await expect(identify()).resolves.toBe(VISITOR_ID); 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; + it('resolves null without a visitor_id, as when Sealed Client Results are on', async () => { + get.mockResolvedValue({ + event_id: EVENT_ID, + sealed_result: { + base64: () => 'noXc7Q==', + }, }); - 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(); }); });