@jitsu/js 1.9.13-canary.1167.20250215132227 → 1.9.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,22 @@
1
+ <!DOCTYPE html>
2
+
3
+ <html lang="en">
4
+ <head>
5
+ <meta charset="utf-8" />
6
+ <meta name="viewport" content="width=device-width, initial-scale=1" />
7
+
8
+ <title>Tracking page</title>
9
+ <script
10
+ type="text/javascript"
11
+ src="<%=trackingBase%>/p.js"
12
+ data-onload="testOnload"
13
+ data-cookie-names='{"anonymousId":"my_anon_ck"}'
14
+ data-debug="true"
15
+ defer
16
+ ></script>
17
+ </head>
18
+
19
+ <body>
20
+ <h1>Test</h1>
21
+ </body>
22
+ </html>
@@ -482,6 +482,66 @@ test("anonymous-id-bug", async ({ browser }) => {
482
482
  expect(p.body.anonymousId).toEqual(anonymousId);
483
483
  });
484
484
 
485
+ test("cookie-names", async ({ browser }) => {
486
+ clearRequestLog();
487
+ const browserContext = await browser.newContext();
488
+ const { page, uncaughtErrors } = await createLoggingPage(browserContext);
489
+ const pageResult = await page.goto(`${server.baseUrl}/cookie-names.html`);
490
+ await page.waitForFunction(() => window["jitsu"] !== undefined, undefined, {
491
+ timeout: 1000,
492
+ polling: 100,
493
+ });
494
+ expect(pageResult.status()).toBe(200);
495
+ const cookies = (await browserContext.cookies()).reduce(
496
+ (res, cookie) => ({
497
+ ...res,
498
+ [cookie.name]: cookie.value,
499
+ }),
500
+ {}
501
+ );
502
+ console.log("🍪 Jitsu Cookies", cookies);
503
+ expect(cookies).toHaveProperty("my_anon_ck");
504
+ const anonymousId = cookies["my_anon_ck"];
505
+
506
+ //wait for some time since the server has an artificial latency of 30ms
507
+ await new Promise(resolve => setTimeout(resolve, 1000));
508
+ expect(uncaughtErrors.length).toEqual(0);
509
+ console.log(
510
+ `📝 Request log size of ${requestLog.length}`,
511
+ requestLog.map(x => describeEvent(x.type, x.body))
512
+ );
513
+ expect(requestLog[0].body.anonymousId).toEqual(anonymousId);
514
+
515
+ const { page: secondPage } = await createLoggingPage(browserContext);
516
+ const pageResult2 = await secondPage.goto(
517
+ `${server.baseUrl}/cookie-names.html?utm_source=source&utm_medium=medium&utm_campaign=campaign`
518
+ );
519
+ await secondPage.waitForFunction(() => window["jitsu"] !== undefined, undefined, {
520
+ timeout: 1000,
521
+ polling: 100,
522
+ });
523
+ expect(pageResult2.status()).toBe(200);
524
+ const cookies2 = (await browserContext.cookies()).reduce(
525
+ (res, cookie) => ({
526
+ ...res,
527
+ [cookie.name]: cookie.value,
528
+ }),
529
+ {}
530
+ );
531
+ console.log("🍪 Jitsu Cookies", cookies2);
532
+ expect(cookies2).toHaveProperty("my_anon_ck");
533
+ expect(cookies["my_anon_ck"]).toEqual(anonymousId);
534
+
535
+ //wait for some time since the server has an artificial latency of 30ms
536
+ await new Promise(resolve => setTimeout(resolve, 1000));
537
+ expect(uncaughtErrors.length).toEqual(0);
538
+ console.log(
539
+ `📝 Request log size of ${requestLog.length}`,
540
+ requestLog.map(x => describeEvent(x.type, x.body))
541
+ );
542
+ expect(requestLog[1].body.anonymousId).toEqual(anonymousId);
543
+ });
544
+
485
545
  test("basic", async ({ browser }) => {
486
546
  clearRequestLog();
487
547
  const browserContext = await browser.newContext();
@@ -1,7 +1,7 @@
1
1
  import { JitsuOptions, PersistentStorage, RuntimeFacade } from "@jitsu/protocols/analytics";
2
2
  import { AnalyticsPlugin } from "analytics";
3
3
  export declare const parseQuery: (qs?: string) => Record<string, string>;
4
- export type StorageFactory = (cookieDomain: string, cookie2key: Record<string, string>) => PersistentStorage;
4
+ export type StorageFactory = (cookieDomain: string, key2Cookie: (key: string) => string) => PersistentStorage;
5
5
  export declare function windowRuntime(opts: JitsuOptions): RuntimeFacade;
6
6
  export declare const emptyRuntime: (config: JitsuOptions) => RuntimeFacade;
7
7
  export declare function isInBrowser(): boolean;
package/dist/jitsu.cjs.js CHANGED
@@ -1061,6 +1061,7 @@ const defaultConfig = {
1061
1061
  fetch: null,
1062
1062
  echoEvents: false,
1063
1063
  cookieDomain: undefined,
1064
+ cookieNames: {},
1064
1065
  cookieCapture: {},
1065
1066
  runtime: undefined,
1066
1067
  fetchTimeoutMs: undefined,
@@ -1212,30 +1213,30 @@ function setCookie(name, val, { domain, secure }) {
1212
1213
  }
1213
1214
  const defaultCookie2Key = {
1214
1215
  __anon_id: "__eventn_id",
1215
- __user_traits: "__eventn_id_usr",
1216
1216
  __user_id: "__eventn_uid",
1217
+ __user_traits: "__eventn_id_usr",
1217
1218
  __group_id: "__group_id",
1218
1219
  __group_traits: "__group_traits",
1219
1220
  };
1220
1221
  const cookieStorage = (cookieDomain, key2cookie) => {
1221
1222
  return {
1222
1223
  setItem(key, val) {
1224
+ const cookieName = key2cookie(key) || key;
1223
1225
  if (typeof val === "undefined") {
1224
- removeCookie(key2cookie[key] || key, {
1226
+ removeCookie(cookieName, {
1225
1227
  domain: cookieDomain,
1226
1228
  secure: window.location.protocol === "https:",
1227
1229
  });
1228
1230
  return;
1229
1231
  }
1230
1232
  const strVal = typeof val === "object" && val !== null ? encodeURIComponent(JSON.stringify(val)) : val;
1231
- const cookieName = key2cookie[key] || key;
1232
1233
  setCookie(cookieName, strVal, {
1233
1234
  domain: cookieDomain,
1234
1235
  secure: window.location.protocol === "https:",
1235
1236
  });
1236
1237
  },
1237
1238
  getItem(key) {
1238
- const cookieName = key2cookie[key] || key;
1239
+ const cookieName = key2cookie(key) || key;
1239
1240
  const result = getCookie(cookieName);
1240
1241
  if (key === "__anon_id") {
1241
1242
  //anonymous id must always be a string, so we don't parse it to preserve its exact value
@@ -1243,20 +1244,20 @@ const cookieStorage = (cookieDomain, key2cookie) => {
1243
1244
  }
1244
1245
  if (typeof result === "undefined" && key === "__user_id") {
1245
1246
  //backward compatibility with old jitsu cookie. get user id if from traits
1246
- const traits = parse(getCookie("__eventn_id_usr")) || {};
1247
+ const traits = parse(getCookie(key2cookie("__user_traits") || "__eventn_id_usr")) || {};
1247
1248
  return traits.internal_id || traits.user_id || traits.id || traits.userId;
1248
1249
  }
1249
1250
  return parse(result);
1250
1251
  },
1251
1252
  removeItem(key) {
1252
- removeCookie(key2cookie[key] || key, {
1253
+ removeCookie(key2cookie(key) || key, {
1253
1254
  domain: cookieDomain,
1254
1255
  secure: window.location.protocol === "https:",
1255
1256
  });
1256
1257
  },
1257
1258
  reset() {
1258
- for (const v of Object.values(key2cookie)) {
1259
- removeCookie(v, {
1259
+ for (const key of Object.keys(defaultCookie2Key)) {
1260
+ removeCookie(key2cookie(key) || key, {
1260
1261
  domain: cookieDomain,
1261
1262
  secure: window.location.protocol === "https:",
1262
1263
  });
@@ -1265,6 +1266,23 @@ const cookieStorage = (cookieDomain, key2cookie) => {
1265
1266
  };
1266
1267
  };
1267
1268
  function windowRuntime(opts) {
1269
+ const key2Cookie = (key) => {
1270
+ var _a, _b, _c, _d, _e;
1271
+ switch (key) {
1272
+ case "__anon_id":
1273
+ return ((_a = opts.cookieNames) === null || _a === void 0 ? void 0 : _a.anonymousId) || defaultCookie2Key.__anon_id;
1274
+ case "__user_id":
1275
+ return ((_b = opts.cookieNames) === null || _b === void 0 ? void 0 : _b.userId) || defaultCookie2Key.__user_id;
1276
+ case "__user_traits":
1277
+ return ((_c = opts.cookieNames) === null || _c === void 0 ? void 0 : _c.userTraits) || defaultCookie2Key.__user_traits;
1278
+ case "__group_id":
1279
+ return ((_d = opts.cookieNames) === null || _d === void 0 ? void 0 : _d.groupId) || defaultCookie2Key.__group_id;
1280
+ case "__group_traits":
1281
+ return ((_e = opts.cookieNames) === null || _e === void 0 ? void 0 : _e.groupTraits) || defaultCookie2Key.__group_traits;
1282
+ default:
1283
+ return key;
1284
+ }
1285
+ };
1268
1286
  return {
1269
1287
  getCookie(name) {
1270
1288
  const value = `; ${document.cookie}`;
@@ -1287,7 +1305,7 @@ function windowRuntime(opts) {
1287
1305
  return new Date().getTimezoneOffset();
1288
1306
  },
1289
1307
  store() {
1290
- return cookieStorage(opts.cookieDomain || getTopLevelDomain(window.location.hostname), defaultCookie2Key);
1308
+ return cookieStorage(opts.cookieDomain || getTopLevelDomain(window.location.hostname), key2Cookie);
1291
1309
  },
1292
1310
  language() {
1293
1311
  return window.navigator.language;
package/dist/jitsu.es.js CHANGED
@@ -1059,6 +1059,7 @@ const defaultConfig = {
1059
1059
  fetch: null,
1060
1060
  echoEvents: false,
1061
1061
  cookieDomain: undefined,
1062
+ cookieNames: {},
1062
1063
  cookieCapture: {},
1063
1064
  runtime: undefined,
1064
1065
  fetchTimeoutMs: undefined,
@@ -1210,30 +1211,30 @@ function setCookie(name, val, { domain, secure }) {
1210
1211
  }
1211
1212
  const defaultCookie2Key = {
1212
1213
  __anon_id: "__eventn_id",
1213
- __user_traits: "__eventn_id_usr",
1214
1214
  __user_id: "__eventn_uid",
1215
+ __user_traits: "__eventn_id_usr",
1215
1216
  __group_id: "__group_id",
1216
1217
  __group_traits: "__group_traits",
1217
1218
  };
1218
1219
  const cookieStorage = (cookieDomain, key2cookie) => {
1219
1220
  return {
1220
1221
  setItem(key, val) {
1222
+ const cookieName = key2cookie(key) || key;
1221
1223
  if (typeof val === "undefined") {
1222
- removeCookie(key2cookie[key] || key, {
1224
+ removeCookie(cookieName, {
1223
1225
  domain: cookieDomain,
1224
1226
  secure: window.location.protocol === "https:",
1225
1227
  });
1226
1228
  return;
1227
1229
  }
1228
1230
  const strVal = typeof val === "object" && val !== null ? encodeURIComponent(JSON.stringify(val)) : val;
1229
- const cookieName = key2cookie[key] || key;
1230
1231
  setCookie(cookieName, strVal, {
1231
1232
  domain: cookieDomain,
1232
1233
  secure: window.location.protocol === "https:",
1233
1234
  });
1234
1235
  },
1235
1236
  getItem(key) {
1236
- const cookieName = key2cookie[key] || key;
1237
+ const cookieName = key2cookie(key) || key;
1237
1238
  const result = getCookie(cookieName);
1238
1239
  if (key === "__anon_id") {
1239
1240
  //anonymous id must always be a string, so we don't parse it to preserve its exact value
@@ -1241,20 +1242,20 @@ const cookieStorage = (cookieDomain, key2cookie) => {
1241
1242
  }
1242
1243
  if (typeof result === "undefined" && key === "__user_id") {
1243
1244
  //backward compatibility with old jitsu cookie. get user id if from traits
1244
- const traits = parse(getCookie("__eventn_id_usr")) || {};
1245
+ const traits = parse(getCookie(key2cookie("__user_traits") || "__eventn_id_usr")) || {};
1245
1246
  return traits.internal_id || traits.user_id || traits.id || traits.userId;
1246
1247
  }
1247
1248
  return parse(result);
1248
1249
  },
1249
1250
  removeItem(key) {
1250
- removeCookie(key2cookie[key] || key, {
1251
+ removeCookie(key2cookie(key) || key, {
1251
1252
  domain: cookieDomain,
1252
1253
  secure: window.location.protocol === "https:",
1253
1254
  });
1254
1255
  },
1255
1256
  reset() {
1256
- for (const v of Object.values(key2cookie)) {
1257
- removeCookie(v, {
1257
+ for (const key of Object.keys(defaultCookie2Key)) {
1258
+ removeCookie(key2cookie(key) || key, {
1258
1259
  domain: cookieDomain,
1259
1260
  secure: window.location.protocol === "https:",
1260
1261
  });
@@ -1263,6 +1264,23 @@ const cookieStorage = (cookieDomain, key2cookie) => {
1263
1264
  };
1264
1265
  };
1265
1266
  function windowRuntime(opts) {
1267
+ const key2Cookie = (key) => {
1268
+ var _a, _b, _c, _d, _e;
1269
+ switch (key) {
1270
+ case "__anon_id":
1271
+ return ((_a = opts.cookieNames) === null || _a === void 0 ? void 0 : _a.anonymousId) || defaultCookie2Key.__anon_id;
1272
+ case "__user_id":
1273
+ return ((_b = opts.cookieNames) === null || _b === void 0 ? void 0 : _b.userId) || defaultCookie2Key.__user_id;
1274
+ case "__user_traits":
1275
+ return ((_c = opts.cookieNames) === null || _c === void 0 ? void 0 : _c.userTraits) || defaultCookie2Key.__user_traits;
1276
+ case "__group_id":
1277
+ return ((_d = opts.cookieNames) === null || _d === void 0 ? void 0 : _d.groupId) || defaultCookie2Key.__group_id;
1278
+ case "__group_traits":
1279
+ return ((_e = opts.cookieNames) === null || _e === void 0 ? void 0 : _e.groupTraits) || defaultCookie2Key.__group_traits;
1280
+ default:
1281
+ return key;
1282
+ }
1283
+ };
1266
1284
  return {
1267
1285
  getCookie(name) {
1268
1286
  const value = `; ${document.cookie}`;
@@ -1285,7 +1303,7 @@ function windowRuntime(opts) {
1285
1303
  return new Date().getTimezoneOffset();
1286
1304
  },
1287
1305
  store() {
1288
- return cookieStorage(opts.cookieDomain || getTopLevelDomain(window.location.hostname), defaultCookie2Key);
1306
+ return cookieStorage(opts.cookieDomain || getTopLevelDomain(window.location.hostname), key2Cookie);
1289
1307
  },
1290
1308
  language() {
1291
1309
  return window.navigator.language;
package/dist/web/p.js.txt CHANGED
@@ -1062,6 +1062,7 @@
1062
1062
  fetch: null,
1063
1063
  echoEvents: false,
1064
1064
  cookieDomain: undefined,
1065
+ cookieNames: {},
1065
1066
  cookieCapture: {},
1066
1067
  runtime: undefined,
1067
1068
  fetchTimeoutMs: undefined,
@@ -1213,30 +1214,30 @@
1213
1214
  }
1214
1215
  const defaultCookie2Key = {
1215
1216
  __anon_id: "__eventn_id",
1216
- __user_traits: "__eventn_id_usr",
1217
1217
  __user_id: "__eventn_uid",
1218
+ __user_traits: "__eventn_id_usr",
1218
1219
  __group_id: "__group_id",
1219
1220
  __group_traits: "__group_traits",
1220
1221
  };
1221
1222
  const cookieStorage = (cookieDomain, key2cookie) => {
1222
1223
  return {
1223
1224
  setItem(key, val) {
1225
+ const cookieName = key2cookie(key) || key;
1224
1226
  if (typeof val === "undefined") {
1225
- removeCookie(key2cookie[key] || key, {
1227
+ removeCookie(cookieName, {
1226
1228
  domain: cookieDomain,
1227
1229
  secure: window.location.protocol === "https:",
1228
1230
  });
1229
1231
  return;
1230
1232
  }
1231
1233
  const strVal = typeof val === "object" && val !== null ? encodeURIComponent(JSON.stringify(val)) : val;
1232
- const cookieName = key2cookie[key] || key;
1233
1234
  setCookie(cookieName, strVal, {
1234
1235
  domain: cookieDomain,
1235
1236
  secure: window.location.protocol === "https:",
1236
1237
  });
1237
1238
  },
1238
1239
  getItem(key) {
1239
- const cookieName = key2cookie[key] || key;
1240
+ const cookieName = key2cookie(key) || key;
1240
1241
  const result = getCookie(cookieName);
1241
1242
  if (key === "__anon_id") {
1242
1243
  //anonymous id must always be a string, so we don't parse it to preserve its exact value
@@ -1244,20 +1245,20 @@
1244
1245
  }
1245
1246
  if (typeof result === "undefined" && key === "__user_id") {
1246
1247
  //backward compatibility with old jitsu cookie. get user id if from traits
1247
- const traits = parse(getCookie("__eventn_id_usr")) || {};
1248
+ const traits = parse(getCookie(key2cookie("__user_traits") || "__eventn_id_usr")) || {};
1248
1249
  return traits.internal_id || traits.user_id || traits.id || traits.userId;
1249
1250
  }
1250
1251
  return parse(result);
1251
1252
  },
1252
1253
  removeItem(key) {
1253
- removeCookie(key2cookie[key] || key, {
1254
+ removeCookie(key2cookie(key) || key, {
1254
1255
  domain: cookieDomain,
1255
1256
  secure: window.location.protocol === "https:",
1256
1257
  });
1257
1258
  },
1258
1259
  reset() {
1259
- for (const v of Object.values(key2cookie)) {
1260
- removeCookie(v, {
1260
+ for (const key of Object.keys(defaultCookie2Key)) {
1261
+ removeCookie(key2cookie(key) || key, {
1261
1262
  domain: cookieDomain,
1262
1263
  secure: window.location.protocol === "https:",
1263
1264
  });
@@ -1266,6 +1267,23 @@
1266
1267
  };
1267
1268
  };
1268
1269
  function windowRuntime(opts) {
1270
+ const key2Cookie = (key) => {
1271
+ var _a, _b, _c, _d, _e;
1272
+ switch (key) {
1273
+ case "__anon_id":
1274
+ return ((_a = opts.cookieNames) === null || _a === void 0 ? void 0 : _a.anonymousId) || defaultCookie2Key.__anon_id;
1275
+ case "__user_id":
1276
+ return ((_b = opts.cookieNames) === null || _b === void 0 ? void 0 : _b.userId) || defaultCookie2Key.__user_id;
1277
+ case "__user_traits":
1278
+ return ((_c = opts.cookieNames) === null || _c === void 0 ? void 0 : _c.userTraits) || defaultCookie2Key.__user_traits;
1279
+ case "__group_id":
1280
+ return ((_d = opts.cookieNames) === null || _d === void 0 ? void 0 : _d.groupId) || defaultCookie2Key.__group_id;
1281
+ case "__group_traits":
1282
+ return ((_e = opts.cookieNames) === null || _e === void 0 ? void 0 : _e.groupTraits) || defaultCookie2Key.__group_traits;
1283
+ default:
1284
+ return key;
1285
+ }
1286
+ };
1269
1287
  return {
1270
1288
  getCookie(name) {
1271
1289
  const value = `; ${document.cookie}`;
@@ -1288,7 +1306,7 @@
1288
1306
  return new Date().getTimezoneOffset();
1289
1307
  },
1290
1308
  store() {
1291
- return cookieStorage(opts.cookieDomain || getTopLevelDomain(window.location.hostname), defaultCookie2Key);
1309
+ return cookieStorage(opts.cookieDomain || getTopLevelDomain(window.location.hostname), key2Cookie);
1292
1310
  },
1293
1311
  language() {
1294
1312
  return window.navigator.language;
@@ -2072,6 +2090,15 @@
2072
2090
  parse: (arg) => arg,
2073
2091
  });
2074
2092
  const booleanParser = (nestedPath = []) => (Object.assign(Object.assign({}, defaultParser(nestedPath)), { parse: (arg) => arg === "true" || arg === "1" || arg === "yes" }));
2093
+ const jsonParser = (nestedPath = []) => (Object.assign(Object.assign({}, defaultParser(nestedPath)), { parse: (arg) => {
2094
+ try {
2095
+ return JSON.parse(arg);
2096
+ }
2097
+ catch (e) {
2098
+ console.error(`Can't parse Jitsu Option as JSON: ${arg}`);
2099
+ return {};
2100
+ }
2101
+ } }));
2075
2102
  const parsers = {
2076
2103
  debug: booleanParser(),
2077
2104
  "privacy-disable-user-ids": booleanParser(["privacy"]),
@@ -2079,6 +2106,9 @@
2079
2106
  "privacy-ip-policy": defaultParser(["privacy"]),
2080
2107
  "echo-events": booleanParser(),
2081
2108
  "init-only": booleanParser(),
2109
+ "cookie-names": jsonParser(),
2110
+ "cookie-capture": jsonParser(),
2111
+ "default-payload-context": jsonParser(),
2082
2112
  };
2083
2113
  function getParser(name) {
2084
2114
  return parsers[name] || defaultParser();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jitsu/js",
3
- "version": "1.9.13-canary.1167.20250215132227",
3
+ "version": "1.9.14",
4
4
  "description": "",
5
5
  "author": "Jitsu Dev Team <dev@jitsu.com>",
6
6
  "main": "dist/jitsu.cjs.js",
@@ -35,8 +35,8 @@
35
35
  "rollup": "^3.29.5",
36
36
  "ts-jest": "29.0.5",
37
37
  "typescript": "^5.6.3",
38
- "@jitsu/protocols": "1.9.13-canary.1167.20250215132227",
39
- "jsondiffpatch": "1.9.13-canary.1167.20250215132227"
38
+ "@jitsu/protocols": "1.9.14",
39
+ "jsondiffpatch": "1.9.14"
40
40
  },
41
41
  "dependencies": {
42
42
  "analytics": "0.8.9"
@@ -33,6 +33,7 @@ const defaultConfig: Required<JitsuOptions> = {
33
33
  fetch: null,
34
34
  echoEvents: false,
35
35
  cookieDomain: undefined,
36
+ cookieNames: {},
36
37
  cookieCapture: {},
37
38
  runtime: undefined,
38
39
  fetchTimeoutMs: undefined,
@@ -135,7 +136,7 @@ function restoreTraits(storage: PersistentStorage) {
135
136
  };
136
137
  }
137
138
 
138
- export type StorageFactory = (cookieDomain: string, cookie2key: Record<string, string>) => PersistentStorage;
139
+ export type StorageFactory = (cookieDomain: string, key2Cookie: (key: string) => string) => PersistentStorage;
139
140
 
140
141
  function getCookie(name: string) {
141
142
  const value = `; ${document.cookie}`;
@@ -215,8 +216,8 @@ function setCookie(name: string, val: string, { domain, secure }: { domain: stri
215
216
 
216
217
  const defaultCookie2Key = {
217
218
  __anon_id: "__eventn_id",
218
- __user_traits: "__eventn_id_usr",
219
219
  __user_id: "__eventn_uid",
220
+ __user_traits: "__eventn_id_usr",
220
221
  __group_id: "__group_id",
221
222
  __group_traits: "__group_traits",
222
223
  };
@@ -224,22 +225,22 @@ const defaultCookie2Key = {
224
225
  const cookieStorage: StorageFactory = (cookieDomain, key2cookie) => {
225
226
  return {
226
227
  setItem(key: string, val: any) {
228
+ const cookieName = key2cookie(key) || key;
227
229
  if (typeof val === "undefined") {
228
- removeCookie(key2cookie[key] || key, {
230
+ removeCookie(cookieName, {
229
231
  domain: cookieDomain,
230
232
  secure: window.location.protocol === "https:",
231
233
  });
232
234
  return;
233
235
  }
234
236
  const strVal = typeof val === "object" && val !== null ? encodeURIComponent(JSON.stringify(val)) : val;
235
- const cookieName = key2cookie[key] || key;
236
237
  setCookie(cookieName, strVal, {
237
238
  domain: cookieDomain,
238
239
  secure: window.location.protocol === "https:",
239
240
  });
240
241
  },
241
242
  getItem(key: string) {
242
- const cookieName = key2cookie[key] || key;
243
+ const cookieName = key2cookie(key) || key;
243
244
  const result = getCookie(cookieName);
244
245
  if (key === "__anon_id") {
245
246
  //anonymous id must always be a string, so we don't parse it to preserve its exact value
@@ -247,20 +248,20 @@ const cookieStorage: StorageFactory = (cookieDomain, key2cookie) => {
247
248
  }
248
249
  if (typeof result === "undefined" && key === "__user_id") {
249
250
  //backward compatibility with old jitsu cookie. get user id if from traits
250
- const traits = parse(getCookie("__eventn_id_usr")) || {};
251
+ const traits = parse(getCookie(key2cookie("__user_traits") || "__eventn_id_usr")) || {};
251
252
  return traits.internal_id || traits.user_id || traits.id || traits.userId;
252
253
  }
253
254
  return parse(result);
254
255
  },
255
256
  removeItem(key: string) {
256
- removeCookie(key2cookie[key] || key, {
257
+ removeCookie(key2cookie(key) || key, {
257
258
  domain: cookieDomain,
258
259
  secure: window.location.protocol === "https:",
259
260
  });
260
261
  },
261
262
  reset() {
262
- for (const v of Object.values(key2cookie)) {
263
- removeCookie(v, {
263
+ for (const key of Object.keys(defaultCookie2Key)) {
264
+ removeCookie(key2cookie(key) || key, {
264
265
  domain: cookieDomain,
265
266
  secure: window.location.protocol === "https:",
266
267
  });
@@ -270,6 +271,22 @@ const cookieStorage: StorageFactory = (cookieDomain, key2cookie) => {
270
271
  };
271
272
 
272
273
  export function windowRuntime(opts: JitsuOptions): RuntimeFacade {
274
+ const key2Cookie = (key: string) => {
275
+ switch (key) {
276
+ case "__anon_id":
277
+ return opts.cookieNames?.anonymousId || defaultCookie2Key.__anon_id;
278
+ case "__user_id":
279
+ return opts.cookieNames?.userId || defaultCookie2Key.__user_id;
280
+ case "__user_traits":
281
+ return opts.cookieNames?.userTraits || defaultCookie2Key.__user_traits;
282
+ case "__group_id":
283
+ return opts.cookieNames?.groupId || defaultCookie2Key.__group_id;
284
+ case "__group_traits":
285
+ return opts.cookieNames?.groupTraits || defaultCookie2Key.__group_traits;
286
+ default:
287
+ return key;
288
+ }
289
+ };
273
290
  return {
274
291
  getCookie(name: string): string | undefined {
275
292
  const value = `; ${document.cookie}`;
@@ -292,7 +309,7 @@ export function windowRuntime(opts: JitsuOptions): RuntimeFacade {
292
309
  return new Date().getTimezoneOffset();
293
310
  },
294
311
  store(): PersistentStorage {
295
- return cookieStorage(opts.cookieDomain || getTopLevelDomain(window.location.hostname), defaultCookie2Key);
312
+ return cookieStorage(opts.cookieDomain || getTopLevelDomain(window.location.hostname), key2Cookie);
296
313
  },
297
314
 
298
315
  language(): string {
package/src/browser.ts CHANGED
@@ -32,6 +32,18 @@ const booleanParser = (nestedPath: string[] = []) => ({
32
32
  parse: (arg: string) => arg === "true" || arg === "1" || arg === "yes",
33
33
  });
34
34
 
35
+ const jsonParser = (nestedPath: string[] = []) => ({
36
+ ...defaultParser(nestedPath),
37
+ parse: (arg: string) => {
38
+ try {
39
+ return JSON.parse(arg);
40
+ } catch (e) {
41
+ console.error(`Can't parse Jitsu Option as JSON: ${arg}`);
42
+ return {};
43
+ }
44
+ },
45
+ });
46
+
35
47
  const parsers: Partial<Record<string, Parser>> = {
36
48
  debug: booleanParser(),
37
49
  "privacy-disable-user-ids": booleanParser(["privacy"]),
@@ -39,6 +51,9 @@ const parsers: Partial<Record<string, Parser>> = {
39
51
  "privacy-ip-policy": defaultParser(["privacy"]),
40
52
  "echo-events": booleanParser(),
41
53
  "init-only": booleanParser(),
54
+ "cookie-names": jsonParser(),
55
+ "cookie-capture": jsonParser(),
56
+ "default-payload-context": jsonParser(),
42
57
  };
43
58
 
44
59
  function getParser(name: string): Parser {