Skip to content
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
25 changes: 22 additions & 3 deletions packages/script/src/extensions/identity.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -211,6 +228,8 @@ const initIdentity = () => {
queueManager.queue = remainingQueue;
}

preloadIdentityAgent().catch(() => undefined);

watchIdentityPaths();
};

Expand Down
44 changes: 40 additions & 4 deletions packages/script/src/extensions/identity.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand All @@ -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 () => {
Expand Down Expand Up @@ -443,7 +479,7 @@ describe('identity extension', () => {
});
await flush();

expect(loadedIdentityScripts()).toEqual([]);
expect(identify).not.toHaveBeenCalled();

window.history.pushState(null, '', '/signup');

Expand Down
15 changes: 10 additions & 5 deletions packages/script/src/identity-agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Comment thread
devkiran marked this conversation as resolved.
Resolved
? 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,
});

Expand Down
24 changes: 20 additions & 4 deletions packages/script/src/identity-agent.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ vi.mock('@fingerprint/agent', () => ({
}));

const fetchMock = vi.fn();
const startCallsOnLoad = [...start.mock.calls];

function identify() {
return window._dubIdentity.identify({
Expand All @@ -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,
});
Expand Down
Loading