Skip to content

Commit

Permalink
fix (@jitsu/js): Preserve anonymous ID as a string and avoid parsing (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
dmeremyanin authored Dec 17, 2024
1 parent 66d528b commit 61c168e
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
26 changes: 26 additions & 0 deletions libs/jitsu-js/__tests__/playwright/cases/anonymous-id-bug.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<!DOCTYPE html>

<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />

<title>Tracking page</title>
<script>
window.testOnload = async j => {
j.track("pageLoaded");
};
</script>
<script
type="text/javascript"
src="<%=trackingBase%>/p.js"
data-onload="testOnload"
data-debug="true"
defer
></script>
</head>

<body>
<h1>Test</h1>
</body>
</html>
32 changes: 32 additions & 0 deletions libs/jitsu-js/__tests__/playwright/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,38 @@ test("disable-user-ids-then-consent", async ({ browser }) => {
expect((p.body.anonymousId ?? "").length).toBeGreaterThan(0);
});

test("anonymous-id-bug", async ({ browser }) => {
clearRequestLog();
const anonymousId = "1724633695283.638279";
const browserContext = await browser.newContext();
await browserContext.addCookies([{ name: "__eventn_id", value: anonymousId, url: server.baseUrl }]);
const { page, uncaughtErrors } = await createLoggingPage(browserContext);
const [pageResult] = await Promise.all([page.goto(`${server.baseUrl}/anonymous-id-bug.html`)]);
await page.waitForFunction(() => window["jitsu"] !== undefined, undefined, {
timeout: 1000,
polling: 100,
});
expect(pageResult.status()).toBe(200);
const cookies = (await browserContext.cookies()).reduce(
(res, cookie) => ({
...res,
[cookie.name]: cookie.value,
}),
{}
);
console.log("🍪 Jitsu Cookies", cookies);
//wait for some time since the server has an artificial latency of 30ms
await new Promise(resolve => setTimeout(resolve, 1000));
expect(uncaughtErrors.length).toEqual(0);
console.log(
`📝 Request log size of ${requestLog.length}`,
requestLog.map(x => describeEvent(x.type, x.body))
);
const p = requestLog[0];
console.log(chalk.bold("📝 Checking page event"), JSON.stringify(p, null, 3));
expect(p.body.anonymousId).toEqual(anonymousId);
});

test("basic", async ({ browser }) => {
clearRequestLog();
const browserContext = await browser.newContext();
Expand Down
4 changes: 4 additions & 0 deletions libs/jitsu-js/src/analytics-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,10 @@ const cookieStorage: StorageFactory = (cookieDomain, key2cookie) => {
getItem(key: string) {
const cookieName = key2cookie[key] || key;
const result = getCookie(cookieName);
if (key === "__anon_id") {
//anonymous id must always be a string, so we don't parse it to preserve its exact value
return result;
}
if (typeof result === "undefined" && key === "__user_id") {
//backward compatibility with old jitsu cookie. get user id if from traits
const traits = parse(getCookie("__eventn_id_usr")) || {};
Expand Down

0 comments on commit 61c168e

Please sign in to comment.