Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
55 commits
Select commit Hold shift + click to select a range
3c85e60
Add trackIdentity option to link a visitor's Fingerprint device to th…
devkiran Oct 30, 2026
69a3d5b
Add unit tests for trackIdentity
devkiran Oct 30, 2026
fd8b61a
Document the trackIdentity option
devkiran Oct 30, 2026
1960931
Updated
devkiran Oct 30, 2026
fbc162b
Drop stale freshness note from trackIdentity comment
devkiran Nov 2, 2026
9933213
Match the identity response field in trackIdentity test mock
devkiran Nov 2, 2026
62b79f5
Scope trackIdentity to configured paths and client-side navigation
devkiran Nov 2, 2026
23cad32
Expose trackIdentity on window.dubAnalytics and useAnalytics
devkiran Nov 2, 2026
0a3f936
Document trackIdentity paths and method
devkiran Nov 2, 2026
d7fe505
Remove generic.test.ts
devkiran Nov 2, 2026
4bc324c
Describe trackIdentity as partner risk monitoring
devkiran Nov 2, 2026
9876e21
Send the sealed result as identityToken
devkiran Nov 2, 2026
54f07b3
Add identity tracking to the analytics script
devkiran Nov 2, 2026
3bdbcad
Pass trackIdentity paths to the script instead of identifying in the …
devkiran Nov 2, 2026
3c42262
Document trackIdentity for script-tag installs
devkiran Nov 2, 2026
334c790
Ship identity tracking as an identity script variant
devkiran Nov 2, 2026
600df93
Load the identity script variant when trackIdentity is set
devkiran Nov 2, 2026
eeab545
Document the identity script variant
devkiran Nov 2, 2026
9455085
Ignore data-identity-paths that is not an array of paths
devkiran Nov 2, 2026
389d351
Skip trackIdentity in useAnalytics when the identity variant isn't lo…
devkiran Nov 2, 2026
c1a9752
Keep the existing files order in the script package
devkiran Nov 2, 2026
bf8ac23
Fix the identity agent loader comment
devkiran Nov 2, 2026
75098bb
Send the bare clickId when dub_id carries a visitorId suffix
devkiran Nov 4, 2026
0aec2d6
Suffix dub_id with the Fingerprint visitorId instead of sending seale…
devkiran Nov 4, 2026
f89a148
Document the dub_id visitorId suffix
devkiran Nov 4, 2026
a61fa25
Split trackIdentity into an async identifyClick for readability
devkiran Nov 4, 2026
e6d7a46
Drop the visitorId suffix from ?dub_id= before setting the cookie
devkiran Nov 4, 2026
03e2afd
Send the full dub_id in trackLead so the lead is linked to the device
devkiran Nov 5, 2026
3a6039a
Suffix dub_id only after Dub accepts or rejects the identification
devkiran Nov 5, 2026
177d09a
Send the full dub_id in trackSale so a direct sale is linked to the d…
devkiran Nov 5, 2026
e967857
Delete deploy-script.yml
steven-tey Nov 6, 2026
453685f
Fail the script build when an output is missing or empty
devkiran Nov 6, 2026
649adf6
Fail a Cloudflare production build without the Fingerprint key
devkiran Nov 6, 2026
248afa3
Send Fingerprint requests through the identity.dub.co subdomain
devkiran Nov 6, 2026
b0fb568
Remove the verifyEnv test
devkiran Nov 6, 2026
d89e6c9
Remove the verifyOutputs test
devkiran Nov 6, 2026
7613450
Run Playwright on pull requests against a local script build
Joker666 Nov 6, 2026
c86715d
Document the consent rule for trackIdentity
Joker666 Nov 6, 2026
76cadd7
Identify only partner clicks from dub_partner_data
Joker666 Nov 6, 2026
5151467
Document that trackIdentity needs a ?via= partner click
Joker666 Nov 6, 2026
20fb32f
Suffix dub_id in the scope where the click cookie was set
Joker666 Nov 6, 2026
7be07dd
Document that a pending trackIdentity call can't be canceled
Joker666 Nov 6, 2026
929a454
Start the dub_id suffix write at the host-only scope
Joker666 Nov 6, 2026
8140358
Run the analytics script unit tests in CI
Joker666 Nov 6, 2026
ab0e29e
Add a Playwright spec for the identity variant
Joker666 Nov 6, 2026
65fa0b6
Say in the trackIdentity JSDoc that it needs a ?via= partner click
Joker666 Nov 6, 2026
7fdee97
Share the partner cookie name from base.js with the identity extension
Joker666 Nov 6, 2026
b534978
Wait for the script server before the Playwright tests
Joker666 Nov 6, 2026
d2dbd4c
Remove the Playwright install step that pnpm install already covers
Joker666 Nov 6, 2026
a8c60da
Serve the script from the test origin in the identity spec
Joker666 Nov 6, 2026
e06b8f9
Rename data-identity-paths to data-track-identity-paths
Joker666 Nov 9, 2026
92b5af5
Replace the trackIdentity prop object with a trackIdentityPaths array
Joker666 Nov 9, 2026
407a3e2
Document trackIdentityPaths and data-track-identity-paths
Joker666 Nov 9, 2026
816ce5f
Limit the Playwright workflow token to read access
Joker666 Nov 9, 2026
696ad5a
Say that trackIdentityPaths works with any client-side partner query …
Joker666 Nov 9, 2026
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
36 changes: 0 additions & 36 deletions .github/workflows/deploy-script.yml

This file was deleted.

25 changes: 19 additions & 6 deletions .github/workflows/playwright.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
name: Playwright Tests

# Tests the script built from this commit, so a PR is checked before Cloudflare Pages deploys it
on:
repository_dispatch:
types: [analytics-script-deployed]
pull_request:
push:
branches: [main]

permissions:
contents: read

jobs:
test:
Expand All @@ -28,9 +33,17 @@ jobs:
- name: Build workspace dependencies
run: pnpm --filter @dub/analytics build

- name: Install Playwright
working-directory: apps/nextjs
run: pnpm add -D @playwright/test
- name: Build the analytics script
run: pnpm --filter @dub/analytics-script build

- name: Run the analytics script unit tests
run: pnpm --filter @dub/analytics-script test

- name: Serve the analytics script
working-directory: packages/script/dist
run: |
nohup python3 -m http.server 4000 > /dev/null 2>&1 &
curl --silent --fail --retry 10 --retry-connrefused --retry-delay 1 --output /dev/null http://localhost:4000/analytics/script.js

- name: Install Playwright Browsers
working-directory: apps/nextjs
Expand All @@ -40,4 +53,4 @@ jobs:
working-directory: apps/nextjs
run: pnpm exec playwright test --reporter=list
env:
DUB_ANALYTICS_SCRIPT_URL: ${{ github.event.client_payload.deployment_url }}/analytics/script.site-visit.outbound-domains.js
DUB_ANALYTICS_SCRIPT_URL: http://localhost:4000/analytics/script.site-visit.outbound-domains.js
108 changes: 108 additions & 0 deletions apps/nextjs/tests/identity.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import { DUB_ANALYTICS_SCRIPT_URL } from '@/app/constants';
import { expect, test, type Page } from '@playwright/test';

// The folder of the build the other specs use
const BUILD_URL = DUB_ANALYTICS_SCRIPT_URL.replace(/script[^/]*\.js$/, '');
const SITE = 'http://identity.test';
const CLICK_ID = 'idE2EClick01';
const VISITOR_ID = 'idE2EVisitor01';

// Serves a page on a test origin that loads the identity variant for /signup
async function serveSite(page: Page) {
await page.route(`${SITE}/**`, (route) =>
route.fulfill({
contentType: 'text/html',
body: `<!doctype html>
<html>
<head>
<script>
(function (w, da) {
w[da] = w[da] || function () { (w[da].q = w[da].q || []).push(arguments); };
['trackClick', 'trackLead', 'trackSale', 'trackIdentity'].forEach(function (m) {
w[da][m] = function () { (w[da].q = w[da].q || []).push([m, ...Array.from(arguments)]); };
});
})(window, 'dubAnalytics');
</script>
<script
src="${SITE}/analytics/script.identity.js"
defer
data-domains='{"refer":"dub.sh"}'
data-track-identity-paths='["/signup"]'
></script>
</head>
<body>Identity test</body>
</html>`,
}),
);

// Chromium blocks a public origin from loading a script from localhost, so the build is fetched here and served from the test origin.
// Registered after the page route, because Playwright tries the newest matching route first.
await page.route(`${SITE}/analytics/**`, async (route) => {
const file = new URL(route.request().url()).pathname.split('/').pop();
const response = await route.fetch({ url: `${BUILD_URL}${file}` });

await route.fulfill({ response });
});
}

// Stubs the Fingerprint agent, so the spec checks the script without calling Fingerprint or Dub
async function stubIdentityAgent(page: Page) {
const agentRequests: string[] = [];

await page.route('**/analytics/identity-agent.js', (route) => {
agentRequests.push(route.request().url());

return route.fulfill({
contentType: 'application/javascript',
body: `window._dubIdentity = { identify: async () => '${VISITOR_ID}' };`,
});
});

return agentRequests;
}

async function getDubId(page: Page) {
const cookies = await page.context().cookies(SITE);

return cookies.find((cookie) => cookie.name === 'dub_id')?.value;
}

test.describe('Identity tracking', () => {
test('identifies a ?via= partner click on a matching path', async ({
page,
}) => {
await serveSite(page);
const agentRequests = await stubIdentityAgent(page);

await page.route('**/track/click', (route) =>
route.fulfill({
contentType: 'application/json',
body: JSON.stringify({
clickId: CLICK_ID,
partner: { id: 'pn_e2e', name: 'Partner', image: null },
discount: null,
}),
}),
);

await page.goto(`${SITE}/signup?via=partner`);

await expect.poll(() => getDubId(page)).toBe(`${CLICK_ID}_${VISITOR_ID}`);
expect(agentRequests).toHaveLength(1);
});

test('never loads the agent for a click without partner data', async ({
page,
}) => {
await serveSite(page);
const agentRequests = await stubIdentityAgent(page);

await page.goto(`${SITE}/signup?dub_id=${CLICK_ID}`);
await expect.poll(() => getDubId(page)).toBe(CLICK_ID);

await page.evaluate(() => (window as any).dubAnalytics.trackIdentity());

expect(agentRequests).toHaveLength(0);
expect(await getDubId(page)).toBe(CLICK_ID);
});
});
170 changes: 62 additions & 108 deletions packages/script/build.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const esbuild = require('esbuild');
const fs = require('fs');
const path = require('path');
const { verifyEnv } = require('./verify-env');
const { verifyOutputs } = require('./verify-outputs');

const baseConfig = {
bundle: true,
Expand All @@ -9,12 +11,39 @@ const baseConfig = {
target: 'es2015',
};

// Order matters: variant names list extensions in this order (e.g. script.site-visit.identity.js)
const EXTENSIONS = [
'site-visit',
'outbound-domains',
'conversion-tracking',
'identity',
];

// Helper to read and combine files
const combineFiles = (files) => {
// Just concatenate the files, esbuild will handle the IIFE wrapping
return files.map((file) => fs.readFileSync(file, 'utf8')).join('\n');
};

// Every subset of EXTENSIONS, keeping their order
const combinations = EXTENSIONS.reduce(
(subsets, extension) => [
...subsets,
...subsets.map((subset) => [...subset, extension]),
],
[[]],
);

const variantFile = (extensions) =>
`dist/analytics/${['script', ...extensions, 'js'].join('.')}`;

try {
verifyEnv(process.env);
} catch (error) {
console.error(error.message);
process.exit(1);
}

// Ensure dist/analytics exists
fs.mkdirSync(path.join(__dirname, 'dist/analytics'), { recursive: true });

Expand All @@ -24,117 +53,42 @@ fs.copyFileSync(
path.join(__dirname, 'dist/_redirects'),
);

// Build all variants (8 total combinations of 3 extensions)
// Build all variants (16 combinations of 4 extensions), plus the identity agent they load on demand
Promise.all([
// 1. Base script (no extensions)
esbuild.build({
...baseConfig,
stdin: {
contents: fs.readFileSync('src/base.js', 'utf8'),
resolveDir: __dirname,
sourcefile: 'base.js',
},
outfile: 'dist/analytics/script.js',
}),

// 2. Site visit only
esbuild.build({
...baseConfig,
stdin: {
contents: combineFiles(['src/base.js', 'src/extensions/site-visit.js']),
resolveDir: __dirname,
sourcefile: 'combined.js',
},
outfile: 'dist/analytics/script.site-visit.js',
}),

// 3. Outbound domains only
esbuild.build({
...baseConfig,
stdin: {
contents: combineFiles([
'src/base.js',
'src/extensions/outbound-domains.js',
]),
resolveDir: __dirname,
sourcefile: 'combined.js',
},
outfile: 'dist/analytics/script.outbound-domains.js',
}),

// 4. Conversion tracking only
esbuild.build({
...baseConfig,
stdin: {
contents: combineFiles([
'src/base.js',
'src/extensions/conversion-tracking.js',
]),
resolveDir: __dirname,
sourcefile: 'combined.js',
},
outfile: 'dist/analytics/script.conversion-tracking.js',
}),

// 5. Site visit + Outbound domains
esbuild.build({
...baseConfig,
stdin: {
contents: combineFiles([
'src/base.js',
'src/extensions/site-visit.js',
'src/extensions/outbound-domains.js',
]),
resolveDir: __dirname,
sourcefile: 'combined.js',
},
outfile: 'dist/analytics/script.site-visit.outbound-domains.js',
}),

// 6. Site visit + Conversion tracking
esbuild.build({
...baseConfig,
stdin: {
contents: combineFiles([
'src/base.js',
'src/extensions/site-visit.js',
'src/extensions/conversion-tracking.js',
]),
resolveDir: __dirname,
sourcefile: 'combined.js',
},
outfile: 'dist/analytics/script.site-visit.conversion-tracking.js',
}),

// 7. Outbound domains + Conversion tracking
esbuild.build({
...baseConfig,
stdin: {
contents: combineFiles([
'src/base.js',
'src/extensions/outbound-domains.js',
'src/extensions/conversion-tracking.js',
]),
resolveDir: __dirname,
sourcefile: 'combined.js',
},
outfile: 'dist/analytics/script.outbound-domains.conversion-tracking.js',
}),
...combinations.map((extensions) =>
esbuild.build({
...baseConfig,
stdin: {
contents: combineFiles([
'src/base.js',
...extensions.map((extension) => `src/extensions/${extension}.js`),
]),
resolveDir: __dirname,
sourcefile: extensions.length ? 'combined.js' : 'base.js',
},
outfile: variantFile(extensions),
}),
),

// 8. All extensions combined
esbuild.build({
...baseConfig,
stdin: {
contents: combineFiles([
'src/base.js',
'src/extensions/site-visit.js',
'src/extensions/outbound-domains.js',
'src/extensions/conversion-tracking.js',
]),
resolveDir: __dirname,
sourcefile: 'combined.js',
entryPoints: ['src/identity-agent.js'],
define: {
'process.env.DUB_FINGERPRINT_PUBLIC_KEY': JSON.stringify(
process.env.DUB_FINGERPRINT_PUBLIC_KEY ?? '',
),
},
outfile:
'dist/analytics/script.site-visit.outbound-domains.conversion-tracking.js',
outfile: 'dist/analytics/identity-agent.js',
}),
]).catch(() => process.exit(1));
])
.then(() =>
verifyOutputs([
...combinations.map(variantFile),
'dist/analytics/identity-agent.js',
'dist/_redirects',
]),
)
.catch((error) => {
console.error(error.message);
process.exit(1);
});
Loading
Loading