@eventra_dev/eventra-sdk 2.0.14 → 2.1.0
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/README.md +69 -17
- package/dist/index.cjs +176 -18
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +46 -3
- package/dist/index.d.ts +46 -3
- package/dist/index.mjs +176 -18
- package/dist/index.mjs.map +1 -1
- package/package.json +5 -2
package/dist/index.d.cts
CHANGED
|
@@ -1,8 +1,16 @@
|
|
|
1
1
|
interface TrackOptions {
|
|
2
|
-
/**
|
|
3
|
-
|
|
2
|
+
/**
|
|
3
|
+
* A string; a number is written out as text. Cut to 120 characters. Takes priority over the user of
|
|
4
|
+
* `withUser()` and `identify()`; `null` sends the event without a user even when one is identified, and an
|
|
5
|
+
* empty or blank string counts as not given.
|
|
6
|
+
*/
|
|
7
|
+
userId?: string | number | null;
|
|
4
8
|
properties?: Record<string, unknown>;
|
|
5
9
|
}
|
|
10
|
+
/** A tracker bound to one user, returned by `withUser()`. Shares the queue and delivery of its `Eventra`. */
|
|
11
|
+
interface EventraScope {
|
|
12
|
+
track(name: string, options?: TrackOptions): void;
|
|
13
|
+
}
|
|
6
14
|
interface TrackerOptions {
|
|
7
15
|
apiKey: string;
|
|
8
16
|
/** Defaults to production ingest URL when omitted */
|
|
@@ -57,6 +65,12 @@ interface TrackerOptions {
|
|
|
57
65
|
status: number;
|
|
58
66
|
events: TrackEvent[];
|
|
59
67
|
}) => void;
|
|
68
|
+
/**
|
|
69
|
+
* Warn once when none of the first 50 events carries a user id (default `true`): in a browser bundle not
|
|
70
|
+
* built for production, on a server only with `NODE_ENV=development`, never in test runs. Set `false` for an
|
|
71
|
+
* app that has no signed-in users.
|
|
72
|
+
*/
|
|
73
|
+
identifyHint?: boolean;
|
|
60
74
|
/** Multi-tab mode (browser only) */
|
|
61
75
|
multiTabMode?: "independent" | "leader";
|
|
62
76
|
}
|
|
@@ -115,8 +129,36 @@ declare class Eventra {
|
|
|
115
129
|
private sdkInfo;
|
|
116
130
|
private options;
|
|
117
131
|
private exitHandlers;
|
|
132
|
+
private userId?;
|
|
133
|
+
private warnedServerIdentify;
|
|
134
|
+
private warnedInvalidIdentify;
|
|
135
|
+
private warnedInvalidUser;
|
|
136
|
+
private singleUser;
|
|
137
|
+
private userKey;
|
|
138
|
+
private eventsWithoutUser;
|
|
139
|
+
private identifyHintDone;
|
|
118
140
|
constructor(options: TrackerOptions);
|
|
119
141
|
track(name: string, options?: TrackOptions): void;
|
|
142
|
+
/**
|
|
143
|
+
* Sets the user of every following event that does not name its own, in a browser or a React Native app. In
|
|
144
|
+
* the browser the user is kept in localStorage (unless `persistQueue: false`) and shared with the other open
|
|
145
|
+
* tabs. Anywhere else (a server, an edge or serverless function, Deno, Cloudflare Workers) one instance may
|
|
146
|
+
* serve many users at once, so it is ignored with one warning: use `withUser()` there.
|
|
147
|
+
*/
|
|
148
|
+
identify(userId: string | number): void;
|
|
149
|
+
/** Forgets the user set by `identify()` (on sign-out), in every open tab and in localStorage. */
|
|
150
|
+
reset(): void;
|
|
151
|
+
/** The user set by `identify()`, if any. */
|
|
152
|
+
getUserId(): string | undefined;
|
|
153
|
+
/**
|
|
154
|
+
* A tracker whose events carry `userId`, for one request on a server (works in every runtime). It shares
|
|
155
|
+
* this instance's queue, so making one per request is cheap. `undefined`, `null` or an invalid id give events
|
|
156
|
+
* without a user (never the user of `identify()`), so `withUser(req.user?.id)` works for signed-out requests.
|
|
157
|
+
*/
|
|
158
|
+
withUser(userId: string | number | null | undefined): EventraScope;
|
|
159
|
+
private setUser;
|
|
160
|
+
private loadStoredUser;
|
|
161
|
+
private trackSafely;
|
|
120
162
|
private trackEvent;
|
|
121
163
|
flush(): Promise<void>;
|
|
122
164
|
private flushNow;
|
|
@@ -131,6 +173,7 @@ declare class Eventra {
|
|
|
131
173
|
private flushEverything;
|
|
132
174
|
destroy(): void;
|
|
133
175
|
private buildBatch;
|
|
176
|
+
private checkIdentifyHint;
|
|
134
177
|
private dropPoisonEvent;
|
|
135
178
|
private removeDelivered;
|
|
136
179
|
private send;
|
|
@@ -160,4 +203,4 @@ declare class Eventra {
|
|
|
160
203
|
private setupProcessExit;
|
|
161
204
|
}
|
|
162
205
|
|
|
163
|
-
export { Eventra, type SdkInfo, type TrackEvent, type TrackOptions, type TrackerOptions };
|
|
206
|
+
export { Eventra, type EventraScope, type SdkInfo, type TrackEvent, type TrackOptions, type TrackerOptions };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,8 +1,16 @@
|
|
|
1
1
|
interface TrackOptions {
|
|
2
|
-
/**
|
|
3
|
-
|
|
2
|
+
/**
|
|
3
|
+
* A string; a number is written out as text. Cut to 120 characters. Takes priority over the user of
|
|
4
|
+
* `withUser()` and `identify()`; `null` sends the event without a user even when one is identified, and an
|
|
5
|
+
* empty or blank string counts as not given.
|
|
6
|
+
*/
|
|
7
|
+
userId?: string | number | null;
|
|
4
8
|
properties?: Record<string, unknown>;
|
|
5
9
|
}
|
|
10
|
+
/** A tracker bound to one user, returned by `withUser()`. Shares the queue and delivery of its `Eventra`. */
|
|
11
|
+
interface EventraScope {
|
|
12
|
+
track(name: string, options?: TrackOptions): void;
|
|
13
|
+
}
|
|
6
14
|
interface TrackerOptions {
|
|
7
15
|
apiKey: string;
|
|
8
16
|
/** Defaults to production ingest URL when omitted */
|
|
@@ -57,6 +65,12 @@ interface TrackerOptions {
|
|
|
57
65
|
status: number;
|
|
58
66
|
events: TrackEvent[];
|
|
59
67
|
}) => void;
|
|
68
|
+
/**
|
|
69
|
+
* Warn once when none of the first 50 events carries a user id (default `true`): in a browser bundle not
|
|
70
|
+
* built for production, on a server only with `NODE_ENV=development`, never in test runs. Set `false` for an
|
|
71
|
+
* app that has no signed-in users.
|
|
72
|
+
*/
|
|
73
|
+
identifyHint?: boolean;
|
|
60
74
|
/** Multi-tab mode (browser only) */
|
|
61
75
|
multiTabMode?: "independent" | "leader";
|
|
62
76
|
}
|
|
@@ -115,8 +129,36 @@ declare class Eventra {
|
|
|
115
129
|
private sdkInfo;
|
|
116
130
|
private options;
|
|
117
131
|
private exitHandlers;
|
|
132
|
+
private userId?;
|
|
133
|
+
private warnedServerIdentify;
|
|
134
|
+
private warnedInvalidIdentify;
|
|
135
|
+
private warnedInvalidUser;
|
|
136
|
+
private singleUser;
|
|
137
|
+
private userKey;
|
|
138
|
+
private eventsWithoutUser;
|
|
139
|
+
private identifyHintDone;
|
|
118
140
|
constructor(options: TrackerOptions);
|
|
119
141
|
track(name: string, options?: TrackOptions): void;
|
|
142
|
+
/**
|
|
143
|
+
* Sets the user of every following event that does not name its own, in a browser or a React Native app. In
|
|
144
|
+
* the browser the user is kept in localStorage (unless `persistQueue: false`) and shared with the other open
|
|
145
|
+
* tabs. Anywhere else (a server, an edge or serverless function, Deno, Cloudflare Workers) one instance may
|
|
146
|
+
* serve many users at once, so it is ignored with one warning: use `withUser()` there.
|
|
147
|
+
*/
|
|
148
|
+
identify(userId: string | number): void;
|
|
149
|
+
/** Forgets the user set by `identify()` (on sign-out), in every open tab and in localStorage. */
|
|
150
|
+
reset(): void;
|
|
151
|
+
/** The user set by `identify()`, if any. */
|
|
152
|
+
getUserId(): string | undefined;
|
|
153
|
+
/**
|
|
154
|
+
* A tracker whose events carry `userId`, for one request on a server (works in every runtime). It shares
|
|
155
|
+
* this instance's queue, so making one per request is cheap. `undefined`, `null` or an invalid id give events
|
|
156
|
+
* without a user (never the user of `identify()`), so `withUser(req.user?.id)` works for signed-out requests.
|
|
157
|
+
*/
|
|
158
|
+
withUser(userId: string | number | null | undefined): EventraScope;
|
|
159
|
+
private setUser;
|
|
160
|
+
private loadStoredUser;
|
|
161
|
+
private trackSafely;
|
|
120
162
|
private trackEvent;
|
|
121
163
|
flush(): Promise<void>;
|
|
122
164
|
private flushNow;
|
|
@@ -131,6 +173,7 @@ declare class Eventra {
|
|
|
131
173
|
private flushEverything;
|
|
132
174
|
destroy(): void;
|
|
133
175
|
private buildBatch;
|
|
176
|
+
private checkIdentifyHint;
|
|
134
177
|
private dropPoisonEvent;
|
|
135
178
|
private removeDelivered;
|
|
136
179
|
private send;
|
|
@@ -160,4 +203,4 @@ declare class Eventra {
|
|
|
160
203
|
private setupProcessExit;
|
|
161
204
|
}
|
|
162
205
|
|
|
163
|
-
export { Eventra, type SdkInfo, type TrackEvent, type TrackOptions, type TrackerOptions };
|
|
206
|
+
export { Eventra, type EventraScope, type SdkInfo, type TrackEvent, type TrackOptions, type TrackerOptions };
|
package/dist/index.mjs
CHANGED
|
@@ -167,11 +167,12 @@ var Diagnostics = class {
|
|
|
167
167
|
// src/client.ts
|
|
168
168
|
var SDK_NAME = "@eventra_dev/eventra-sdk";
|
|
169
169
|
var DEFAULT_ENDPOINT = "https://api.eventra.dev/api/v1/ingest/batch";
|
|
170
|
-
var SDK_VERSION = "2.0
|
|
170
|
+
var SDK_VERSION = "2.1.0";
|
|
171
171
|
var LEADER_KEY = "__eventra_leader__";
|
|
172
172
|
var LEADER_TTL_MS = 4e3;
|
|
173
173
|
var QUEUE_KEY = "__eventra_q__";
|
|
174
174
|
var DIAGNOSTICS_KEY = "__eventra_d__";
|
|
175
|
+
var USER_KEY_PREFIX = "__eventra_u__";
|
|
175
176
|
var CHANNEL_NAME = "eventra-sdk";
|
|
176
177
|
var MAX_EVENT_NAME = 64;
|
|
177
178
|
var MAX_USER_ID = 120;
|
|
@@ -192,6 +193,7 @@ var KEEPALIVE_MAX_BYTES = 6e4;
|
|
|
192
193
|
var DELIVERED_KEYS_MAX = 1e4;
|
|
193
194
|
var LIVE_TAB_GRACE_MS = 15e3;
|
|
194
195
|
var LIVE_TAB_KEYS_MAX = 1e4;
|
|
196
|
+
var IDENTIFY_HINT_AFTER = 50;
|
|
195
197
|
var activeInstances = 0;
|
|
196
198
|
function detectRuntime() {
|
|
197
199
|
const g = globalThis;
|
|
@@ -258,10 +260,11 @@ function isUrl(value) {
|
|
|
258
260
|
return false;
|
|
259
261
|
}
|
|
260
262
|
}
|
|
263
|
+
function warn(message) {
|
|
264
|
+
if (typeof console !== "undefined") console.warn(message);
|
|
265
|
+
}
|
|
261
266
|
function warnOption(name, value, fallback) {
|
|
262
|
-
|
|
263
|
-
console.warn(`Eventra: option "${name}" is invalid (${String(value)}), using ${String(fallback)}`);
|
|
264
|
-
}
|
|
267
|
+
warn(`Eventra: option "${name}" is invalid (${String(value)}), using ${String(fallback)}`);
|
|
265
268
|
}
|
|
266
269
|
function numberOption(name, value, fallback, min, max = Number.MAX_SAFE_INTEGER) {
|
|
267
270
|
if (value === void 0) return fallback;
|
|
@@ -315,6 +318,39 @@ function normalizeUserId(value, max) {
|
|
|
315
318
|
if (typeof value !== "string") return void 0;
|
|
316
319
|
return cut(value, max);
|
|
317
320
|
}
|
|
321
|
+
function identifiedUserId(value) {
|
|
322
|
+
const id = normalizeUserId(value, MAX_USER_ID);
|
|
323
|
+
if (id === void 0) return void 0;
|
|
324
|
+
const trimmed = id.trim();
|
|
325
|
+
return trimmed && trimmed !== "undefined" && trimmed !== "null" ? id : void 0;
|
|
326
|
+
}
|
|
327
|
+
function keyHash(value) {
|
|
328
|
+
let hash = 2166136261;
|
|
329
|
+
for (let i = 0; i < value.length; i++) {
|
|
330
|
+
hash ^= value.charCodeAt(i);
|
|
331
|
+
hash = Math.imul(hash, 16777619);
|
|
332
|
+
}
|
|
333
|
+
return (hash >>> 0).toString(16);
|
|
334
|
+
}
|
|
335
|
+
function isReactNative() {
|
|
336
|
+
try {
|
|
337
|
+
return globalThis.navigator?.product === "ReactNative";
|
|
338
|
+
} catch {
|
|
339
|
+
return false;
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
function nodeEnv() {
|
|
343
|
+
try {
|
|
344
|
+
return process.env.NODE_ENV;
|
|
345
|
+
} catch {
|
|
346
|
+
return null;
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
function isDevelopment(server) {
|
|
350
|
+
const env = nodeEnv();
|
|
351
|
+
if (env === null || env === "test") return false;
|
|
352
|
+
return server ? env === "development" : env !== "production";
|
|
353
|
+
}
|
|
318
354
|
function stripUnsafeForName(value) {
|
|
319
355
|
let out = "";
|
|
320
356
|
for (let i = 0; i < value.length; i++) {
|
|
@@ -359,7 +395,9 @@ function utf8ByteLength(value) {
|
|
|
359
395
|
function isStoredEvent(e) {
|
|
360
396
|
if (e === null || typeof e !== "object") return false;
|
|
361
397
|
const ev = e;
|
|
362
|
-
return typeof ev.idempotencyKey === "string" && ev.idempotencyKey.length > 0 && ev.idempotencyKey.length <= 100 &&
|
|
398
|
+
return typeof ev.idempotencyKey === "string" && ev.idempotencyKey.length > 0 && ev.idempotencyKey.length <= 100 && // Stryker disable next-line ConditionalExpression: equivalent, a name that is not a string has no length and fails the next check
|
|
399
|
+
typeof ev.name === "string" && ev.name.length > 0 && ev.name.length <= MAX_EVENT_NAME && typeof ev.timestamp === "string" && // Stryker disable next-line ConditionalExpression: equivalent, a user id that is not a string has no length to pass the check
|
|
400
|
+
(ev.userId === void 0 || typeof ev.userId === "string" && ev.userId.length <= MAX_USER_ID) && (ev.properties === void 0 || ev.properties !== null && typeof ev.properties === "object" && !Array.isArray(ev.properties));
|
|
363
401
|
}
|
|
364
402
|
function mergeQueues(...queues) {
|
|
365
403
|
const seen = /* @__PURE__ */ new Set();
|
|
@@ -393,8 +431,12 @@ function persistedQueueJson(queue) {
|
|
|
393
431
|
}
|
|
394
432
|
var MAX_PROPERTIES_NODES = 5e4;
|
|
395
433
|
function validateProperties(properties) {
|
|
396
|
-
if (
|
|
397
|
-
|
|
434
|
+
if (
|
|
435
|
+
// Stryker disable next-line all: equivalent, track() passes `properties ?? {}`, and any other non-object fails the toString check below
|
|
436
|
+
properties === null || // Stryker disable next-line all: equivalent, see the line above
|
|
437
|
+
typeof properties !== "object" || Array.isArray(properties) || // a Date, a Map, a Set, a RegExp: objects, but not a bag of named values
|
|
438
|
+
Object.prototype.toString.call(properties) !== "[object Object]"
|
|
439
|
+
) {
|
|
398
440
|
throw new Error("Eventra: properties must be an object");
|
|
399
441
|
}
|
|
400
442
|
const topKeys = Object.keys(properties);
|
|
@@ -502,11 +544,6 @@ var Storage = class {
|
|
|
502
544
|
} catch {
|
|
503
545
|
}
|
|
504
546
|
}
|
|
505
|
-
// Not currently called anywhere in the client — kept for API symmetry with
|
|
506
|
-
// get()/set() on this internal class. No real call site exists to exercise
|
|
507
|
-
// it, and Storage isn't part of the public surface, so a direct call here
|
|
508
|
-
// would only be a reflection-hack test, not real usage.
|
|
509
|
-
/* v8 ignore start */
|
|
510
547
|
remove(key) {
|
|
511
548
|
if (!this.enabled) return;
|
|
512
549
|
try {
|
|
@@ -514,7 +551,6 @@ var Storage = class {
|
|
|
514
551
|
} catch {
|
|
515
552
|
}
|
|
516
553
|
}
|
|
517
|
-
/* v8 ignore stop */
|
|
518
554
|
};
|
|
519
555
|
function storageAvailable() {
|
|
520
556
|
try {
|
|
@@ -675,6 +711,16 @@ var Eventra = class {
|
|
|
675
711
|
// the page is being left (`pagehide`)
|
|
676
712
|
this.leaving = false;
|
|
677
713
|
this.exitHandlers = [];
|
|
714
|
+
this.warnedServerIdentify = false;
|
|
715
|
+
this.warnedInvalidIdentify = false;
|
|
716
|
+
this.warnedInvalidUser = false;
|
|
717
|
+
// one user for the whole instance is safe only where the instance serves one person: a browser tab, a React
|
|
718
|
+
// Native app. Everything else (Node, edge, Lambda, Deno, Cloudflare Workers) may serve many requests at once.
|
|
719
|
+
this.singleUser = false;
|
|
720
|
+
this.userKey = USER_KEY_PREFIX;
|
|
721
|
+
// identifyHint: events without a user id so far, and whether the check is over
|
|
722
|
+
this.eventsWithoutUser = 0;
|
|
723
|
+
this.identifyHintDone = false;
|
|
678
724
|
if (!options || typeof options !== "object" || typeof options.apiKey !== "string" || !options.apiKey) {
|
|
679
725
|
throw new Error('Eventra: apiKey required - new Eventra({ apiKey: "..." })');
|
|
680
726
|
}
|
|
@@ -707,6 +753,10 @@ var Eventra = class {
|
|
|
707
753
|
if (options.diagnostics !== void 0 && typeof options.diagnostics !== "boolean") {
|
|
708
754
|
warnOption("diagnostics", options.diagnostics, "true");
|
|
709
755
|
}
|
|
756
|
+
if (options.identifyHint !== void 0 && typeof options.identifyHint !== "boolean") {
|
|
757
|
+
warnOption("identifyHint", options.identifyHint, "true");
|
|
758
|
+
}
|
|
759
|
+
this.identifyHintDone = options.identifyHint === false;
|
|
710
760
|
for (const name of ["maxBatchSize", "maxQueueSize"]) {
|
|
711
761
|
const value = options[name];
|
|
712
762
|
if (typeof value === "number" && Number.isFinite(value) && value >= 1 && !Number.isInteger(value)) {
|
|
@@ -714,6 +764,8 @@ var Eventra = class {
|
|
|
714
764
|
}
|
|
715
765
|
}
|
|
716
766
|
this.runtime = detectRuntime();
|
|
767
|
+
this.singleUser = this.runtime === "browser" || isReactNative();
|
|
768
|
+
this.userKey = `${USER_KEY_PREFIX}${keyHash(options.apiKey)}`;
|
|
717
769
|
warnIfInsecureEndpoint(this.endpoint);
|
|
718
770
|
this.fetch = options.fetchImpl ?? globalThis.fetch?.bind(globalThis);
|
|
719
771
|
if (!this.fetch) {
|
|
@@ -745,7 +797,10 @@ var Eventra = class {
|
|
|
745
797
|
};
|
|
746
798
|
if (this.runtime === "browser") {
|
|
747
799
|
if (this.persistQueue) {
|
|
800
|
+
this.userId = this.loadStoredUser();
|
|
748
801
|
this.loadAndMergeQueue();
|
|
802
|
+
} else {
|
|
803
|
+
this.storage.remove(this.userKey);
|
|
749
804
|
}
|
|
750
805
|
this.setupQueueSync();
|
|
751
806
|
}
|
|
@@ -783,14 +838,86 @@ var Eventra = class {
|
|
|
783
838
|
}
|
|
784
839
|
}
|
|
785
840
|
track(name, options) {
|
|
841
|
+
this.trackSafely(name, options, void 0);
|
|
842
|
+
}
|
|
843
|
+
/**
|
|
844
|
+
* Sets the user of every following event that does not name its own, in a browser or a React Native app. In
|
|
845
|
+
* the browser the user is kept in localStorage (unless `persistQueue: false`) and shared with the other open
|
|
846
|
+
* tabs. Anywhere else (a server, an edge or serverless function, Deno, Cloudflare Workers) one instance may
|
|
847
|
+
* serve many users at once, so it is ignored with one warning: use `withUser()` there.
|
|
848
|
+
*/
|
|
849
|
+
identify(userId) {
|
|
850
|
+
if (this.destroyed) return;
|
|
851
|
+
if (!this.singleUser) {
|
|
852
|
+
if (!this.warnedServerIdentify) {
|
|
853
|
+
this.warnedServerIdentify = true;
|
|
854
|
+
warn(
|
|
855
|
+
"Eventra: identify() is ignored outside a browser, where one instance may serve many users - use withUser(userId).track(...) for each request (in Next.js and other SSR apps, call identify() in client code, e.g. useEffect)"
|
|
856
|
+
);
|
|
857
|
+
}
|
|
858
|
+
return;
|
|
859
|
+
}
|
|
860
|
+
const id = identifiedUserId(userId);
|
|
861
|
+
if (id === void 0) {
|
|
862
|
+
if (!this.warnedInvalidIdentify) {
|
|
863
|
+
this.warnedInvalidIdentify = true;
|
|
864
|
+
warn("Eventra: identify() needs a user id (a non-empty string or a number) - to sign a user out, call reset()");
|
|
865
|
+
}
|
|
866
|
+
return;
|
|
867
|
+
}
|
|
868
|
+
this.identifyHintDone = true;
|
|
869
|
+
if (id !== this.userId) this.setUser(id);
|
|
870
|
+
}
|
|
871
|
+
/** Forgets the user set by `identify()` (on sign-out), in every open tab and in localStorage. */
|
|
872
|
+
reset() {
|
|
873
|
+
if (this.destroyed || !this.singleUser) return;
|
|
874
|
+
this.setUser(void 0);
|
|
875
|
+
}
|
|
876
|
+
/** The user set by `identify()`, if any. */
|
|
877
|
+
getUserId() {
|
|
878
|
+
return this.userId;
|
|
879
|
+
}
|
|
880
|
+
/**
|
|
881
|
+
* A tracker whose events carry `userId`, for one request on a server (works in every runtime). It shares
|
|
882
|
+
* this instance's queue, so making one per request is cheap. `undefined`, `null` or an invalid id give events
|
|
883
|
+
* without a user (never the user of `identify()`), so `withUser(req.user?.id)` works for signed-out requests.
|
|
884
|
+
*/
|
|
885
|
+
withUser(userId) {
|
|
886
|
+
let id;
|
|
887
|
+
if (userId !== void 0 && userId !== null) {
|
|
888
|
+
id = identifiedUserId(userId);
|
|
889
|
+
if (id === void 0 && !this.warnedInvalidUser) {
|
|
890
|
+
this.warnedInvalidUser = true;
|
|
891
|
+
warn("Eventra: withUser() got an invalid user id (it needs a non-empty string or a number), its events are sent without one");
|
|
892
|
+
}
|
|
893
|
+
}
|
|
894
|
+
if (id !== void 0) this.identifyHintDone = true;
|
|
895
|
+
const scopeUser = id ?? null;
|
|
896
|
+
return {
|
|
897
|
+
track: (name, options) => this.trackSafely(name, options, scopeUser)
|
|
898
|
+
};
|
|
899
|
+
}
|
|
900
|
+
setUser(id) {
|
|
901
|
+
this.userId = id;
|
|
902
|
+
if (this.runtime !== "browser") return;
|
|
903
|
+
if (id === void 0) this.storage.remove(this.userKey);
|
|
904
|
+
else if (this.persistQueue) this.storage.set(this.userKey, id);
|
|
905
|
+
this.broadcast({ type: "user", key: this.userKey, userId: id ?? null });
|
|
906
|
+
}
|
|
907
|
+
loadStoredUser() {
|
|
908
|
+
const saved = this.storage.get(this.userKey);
|
|
909
|
+
return typeof saved === "string" ? identifiedUserId(saved) : void 0;
|
|
910
|
+
}
|
|
911
|
+
// scopeUserId: undefined outside a scope, null for a scope without a user
|
|
912
|
+
trackSafely(name, options, scopeUserId) {
|
|
786
913
|
try {
|
|
787
|
-
this.trackEvent(name, options);
|
|
914
|
+
this.trackEvent(name, options, scopeUserId);
|
|
788
915
|
} catch {
|
|
789
916
|
this.recordDiagnostic("invalid_event");
|
|
790
917
|
callSafely(this.options.onEventsDropped, 1);
|
|
791
918
|
}
|
|
792
919
|
}
|
|
793
|
-
trackEvent(name, options) {
|
|
920
|
+
trackEvent(name, options, scopeUserId) {
|
|
794
921
|
if (this.destroyed || this.shuttingDown) return;
|
|
795
922
|
const eventName = normalizeEventName(name);
|
|
796
923
|
if (eventName === null) {
|
|
@@ -811,14 +938,17 @@ var Eventra = class {
|
|
|
811
938
|
callSafely(this.options.onEventsDropped, 1);
|
|
812
939
|
return;
|
|
813
940
|
}
|
|
941
|
+
const given = options?.userId;
|
|
942
|
+
const ownUserId = typeof given === "string" && !given.trim() ? void 0 : given;
|
|
814
943
|
const event = {
|
|
815
944
|
idempotencyKey: uuidV4(),
|
|
816
945
|
name: eventName,
|
|
817
|
-
userId: normalizeUserId(
|
|
946
|
+
userId: ownUserId !== void 0 ? normalizeUserId(ownUserId, MAX_USER_ID) : scopeUserId !== void 0 ? scopeUserId ?? void 0 : this.userId,
|
|
818
947
|
properties,
|
|
819
948
|
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
820
949
|
};
|
|
821
950
|
this.queue.push(event);
|
|
951
|
+
if (!this.identifyHintDone) this.checkIdentifyHint(identifiedUserId(event.userId) !== void 0);
|
|
822
952
|
if (this.runtime === "browser") {
|
|
823
953
|
this.schedulePersist();
|
|
824
954
|
this.broadcast({ type: "queue-add", events: [event] });
|
|
@@ -1013,6 +1143,20 @@ var Eventra = class {
|
|
|
1013
1143
|
diagnostics: withReport && report ? report : void 0
|
|
1014
1144
|
};
|
|
1015
1145
|
}
|
|
1146
|
+
// one warning during development when an app sends events without ever saying whose they are
|
|
1147
|
+
checkIdentifyHint(hasUser) {
|
|
1148
|
+
if (hasUser) {
|
|
1149
|
+
this.identifyHintDone = true;
|
|
1150
|
+
return;
|
|
1151
|
+
}
|
|
1152
|
+
if (++this.eventsWithoutUser < IDENTIFY_HINT_AFTER) return;
|
|
1153
|
+
this.identifyHintDone = true;
|
|
1154
|
+
if (!isDevelopment(!this.singleUser)) return;
|
|
1155
|
+
const how = this.singleUser ? "call tracker.identify(userId) after sign-in" : "track through tracker.withUser(userId) in each request";
|
|
1156
|
+
warn(
|
|
1157
|
+
`Eventra: none of the first ${IDENTIFY_HINT_AFTER} events has a userId, so unique users cannot be counted - ${how} (identifyHint: false turns this off)`
|
|
1158
|
+
);
|
|
1159
|
+
}
|
|
1016
1160
|
dropPoisonEvent(event) {
|
|
1017
1161
|
this.recordDiagnostic("too_large");
|
|
1018
1162
|
this.removeDelivered([event]);
|
|
@@ -1048,8 +1192,13 @@ var Eventra = class {
|
|
|
1048
1192
|
this.dropPoisonEvents(events);
|
|
1049
1193
|
return false;
|
|
1050
1194
|
}
|
|
1051
|
-
const useKeepalive =
|
|
1052
|
-
|
|
1195
|
+
const useKeepalive = (
|
|
1196
|
+
// Stryker disable next-line all: equivalent, document exists exactly in the browser runtime
|
|
1197
|
+
this.runtime === "browser" && // Stryker disable next-line all: equivalent, see the line above
|
|
1198
|
+
typeof document !== "undefined" && (document.visibilityState === "hidden" || this.leaving) && // browsers refuse a keepalive body above 64 KiB (the whole request fails), whatever maxPayloadBytes allows
|
|
1199
|
+
// Stryker disable next-line EqualityOperator: a body of exactly the keepalive limit; not worth a 60,000-byte fixture
|
|
1200
|
+
payloadBytes <= Math.min(this.maxPayloadBytes, KEEPALIVE_MAX_BYTES)
|
|
1201
|
+
);
|
|
1053
1202
|
const maxAttempts = this.circuitHalfOpen ? 1 : Math.max(1, this.retries);
|
|
1054
1203
|
let strippedDiagnostics = false;
|
|
1055
1204
|
for (let attempt = 0; attempt < maxAttempts; attempt++) {
|
|
@@ -1077,7 +1226,9 @@ var Eventra = class {
|
|
|
1077
1226
|
discardBody(res);
|
|
1078
1227
|
if (res.status === 429 || res.status >= 500) {
|
|
1079
1228
|
throw new RetryableDeliveryError(
|
|
1229
|
+
// Stryker disable next-line StringLiteral: the message is never read
|
|
1080
1230
|
"retryable delivery error",
|
|
1231
|
+
// Stryker disable next-line OptionalChaining: equivalent with a real Response, which always has headers.get
|
|
1081
1232
|
parseRetryAfterMs(res.headers?.get?.("retry-after")),
|
|
1082
1233
|
res.status === 429 ? "rate_limited" : "server_error"
|
|
1083
1234
|
);
|
|
@@ -1253,6 +1404,12 @@ var Eventra = class {
|
|
|
1253
1404
|
if (this.channel) {
|
|
1254
1405
|
this.channel.onmessage = (e) => {
|
|
1255
1406
|
const data = e.data;
|
|
1407
|
+
if (data?.type === "user") {
|
|
1408
|
+
if (data.key !== this.userKey) return;
|
|
1409
|
+
if (data.userId === null) this.userId = void 0;
|
|
1410
|
+
else if (typeof data.userId === "string") this.userId = identifiedUserId(data.userId) ?? this.userId;
|
|
1411
|
+
return;
|
|
1412
|
+
}
|
|
1256
1413
|
if (data?.type === "delivered" && Array.isArray(data.keys)) {
|
|
1257
1414
|
const keys = new Set(data.keys);
|
|
1258
1415
|
this.rememberDelivered(keys);
|
|
@@ -1271,6 +1428,7 @@ var Eventra = class {
|
|
|
1271
1428
|
}
|
|
1272
1429
|
if (this.persistQueue && typeof window !== "undefined") {
|
|
1273
1430
|
this.offStorageQueue = listen(window, "storage", (e) => {
|
|
1431
|
+
if (e.key === this.userKey || e.key === null) this.userId = this.loadStoredUser();
|
|
1274
1432
|
if (e.key !== QUEUE_KEY) return;
|
|
1275
1433
|
this.loadAndMergeQueue();
|
|
1276
1434
|
});
|