@jitsu/js 0.0.0-alpha.98 → 0.0.1-alpha.120
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.
- package/.turbo/turbo-build.log +3 -3
- package/.turbo/turbo-clean.log +1 -1
- package/__tests__/node/nodejs.test.ts +2 -2
- package/__tests__/playwright/integration.test.ts +2 -2
- package/dist/jitsu.cjs.js +30 -17
- package/dist/jitsu.d.ts +1 -1
- package/dist/jitsu.es.js +30 -17
- package/dist/web/p.js.txt +30 -17
- package/package.json +4 -4
- package/src/analytics-plugin.ts +37 -18
- package/src/index.ts +2 -2
- package/src/jitsu.d.ts +1 -1
package/.turbo/turbo-build.log
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
@jitsu/js:build: cache hit, replaying output [
|
|
1
|
+
@jitsu/js:build: cache hit, replaying output [2md0a9e8b6bb4cbc1a[0m
|
|
2
2
|
[34m@jitsu/js:build: [0m
|
|
3
3
|
[34m@jitsu/js:build: [0m> @jitsu/js@0.0.0 build /Users/ildarnurislamov/Projects/onetag/libs/jitsu-js
|
|
4
4
|
[34m@jitsu/js:build: [0m> tsc -p . && rollup -c && cp src/jitsu.d.ts dist
|
|
@@ -7,9 +7,9 @@
|
|
|
7
7
|
[34m@jitsu/js:build: [0m[1m./compiled/src/browser.js[22m → [1mdist/web/p.js.txt[22m...[39m
|
|
8
8
|
[34m@jitsu/js:build: [0m[1m[33m(!) Circular dependency[39m[22m
|
|
9
9
|
[34m@jitsu/js:build: [0mcompiled/src/index.js -> compiled/src/analytics-plugin.js -> compiled/src/index.js
|
|
10
|
-
[34m@jitsu/js:build: [0m[32mcreated [1mdist/web/p.js.txt[22m in [
|
|
10
|
+
[34m@jitsu/js:build: [0m[32mcreated [1mdist/web/p.js.txt[22m in [1m218ms[22m[39m
|
|
11
11
|
[34m@jitsu/js:build: [0m[36m
|
|
12
12
|
[34m@jitsu/js:build: [0m[1m./compiled/src/index.js[22m → [1mdist/jitsu.es.js, dist/jitsu.cjs.js[22m...[39m
|
|
13
13
|
[34m@jitsu/js:build: [0m[1m[33m(!) Circular dependency[39m[22m
|
|
14
14
|
[34m@jitsu/js:build: [0mcompiled/src/index.js -> compiled/src/analytics-plugin.js -> compiled/src/index.js
|
|
15
|
-
[34m@jitsu/js:build: [0m[32mcreated [1mdist/jitsu.es.js, dist/jitsu.cjs.js[22m in [
|
|
15
|
+
[34m@jitsu/js:build: [0m[32mcreated [1mdist/jitsu.es.js, dist/jitsu.cjs.js[22m in [1m119ms[22m[39m
|
package/.turbo/turbo-clean.log
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
@jitsu/js:clean: cache hit, replaying output [
|
|
1
|
+
@jitsu/js:clean: cache hit, replaying output [2ma743e35d3f6db643[0m
|
|
2
2
|
[34m@jitsu/js:clean: [0m
|
|
3
3
|
[34m@jitsu/js:clean: [0m> @jitsu/js@0.0.0 clean /Users/ildarnurislamov/Projects/onetag/libs/jitsu-js
|
|
4
4
|
[34m@jitsu/js:clean: [0m> rm -rf ./dist
|
|
@@ -2,7 +2,7 @@ import fs from "fs";
|
|
|
2
2
|
import path from "path";
|
|
3
3
|
import { createServer, SimpleSyrup } from "../simple-syrup";
|
|
4
4
|
import process from "process";
|
|
5
|
-
import { AnalyticsClientEvent, AnalyticsInterface } from "@jitsu/types/analytics";
|
|
5
|
+
import { AnalyticsClientEvent, AnalyticsInterface } from "@jitsu/analytics-types/analytics";
|
|
6
6
|
|
|
7
7
|
const jitsuAnalytics = require("../../dist/jitsu.cjs.js").jitsuAnalytics;
|
|
8
8
|
const fetchImpl = require("node-fetch-commonjs");
|
|
@@ -17,7 +17,7 @@ describe("Test Jitsu NodeJS client", () => {
|
|
|
17
17
|
port: 3088,
|
|
18
18
|
https: false,
|
|
19
19
|
handlers: {
|
|
20
|
-
"/s/:type": (req, res) => {
|
|
20
|
+
"/api/s/:type": (req, res) => {
|
|
21
21
|
res.setHeader("Content-Type", "text/javascript");
|
|
22
22
|
res.send({ ok: true });
|
|
23
23
|
requestLog.push({
|
|
@@ -6,7 +6,7 @@ import ejs from "ejs";
|
|
|
6
6
|
import chalk from "chalk";
|
|
7
7
|
import * as process from "process";
|
|
8
8
|
import * as console from "console";
|
|
9
|
-
import { AnalyticsClientEvent, AnalyticsInterface } from "@jitsu/types/analytics.d";
|
|
9
|
+
import { AnalyticsClientEvent, AnalyticsInterface } from "@jitsu/analytics-types/analytics.d";
|
|
10
10
|
import type { AnalyticsCore } from "@segment/analytics-next/src/core/analytics/interfaces";
|
|
11
11
|
import { response } from "express";
|
|
12
12
|
|
|
@@ -49,7 +49,7 @@ test.beforeAll(async () => {
|
|
|
49
49
|
res.setHeader("Content-Type", "text/javascript");
|
|
50
50
|
res.send(fs.readFileSync(path.join(__dirname, "../../dist/web/p.js.txt")).toString());
|
|
51
51
|
},
|
|
52
|
-
"/s/:type": (req, res) => {
|
|
52
|
+
"/api/s/:type": (req, res) => {
|
|
53
53
|
res.setHeader("Content-Type", "text/javascript");
|
|
54
54
|
res.send({ ok: true });
|
|
55
55
|
requestLog.push({
|
package/dist/jitsu.cjs.js
CHANGED
|
@@ -166,6 +166,11 @@ const cookieStorage = (cookieDomain, key2cookie) => {
|
|
|
166
166
|
getItem(key) {
|
|
167
167
|
const cookieName = key2cookie[key] || key;
|
|
168
168
|
const result = getCookie(cookieName);
|
|
169
|
+
if (typeof result === "undefined" && key === "__user_id") {
|
|
170
|
+
//backward compatibility with old jitsu cookie. get user id if from traits
|
|
171
|
+
const traits = parse(getCookie("__eventn_id_usr")) || {};
|
|
172
|
+
return traits.internal_id || traits.user_id || traits.id || traits.userId;
|
|
173
|
+
}
|
|
169
174
|
return parse(result);
|
|
170
175
|
},
|
|
171
176
|
removeItem(key) {
|
|
@@ -202,7 +207,7 @@ function windowRuntime(opts) {
|
|
|
202
207
|
height: window.screen.height,
|
|
203
208
|
innerWidth: window.innerWidth,
|
|
204
209
|
innerHeight: window.innerHeight,
|
|
205
|
-
density: window.devicePixelRatio,
|
|
210
|
+
density: Math.floor(window.devicePixelRatio),
|
|
206
211
|
};
|
|
207
212
|
},
|
|
208
213
|
userAgent() {
|
|
@@ -272,9 +277,12 @@ function deepMerge(target, source) {
|
|
|
272
277
|
return acc;
|
|
273
278
|
}, target);
|
|
274
279
|
}
|
|
280
|
+
function isInBrowser() {
|
|
281
|
+
return typeof document !== "undefined";
|
|
282
|
+
}
|
|
275
283
|
function adjustPayload(payload, config, storage) {
|
|
276
284
|
var _a, _b;
|
|
277
|
-
const runtime = config.runtime || (
|
|
285
|
+
const runtime = config.runtime || (isInBrowser() ? windowRuntime(config) : emptyRuntime(config));
|
|
278
286
|
const url = runtime.pageUrl();
|
|
279
287
|
const parsedUrl = safeCall(() => new URL(url), undefined);
|
|
280
288
|
const query = parsedUrl ? parseQuery(parsedUrl.search) : {};
|
|
@@ -303,7 +311,7 @@ function adjustPayload(payload, config, storage) {
|
|
|
303
311
|
},
|
|
304
312
|
campaign: parseUtms(query),
|
|
305
313
|
};
|
|
306
|
-
const withContext = Object.assign(Object.assign({}, payload), { timestamp: new Date().toISOString(), sentAt: new Date().toISOString(), messageId: randomId(), context: deepMerge(context, customContext) });
|
|
314
|
+
const withContext = Object.assign(Object.assign({}, payload), { timestamp: new Date().toISOString(), sentAt: new Date().toISOString(), messageId: randomId(properties.path || (parsedUrl && parsedUrl.pathname)), context: deepMerge(context, customContext) });
|
|
307
315
|
delete withContext.meta;
|
|
308
316
|
delete withContext.options;
|
|
309
317
|
return withContext;
|
|
@@ -386,21 +394,26 @@ const jitsuAnalyticsPlugin = (pluginConfig = {}) => {
|
|
|
386
394
|
},
|
|
387
395
|
};
|
|
388
396
|
};
|
|
389
|
-
function
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
}
|
|
394
|
-
return id;
|
|
397
|
+
function getSeed() {
|
|
398
|
+
var _a;
|
|
399
|
+
const defaultSeed = Date.now() % 2147483647;
|
|
400
|
+
return isInBrowser() ? ((_a = window === null || window === void 0 ? void 0 : window.performance) === null || _a === void 0 ? void 0 : _a.now()) || defaultSeed : defaultSeed;
|
|
395
401
|
}
|
|
396
|
-
function
|
|
397
|
-
const
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
402
|
+
function randomId(hashString = "") {
|
|
403
|
+
const d = Date.now();
|
|
404
|
+
return (((Math.random() * d * hash(hashString !== null && hashString !== void 0 ? hashString : "", getSeed())) % Number.MAX_SAFE_INTEGER).toString(36) +
|
|
405
|
+
((Math.random() * d * hash(hashString !== null && hashString !== void 0 ? hashString : "", getSeed())) % Number.MAX_SAFE_INTEGER).toString(36));
|
|
406
|
+
}
|
|
407
|
+
function hash(str, seed = 0) {
|
|
408
|
+
let h1 = 0xdeadbeef ^ seed, h2 = 0x41c6ce57 ^ seed;
|
|
409
|
+
for (let i = 0, ch; i < str.length; i++) {
|
|
410
|
+
ch = str.charCodeAt(i);
|
|
411
|
+
h1 = Math.imul(h1 ^ ch, 2654435761);
|
|
412
|
+
h2 = Math.imul(h2 ^ ch, 1597334677);
|
|
403
413
|
}
|
|
414
|
+
h1 = Math.imul(h1 ^ (h1 >>> 16), 2246822507) ^ Math.imul(h2 ^ (h2 >>> 13), 3266489909);
|
|
415
|
+
h2 = Math.imul(h2 ^ (h2 >>> 16), 2246822507) ^ Math.imul(h1 ^ (h1 >>> 13), 3266489909);
|
|
416
|
+
return 4294967296 * (2097151 & h2) + (h1 >>> 0);
|
|
404
417
|
}
|
|
405
418
|
|
|
406
419
|
function parse(input) {
|
|
@@ -409,7 +422,7 @@ function parse(input) {
|
|
|
409
422
|
value = decodeURIComponent(input);
|
|
410
423
|
}
|
|
411
424
|
try {
|
|
412
|
-
value = JSON.parse(
|
|
425
|
+
value = JSON.parse(value);
|
|
413
426
|
if (value === "true")
|
|
414
427
|
return true;
|
|
415
428
|
if (value === "false")
|
package/dist/jitsu.d.ts
CHANGED
package/dist/jitsu.es.js
CHANGED
|
@@ -164,6 +164,11 @@ const cookieStorage = (cookieDomain, key2cookie) => {
|
|
|
164
164
|
getItem(key) {
|
|
165
165
|
const cookieName = key2cookie[key] || key;
|
|
166
166
|
const result = getCookie(cookieName);
|
|
167
|
+
if (typeof result === "undefined" && key === "__user_id") {
|
|
168
|
+
//backward compatibility with old jitsu cookie. get user id if from traits
|
|
169
|
+
const traits = parse(getCookie("__eventn_id_usr")) || {};
|
|
170
|
+
return traits.internal_id || traits.user_id || traits.id || traits.userId;
|
|
171
|
+
}
|
|
167
172
|
return parse(result);
|
|
168
173
|
},
|
|
169
174
|
removeItem(key) {
|
|
@@ -200,7 +205,7 @@ function windowRuntime(opts) {
|
|
|
200
205
|
height: window.screen.height,
|
|
201
206
|
innerWidth: window.innerWidth,
|
|
202
207
|
innerHeight: window.innerHeight,
|
|
203
|
-
density: window.devicePixelRatio,
|
|
208
|
+
density: Math.floor(window.devicePixelRatio),
|
|
204
209
|
};
|
|
205
210
|
},
|
|
206
211
|
userAgent() {
|
|
@@ -270,9 +275,12 @@ function deepMerge(target, source) {
|
|
|
270
275
|
return acc;
|
|
271
276
|
}, target);
|
|
272
277
|
}
|
|
278
|
+
function isInBrowser() {
|
|
279
|
+
return typeof document !== "undefined";
|
|
280
|
+
}
|
|
273
281
|
function adjustPayload(payload, config, storage) {
|
|
274
282
|
var _a, _b;
|
|
275
|
-
const runtime = config.runtime || (
|
|
283
|
+
const runtime = config.runtime || (isInBrowser() ? windowRuntime(config) : emptyRuntime(config));
|
|
276
284
|
const url = runtime.pageUrl();
|
|
277
285
|
const parsedUrl = safeCall(() => new URL(url), undefined);
|
|
278
286
|
const query = parsedUrl ? parseQuery(parsedUrl.search) : {};
|
|
@@ -301,7 +309,7 @@ function adjustPayload(payload, config, storage) {
|
|
|
301
309
|
},
|
|
302
310
|
campaign: parseUtms(query),
|
|
303
311
|
};
|
|
304
|
-
const withContext = Object.assign(Object.assign({}, payload), { timestamp: new Date().toISOString(), sentAt: new Date().toISOString(), messageId: randomId(), context: deepMerge(context, customContext) });
|
|
312
|
+
const withContext = Object.assign(Object.assign({}, payload), { timestamp: new Date().toISOString(), sentAt: new Date().toISOString(), messageId: randomId(properties.path || (parsedUrl && parsedUrl.pathname)), context: deepMerge(context, customContext) });
|
|
305
313
|
delete withContext.meta;
|
|
306
314
|
delete withContext.options;
|
|
307
315
|
return withContext;
|
|
@@ -384,21 +392,26 @@ const jitsuAnalyticsPlugin = (pluginConfig = {}) => {
|
|
|
384
392
|
},
|
|
385
393
|
};
|
|
386
394
|
};
|
|
387
|
-
function
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
}
|
|
392
|
-
return id;
|
|
395
|
+
function getSeed() {
|
|
396
|
+
var _a;
|
|
397
|
+
const defaultSeed = Date.now() % 2147483647;
|
|
398
|
+
return isInBrowser() ? ((_a = window === null || window === void 0 ? void 0 : window.performance) === null || _a === void 0 ? void 0 : _a.now()) || defaultSeed : defaultSeed;
|
|
393
399
|
}
|
|
394
|
-
function
|
|
395
|
-
const
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
400
|
+
function randomId(hashString = "") {
|
|
401
|
+
const d = Date.now();
|
|
402
|
+
return (((Math.random() * d * hash(hashString !== null && hashString !== void 0 ? hashString : "", getSeed())) % Number.MAX_SAFE_INTEGER).toString(36) +
|
|
403
|
+
((Math.random() * d * hash(hashString !== null && hashString !== void 0 ? hashString : "", getSeed())) % Number.MAX_SAFE_INTEGER).toString(36));
|
|
404
|
+
}
|
|
405
|
+
function hash(str, seed = 0) {
|
|
406
|
+
let h1 = 0xdeadbeef ^ seed, h2 = 0x41c6ce57 ^ seed;
|
|
407
|
+
for (let i = 0, ch; i < str.length; i++) {
|
|
408
|
+
ch = str.charCodeAt(i);
|
|
409
|
+
h1 = Math.imul(h1 ^ ch, 2654435761);
|
|
410
|
+
h2 = Math.imul(h2 ^ ch, 1597334677);
|
|
401
411
|
}
|
|
412
|
+
h1 = Math.imul(h1 ^ (h1 >>> 16), 2246822507) ^ Math.imul(h2 ^ (h2 >>> 13), 3266489909);
|
|
413
|
+
h2 = Math.imul(h2 ^ (h2 >>> 16), 2246822507) ^ Math.imul(h1 ^ (h1 >>> 13), 3266489909);
|
|
414
|
+
return 4294967296 * (2097151 & h2) + (h1 >>> 0);
|
|
402
415
|
}
|
|
403
416
|
|
|
404
417
|
function parse(input) {
|
|
@@ -407,7 +420,7 @@ function parse(input) {
|
|
|
407
420
|
value = decodeURIComponent(input);
|
|
408
421
|
}
|
|
409
422
|
try {
|
|
410
|
-
value = JSON.parse(
|
|
423
|
+
value = JSON.parse(value);
|
|
411
424
|
if (value === "true")
|
|
412
425
|
return true;
|
|
413
426
|
if (value === "false")
|
package/dist/web/p.js.txt
CHANGED
|
@@ -167,6 +167,11 @@
|
|
|
167
167
|
getItem(key) {
|
|
168
168
|
const cookieName = key2cookie[key] || key;
|
|
169
169
|
const result = getCookie(cookieName);
|
|
170
|
+
if (typeof result === "undefined" && key === "__user_id") {
|
|
171
|
+
//backward compatibility with old jitsu cookie. get user id if from traits
|
|
172
|
+
const traits = parse(getCookie("__eventn_id_usr")) || {};
|
|
173
|
+
return traits.internal_id || traits.user_id || traits.id || traits.userId;
|
|
174
|
+
}
|
|
170
175
|
return parse(result);
|
|
171
176
|
},
|
|
172
177
|
removeItem(key) {
|
|
@@ -203,7 +208,7 @@
|
|
|
203
208
|
height: window.screen.height,
|
|
204
209
|
innerWidth: window.innerWidth,
|
|
205
210
|
innerHeight: window.innerHeight,
|
|
206
|
-
density: window.devicePixelRatio,
|
|
211
|
+
density: Math.floor(window.devicePixelRatio),
|
|
207
212
|
};
|
|
208
213
|
},
|
|
209
214
|
userAgent() {
|
|
@@ -273,9 +278,12 @@
|
|
|
273
278
|
return acc;
|
|
274
279
|
}, target);
|
|
275
280
|
}
|
|
281
|
+
function isInBrowser() {
|
|
282
|
+
return typeof document !== "undefined";
|
|
283
|
+
}
|
|
276
284
|
function adjustPayload(payload, config, storage) {
|
|
277
285
|
var _a, _b;
|
|
278
|
-
const runtime = config.runtime || (
|
|
286
|
+
const runtime = config.runtime || (isInBrowser() ? windowRuntime(config) : emptyRuntime(config));
|
|
279
287
|
const url = runtime.pageUrl();
|
|
280
288
|
const parsedUrl = safeCall(() => new URL(url), undefined);
|
|
281
289
|
const query = parsedUrl ? parseQuery(parsedUrl.search) : {};
|
|
@@ -304,7 +312,7 @@
|
|
|
304
312
|
},
|
|
305
313
|
campaign: parseUtms(query),
|
|
306
314
|
};
|
|
307
|
-
const withContext = Object.assign(Object.assign({}, payload), { timestamp: new Date().toISOString(), sentAt: new Date().toISOString(), messageId: randomId(), context: deepMerge(context, customContext) });
|
|
315
|
+
const withContext = Object.assign(Object.assign({}, payload), { timestamp: new Date().toISOString(), sentAt: new Date().toISOString(), messageId: randomId(properties.path || (parsedUrl && parsedUrl.pathname)), context: deepMerge(context, customContext) });
|
|
308
316
|
delete withContext.meta;
|
|
309
317
|
delete withContext.options;
|
|
310
318
|
return withContext;
|
|
@@ -387,21 +395,26 @@
|
|
|
387
395
|
},
|
|
388
396
|
};
|
|
389
397
|
};
|
|
390
|
-
function
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
}
|
|
395
|
-
return id;
|
|
398
|
+
function getSeed() {
|
|
399
|
+
var _a;
|
|
400
|
+
const defaultSeed = Date.now() % 2147483647;
|
|
401
|
+
return isInBrowser() ? ((_a = window === null || window === void 0 ? void 0 : window.performance) === null || _a === void 0 ? void 0 : _a.now()) || defaultSeed : defaultSeed;
|
|
396
402
|
}
|
|
397
|
-
function
|
|
398
|
-
const
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
403
|
+
function randomId(hashString = "") {
|
|
404
|
+
const d = Date.now();
|
|
405
|
+
return (((Math.random() * d * hash(hashString !== null && hashString !== void 0 ? hashString : "", getSeed())) % Number.MAX_SAFE_INTEGER).toString(36) +
|
|
406
|
+
((Math.random() * d * hash(hashString !== null && hashString !== void 0 ? hashString : "", getSeed())) % Number.MAX_SAFE_INTEGER).toString(36));
|
|
407
|
+
}
|
|
408
|
+
function hash(str, seed = 0) {
|
|
409
|
+
let h1 = 0xdeadbeef ^ seed, h2 = 0x41c6ce57 ^ seed;
|
|
410
|
+
for (let i = 0, ch; i < str.length; i++) {
|
|
411
|
+
ch = str.charCodeAt(i);
|
|
412
|
+
h1 = Math.imul(h1 ^ ch, 2654435761);
|
|
413
|
+
h2 = Math.imul(h2 ^ ch, 1597334677);
|
|
404
414
|
}
|
|
415
|
+
h1 = Math.imul(h1 ^ (h1 >>> 16), 2246822507) ^ Math.imul(h2 ^ (h2 >>> 13), 3266489909);
|
|
416
|
+
h2 = Math.imul(h2 ^ (h2 >>> 16), 2246822507) ^ Math.imul(h1 ^ (h1 >>> 13), 3266489909);
|
|
417
|
+
return 4294967296 * (2097151 & h2) + (h1 >>> 0);
|
|
405
418
|
}
|
|
406
419
|
|
|
407
420
|
function parse(input) {
|
|
@@ -410,7 +423,7 @@
|
|
|
410
423
|
value = decodeURIComponent(input);
|
|
411
424
|
}
|
|
412
425
|
try {
|
|
413
|
-
value = JSON.parse(
|
|
426
|
+
value = JSON.parse(value);
|
|
414
427
|
if (value === "true")
|
|
415
428
|
return true;
|
|
416
429
|
if (value === "false")
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jitsu/js",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.1-alpha.120",
|
|
4
4
|
"description": "",
|
|
5
5
|
"author": "Jitsu Dev Team <dev@jitsu.com>",
|
|
6
6
|
"main": "dist/jitsu.cjs.js",
|
|
@@ -32,11 +32,11 @@
|
|
|
32
32
|
"node-forge": "^1.3.1",
|
|
33
33
|
"raw-loader": "^4.0.2",
|
|
34
34
|
"rollup": "^3.2.5",
|
|
35
|
-
"ts-jest": "^29.0.3"
|
|
36
|
-
"@jitsu/types": "0.0.0-alpha.98"
|
|
35
|
+
"ts-jest": "^29.0.3"
|
|
37
36
|
},
|
|
38
37
|
"dependencies": {
|
|
39
|
-
"analytics": "^0.8.1"
|
|
38
|
+
"analytics": "^0.8.1",
|
|
39
|
+
"@jitsu/analytics-types": "0.0.1-alpha.120"
|
|
40
40
|
},
|
|
41
41
|
"scripts": {
|
|
42
42
|
"clean": "rm -rf ./dist",
|
package/src/analytics-plugin.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/* global analytics */
|
|
2
2
|
|
|
3
3
|
import { JitsuOptions, PersistentStorage, RuntimeFacade } from "./jitsu";
|
|
4
|
-
import { AnalyticsClientEvent } from "@jitsu/types/analytics";
|
|
4
|
+
import { AnalyticsClientEvent } from "@jitsu/analytics-types/analytics";
|
|
5
5
|
import parse from "./index";
|
|
6
6
|
import { AnalyticsPlugin } from "analytics";
|
|
7
7
|
|
|
@@ -120,6 +120,11 @@ const cookieStorage: StorageFactory = (cookieDomain, key2cookie) => {
|
|
|
120
120
|
getItem(key: string) {
|
|
121
121
|
const cookieName = key2cookie[key] || key;
|
|
122
122
|
const result = getCookie(cookieName);
|
|
123
|
+
if (typeof result === "undefined" && key === "__user_id") {
|
|
124
|
+
//backward compatibility with old jitsu cookie. get user id if from traits
|
|
125
|
+
const traits = parse(getCookie("__eventn_id_usr")) || {};
|
|
126
|
+
return traits.internal_id || traits.user_id || traits.id || traits.userId;
|
|
127
|
+
}
|
|
123
128
|
return parse(result);
|
|
124
129
|
},
|
|
125
130
|
removeItem(key: string) {
|
|
@@ -158,7 +163,7 @@ export function windowRuntime(opts: JitsuOptions): RuntimeFacade {
|
|
|
158
163
|
height: window.screen.height,
|
|
159
164
|
innerWidth: window.innerWidth,
|
|
160
165
|
innerHeight: window.innerHeight,
|
|
161
|
-
density: window.devicePixelRatio,
|
|
166
|
+
density: Math.floor(window.devicePixelRatio),
|
|
162
167
|
};
|
|
163
168
|
},
|
|
164
169
|
userAgent(): string {
|
|
@@ -232,9 +237,12 @@ function deepMerge(target: any, source: any) {
|
|
|
232
237
|
}, target);
|
|
233
238
|
}
|
|
234
239
|
|
|
240
|
+
function isInBrowser() {
|
|
241
|
+
return typeof document !== "undefined";
|
|
242
|
+
}
|
|
243
|
+
|
|
235
244
|
function adjustPayload(payload: any, config: JitsuOptions, storage: PersistentStorage): AnalyticsClientEvent {
|
|
236
|
-
const runtime: RuntimeFacade =
|
|
237
|
-
config.runtime || (typeof window === "undefined" ? emptyRuntime(config) : windowRuntime(config));
|
|
245
|
+
const runtime: RuntimeFacade = config.runtime || (isInBrowser() ? windowRuntime(config) : emptyRuntime(config));
|
|
238
246
|
const url = runtime.pageUrl();
|
|
239
247
|
const parsedUrl = safeCall(() => new URL(url), undefined);
|
|
240
248
|
const query = parsedUrl ? parseQuery(parsedUrl.search) : {};
|
|
@@ -267,7 +275,7 @@ function adjustPayload(payload: any, config: JitsuOptions, storage: PersistentSt
|
|
|
267
275
|
...payload,
|
|
268
276
|
timestamp: new Date().toISOString(),
|
|
269
277
|
sentAt: new Date().toISOString(),
|
|
270
|
-
messageId: randomId(),
|
|
278
|
+
messageId: randomId(properties.path || (parsedUrl && parsedUrl.pathname)),
|
|
271
279
|
context: deepMerge(context, customContext),
|
|
272
280
|
};
|
|
273
281
|
delete withContext.meta;
|
|
@@ -368,22 +376,33 @@ const jitsuAnalyticsPlugin = (pluginConfig: JitsuOptions = {}): AnalyticsPlugin
|
|
|
368
376
|
},
|
|
369
377
|
};
|
|
370
378
|
};
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
}
|
|
376
|
-
return id;
|
|
379
|
+
|
|
380
|
+
function getSeed() {
|
|
381
|
+
const defaultSeed = Date.now() % 2147483647;
|
|
382
|
+
return isInBrowser() ? window?.performance?.now() || defaultSeed : defaultSeed;
|
|
377
383
|
}
|
|
378
384
|
|
|
379
|
-
function
|
|
380
|
-
const
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
385
|
+
export function randomId(hashString: string | undefined = ""): string {
|
|
386
|
+
const d = Date.now();
|
|
387
|
+
return (
|
|
388
|
+
((Math.random() * d * hash(hashString ?? "", getSeed())) % Number.MAX_SAFE_INTEGER).toString(36) +
|
|
389
|
+
((Math.random() * d * hash(hashString ?? "", getSeed())) % Number.MAX_SAFE_INTEGER).toString(36)
|
|
390
|
+
);
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
function hash(str: string, seed: number = 0): number {
|
|
394
|
+
let h1 = 0xdeadbeef ^ seed,
|
|
395
|
+
h2 = 0x41c6ce57 ^ seed;
|
|
396
|
+
for (let i = 0, ch; i < str.length; i++) {
|
|
397
|
+
ch = str.charCodeAt(i);
|
|
398
|
+
h1 = Math.imul(h1 ^ ch, 2654435761);
|
|
399
|
+
h2 = Math.imul(h2 ^ ch, 1597334677);
|
|
386
400
|
}
|
|
401
|
+
|
|
402
|
+
h1 = Math.imul(h1 ^ (h1 >>> 16), 2246822507) ^ Math.imul(h2 ^ (h2 >>> 13), 3266489909);
|
|
403
|
+
h2 = Math.imul(h2 ^ (h2 >>> 16), 2246822507) ^ Math.imul(h1 ^ (h1 >>> 13), 3266489909);
|
|
404
|
+
|
|
405
|
+
return 4294967296 * (2097151 & h2) + (h1 >>> 0);
|
|
387
406
|
}
|
|
388
407
|
|
|
389
408
|
export default jitsuAnalyticsPlugin;
|
package/src/index.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { AnalyticsInterface } from "@jitsu/types/analytics.d";
|
|
1
|
+
import { AnalyticsInterface } from "@jitsu/analytics-types/analytics.d";
|
|
2
2
|
import Analytics from "analytics";
|
|
3
3
|
import { JitsuOptions, PersistentStorage } from "./jitsu";
|
|
4
4
|
import jitsuAnalyticsPlugin, { emptyRuntime, windowRuntime } from "./analytics-plugin";
|
|
@@ -9,7 +9,7 @@ export default function parse(input) {
|
|
|
9
9
|
value = decodeURIComponent(input);
|
|
10
10
|
}
|
|
11
11
|
try {
|
|
12
|
-
value = JSON.parse(
|
|
12
|
+
value = JSON.parse(value);
|
|
13
13
|
if (value === "true") return true;
|
|
14
14
|
if (value === "false") return false;
|
|
15
15
|
if (typeof value === "object") return value;
|
package/src/jitsu.d.ts
CHANGED