diff --git a/packages/script/src/extensions/identity.js b/packages/script/src/extensions/identity.js index a1cdf66..ca18de2 100644 --- a/packages/script/src/extensions/identity.js +++ b/packages/script/src/extensions/identity.js @@ -108,19 +108,36 @@ const initIdentity = () => { } } - // Links the visitor's device to their click once, then suffixes dub_id with the visitorId - async function identifyClick() { + async function getUnidentifiedPartnerClickId() { await getPendingClick().catch(() => undefined); const clickId = cookieManager.get(DUB_ID_VAR); // Click ids never contain "_", so a suffixed dub_id is already identified if (!clickId || clickId.includes('_')) { - return; + return undefined; } // Fingerprint is only for partner clicks, so skip ad and site-visit clicks, and clicks newer than the partner data if (getPartnerClickId() !== clickId) { + return undefined; + } + + return clickId; + } + + // Loading the agent early lets Fingerprint collect signals before trackIdentity() needs the visitorId + async function preloadIdentityAgent() { + if (await getUnidentifiedPartnerClickId()) { + await loadIdentityAgent(); + } + } + + // Links the visitor's device to their click once, then suffixes dub_id with the visitorId + async function identifyClick() { + const clickId = await getUnidentifiedPartnerClickId(); + + if (!clickId) { return; } @@ -211,6 +228,8 @@ const initIdentity = () => { queueManager.queue = remainingQueue; } + preloadIdentityAgent().catch(() => undefined); + watchIdentityPaths(); }; diff --git a/packages/script/src/extensions/identity.test.js b/packages/script/src/extensions/identity.test.js index a370bf1..c0d4873 100644 --- a/packages/script/src/extensions/identity.test.js +++ b/packages/script/src/extensions/identity.test.js @@ -179,7 +179,7 @@ describe('identity extension', () => { }); await flush(); - expect(loadedIdentityScripts()).toEqual([]); + expect(identify).not.toHaveBeenCalled(); }); it('is not part of variants without the identity extension', async () => { @@ -198,13 +198,49 @@ describe('identity extension', () => { expect(loadedIdentityScripts()).toEqual([]); }); - it('does nothing without data-track-identity-paths', async () => { + it('preloads identity-agent.js for an unidentified partner click without identifying it', async () => { setPartnerClick(); runScript(); + + await vi.waitFor(() => { + expect(loadedIdentityScripts()).toEqual([ + 'https://www.dubcdn.com/analytics/identity-agent.js', + ]); + }); await flush(); - expect(loadedIdentityScripts()).toEqual([]); + expect(identify).not.toHaveBeenCalled(); + expect(getCookie('dub_id')).toBe(CLICK_ID); + }); + + it('reuses the preloaded identity-agent.js when identifying', async () => { + setPartnerClick(); + + runScript(); + await vi.waitFor(() => { + expect(loadedIdentityScripts()).toHaveLength(1); + }); + + await window.dubAnalytics.trackIdentity(); + + expect(identify).toHaveBeenCalledTimes(1); + expect(loadedIdentityScripts()).toHaveLength(1); + }); + + it('never throws when the identity-agent.js preload fails', async () => { + document.head.appendChild.mockImplementation((node) => { + document.head.append(node); + setTimeout(() => node.onerror()); + return node; + }); + setPartnerClick(); + + expect(() => runScript()).not.toThrow(); + await flush(); + await flush(); + + expect(console.warn).not.toHaveBeenCalled(); }); it('ignores data-track-identity-paths that is not an array of paths', async () => { @@ -443,7 +479,7 @@ describe('identity extension', () => { }); await flush(); - expect(loadedIdentityScripts()).toEqual([]); + expect(identify).not.toHaveBeenCalled(); window.history.pushState(null, '', '/signup'); diff --git a/packages/script/src/identity-agent.js b/packages/script/src/identity-agent.js index 90aa953..75e459c 100644 --- a/packages/script/src/identity-agent.js +++ b/packages/script/src/identity-agent.js @@ -4,17 +4,22 @@ 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']; +// Started on load so browser signals are collected before identify() asks for the visitorId +const agent = FINGERPRINT_PUBLIC_KEY + ? start({ + apiKey: FINGERPRINT_PUBLIC_KEY, + endpoints: FINGERPRINT_ENDPOINTS, + }) + : null; + // Loaded on demand by the identity extension; resolves the visitorId once Fingerprint has identified the device async function identify({ clickId }) { - if (!FINGERPRINT_PUBLIC_KEY) { + if (!agent) { return null; } // Needs Sealed Client Results off for the workspace, so visitor_id comes back in plaintext - const { visitor_id: visitorId } = await start({ - apiKey: FINGERPRINT_PUBLIC_KEY, - endpoints: FINGERPRINT_ENDPOINTS, - }).get({ + const { visitor_id: visitorId } = await agent.get({ linkedId: clickId, }); diff --git a/packages/script/src/identity-agent.test.js b/packages/script/src/identity-agent.test.js index 0bd56dc..8f15ec5 100644 --- a/packages/script/src/identity-agent.test.js +++ b/packages/script/src/identity-agent.test.js @@ -21,6 +21,7 @@ vi.mock('@fingerprint/agent', () => ({ })); const fetchMock = vi.fn(); +const startCallsOnLoad = [...start.mock.calls]; function identify() { return window._dubIdentity.identify({ @@ -42,13 +43,28 @@ describe('identity-agent.js', () => { vi.clearAllMocks(); }); + it('starts the agent once when the file loads', async () => { + expect(startCallsOnLoad).toEqual([ + [ + { + apiKey: 'test_public_key', + endpoints: ['https://identity.dub.co'], + }, + ], + ]); + + const startCalls = start.mock.calls.length; + + await identify(); + await identify(); + + expect(start).toHaveBeenCalledTimes(startCalls); + expect(get).toHaveBeenCalledTimes(2); + }); + 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({ - apiKey: 'test_public_key', - endpoints: ['https://identity.dub.co'], - }); expect(get).toHaveBeenCalledWith({ linkedId: CLICK_ID, });