@journium/js 1.1.0 → 1.2.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 +49 -5
- package/dist/JourniumAnalytics.d.ts +0 -1
- package/dist/JourniumAnalytics.d.ts.map +1 -1
- package/dist/JourniumClient.d.ts +9 -1
- package/dist/JourniumClient.d.ts.map +1 -1
- package/dist/PageviewTracker.d.ts +3 -2
- package/dist/PageviewTracker.d.ts.map +1 -1
- package/dist/cdn.d.ts +40 -0
- package/dist/cdn.d.ts.map +1 -0
- package/dist/index.cjs +135 -135
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +13 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.mjs +136 -135
- package/dist/index.mjs.map +1 -1
- package/dist/index.umd.js +136 -136
- package/dist/index.umd.js.map +1 -1
- package/dist/journium.js +1892 -0
- package/dist/journium.js.map +1 -0
- package/dist/journium.min.js +2 -0
- package/dist/journium.min.js.map +1 -0
- package/package.json +10 -5
package/dist/journium.js
ADDED
|
@@ -0,0 +1,1892 @@
|
|
|
1
|
+
/* Journium Analytics SDK - Browser CDN Build */
|
|
2
|
+
(function (exports) {
|
|
3
|
+
'use strict';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* uuidv7: A JavaScript implementation of UUID version 7
|
|
7
|
+
*
|
|
8
|
+
* Copyright 2021-2024 LiosK
|
|
9
|
+
*
|
|
10
|
+
* @license Apache-2.0
|
|
11
|
+
* @packageDocumentation
|
|
12
|
+
*/
|
|
13
|
+
const DIGITS = "0123456789abcdef";
|
|
14
|
+
/** Represents a UUID as a 16-byte byte array. */
|
|
15
|
+
class UUID {
|
|
16
|
+
/** @param bytes - The 16-byte byte array representation. */
|
|
17
|
+
constructor(bytes) {
|
|
18
|
+
this.bytes = bytes;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Creates an object from the internal representation, a 16-byte byte array
|
|
22
|
+
* containing the binary UUID representation in the big-endian byte order.
|
|
23
|
+
*
|
|
24
|
+
* This method does NOT shallow-copy the argument, and thus the created object
|
|
25
|
+
* holds the reference to the underlying buffer.
|
|
26
|
+
*
|
|
27
|
+
* @throws TypeError if the length of the argument is not 16.
|
|
28
|
+
*/
|
|
29
|
+
static ofInner(bytes) {
|
|
30
|
+
if (bytes.length !== 16) {
|
|
31
|
+
throw new TypeError("not 128-bit length");
|
|
32
|
+
}
|
|
33
|
+
else {
|
|
34
|
+
return new UUID(bytes);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Builds a byte array from UUIDv7 field values.
|
|
39
|
+
*
|
|
40
|
+
* @param unixTsMs - A 48-bit `unix_ts_ms` field value.
|
|
41
|
+
* @param randA - A 12-bit `rand_a` field value.
|
|
42
|
+
* @param randBHi - The higher 30 bits of 62-bit `rand_b` field value.
|
|
43
|
+
* @param randBLo - The lower 32 bits of 62-bit `rand_b` field value.
|
|
44
|
+
* @throws RangeError if any field value is out of the specified range.
|
|
45
|
+
*/
|
|
46
|
+
static fromFieldsV7(unixTsMs, randA, randBHi, randBLo) {
|
|
47
|
+
if (!Number.isInteger(unixTsMs) ||
|
|
48
|
+
!Number.isInteger(randA) ||
|
|
49
|
+
!Number.isInteger(randBHi) ||
|
|
50
|
+
!Number.isInteger(randBLo) ||
|
|
51
|
+
unixTsMs < 0 ||
|
|
52
|
+
randA < 0 ||
|
|
53
|
+
randBHi < 0 ||
|
|
54
|
+
randBLo < 0 ||
|
|
55
|
+
unixTsMs > 281474976710655 ||
|
|
56
|
+
randA > 0xfff ||
|
|
57
|
+
randBHi > 1073741823 ||
|
|
58
|
+
randBLo > 4294967295) {
|
|
59
|
+
throw new RangeError("invalid field value");
|
|
60
|
+
}
|
|
61
|
+
const bytes = new Uint8Array(16);
|
|
62
|
+
bytes[0] = unixTsMs / 2 ** 40;
|
|
63
|
+
bytes[1] = unixTsMs / 2 ** 32;
|
|
64
|
+
bytes[2] = unixTsMs / 2 ** 24;
|
|
65
|
+
bytes[3] = unixTsMs / 2 ** 16;
|
|
66
|
+
bytes[4] = unixTsMs / 2 ** 8;
|
|
67
|
+
bytes[5] = unixTsMs;
|
|
68
|
+
bytes[6] = 0x70 | (randA >>> 8);
|
|
69
|
+
bytes[7] = randA;
|
|
70
|
+
bytes[8] = 0x80 | (randBHi >>> 24);
|
|
71
|
+
bytes[9] = randBHi >>> 16;
|
|
72
|
+
bytes[10] = randBHi >>> 8;
|
|
73
|
+
bytes[11] = randBHi;
|
|
74
|
+
bytes[12] = randBLo >>> 24;
|
|
75
|
+
bytes[13] = randBLo >>> 16;
|
|
76
|
+
bytes[14] = randBLo >>> 8;
|
|
77
|
+
bytes[15] = randBLo;
|
|
78
|
+
return new UUID(bytes);
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Builds a byte array from a string representation.
|
|
82
|
+
*
|
|
83
|
+
* This method accepts the following formats:
|
|
84
|
+
*
|
|
85
|
+
* - 32-digit hexadecimal format without hyphens: `0189dcd553117d408db09496a2eef37b`
|
|
86
|
+
* - 8-4-4-4-12 hyphenated format: `0189dcd5-5311-7d40-8db0-9496a2eef37b`
|
|
87
|
+
* - Hyphenated format with surrounding braces: `{0189dcd5-5311-7d40-8db0-9496a2eef37b}`
|
|
88
|
+
* - RFC 9562 URN format: `urn:uuid:0189dcd5-5311-7d40-8db0-9496a2eef37b`
|
|
89
|
+
*
|
|
90
|
+
* Leading and trailing whitespaces represents an error.
|
|
91
|
+
*
|
|
92
|
+
* @throws SyntaxError if the argument could not parse as a valid UUID string.
|
|
93
|
+
*/
|
|
94
|
+
static parse(uuid) {
|
|
95
|
+
var _a, _b, _c, _d;
|
|
96
|
+
let hex = undefined;
|
|
97
|
+
switch (uuid.length) {
|
|
98
|
+
case 32:
|
|
99
|
+
hex = (_a = /^[0-9a-f]{32}$/i.exec(uuid)) === null || _a === void 0 ? void 0 : _a[0];
|
|
100
|
+
break;
|
|
101
|
+
case 36:
|
|
102
|
+
hex =
|
|
103
|
+
(_b = /^([0-9a-f]{8})-([0-9a-f]{4})-([0-9a-f]{4})-([0-9a-f]{4})-([0-9a-f]{12})$/i
|
|
104
|
+
.exec(uuid)) === null || _b === void 0 ? void 0 : _b.slice(1, 6).join("");
|
|
105
|
+
break;
|
|
106
|
+
case 38:
|
|
107
|
+
hex =
|
|
108
|
+
(_c = /^\{([0-9a-f]{8})-([0-9a-f]{4})-([0-9a-f]{4})-([0-9a-f]{4})-([0-9a-f]{12})\}$/i
|
|
109
|
+
.exec(uuid)) === null || _c === void 0 ? void 0 : _c.slice(1, 6).join("");
|
|
110
|
+
break;
|
|
111
|
+
case 45:
|
|
112
|
+
hex =
|
|
113
|
+
(_d = /^urn:uuid:([0-9a-f]{8})-([0-9a-f]{4})-([0-9a-f]{4})-([0-9a-f]{4})-([0-9a-f]{12})$/i
|
|
114
|
+
.exec(uuid)) === null || _d === void 0 ? void 0 : _d.slice(1, 6).join("");
|
|
115
|
+
break;
|
|
116
|
+
}
|
|
117
|
+
if (hex) {
|
|
118
|
+
const inner = new Uint8Array(16);
|
|
119
|
+
for (let i = 0; i < 16; i += 4) {
|
|
120
|
+
const n = parseInt(hex.substring(2 * i, 2 * i + 8), 16);
|
|
121
|
+
inner[i + 0] = n >>> 24;
|
|
122
|
+
inner[i + 1] = n >>> 16;
|
|
123
|
+
inner[i + 2] = n >>> 8;
|
|
124
|
+
inner[i + 3] = n;
|
|
125
|
+
}
|
|
126
|
+
return new UUID(inner);
|
|
127
|
+
}
|
|
128
|
+
else {
|
|
129
|
+
throw new SyntaxError("could not parse UUID string");
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* @returns The 8-4-4-4-12 canonical hexadecimal string representation
|
|
134
|
+
* (`0189dcd5-5311-7d40-8db0-9496a2eef37b`).
|
|
135
|
+
*/
|
|
136
|
+
toString() {
|
|
137
|
+
let text = "";
|
|
138
|
+
for (let i = 0; i < this.bytes.length; i++) {
|
|
139
|
+
text += DIGITS.charAt(this.bytes[i] >>> 4);
|
|
140
|
+
text += DIGITS.charAt(this.bytes[i] & 0xf);
|
|
141
|
+
if (i === 3 || i === 5 || i === 7 || i === 9) {
|
|
142
|
+
text += "-";
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
return text;
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* @returns The 32-digit hexadecimal representation without hyphens
|
|
149
|
+
* (`0189dcd553117d408db09496a2eef37b`).
|
|
150
|
+
*/
|
|
151
|
+
toHex() {
|
|
152
|
+
let text = "";
|
|
153
|
+
for (let i = 0; i < this.bytes.length; i++) {
|
|
154
|
+
text += DIGITS.charAt(this.bytes[i] >>> 4);
|
|
155
|
+
text += DIGITS.charAt(this.bytes[i] & 0xf);
|
|
156
|
+
}
|
|
157
|
+
return text;
|
|
158
|
+
}
|
|
159
|
+
/** @returns The 8-4-4-4-12 canonical hexadecimal string representation. */
|
|
160
|
+
toJSON() {
|
|
161
|
+
return this.toString();
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* Reports the variant field value of the UUID or, if appropriate, "NIL" or
|
|
165
|
+
* "MAX".
|
|
166
|
+
*
|
|
167
|
+
* For convenience, this method reports "NIL" or "MAX" if `this` represents
|
|
168
|
+
* the Nil or Max UUID, although the Nil and Max UUIDs are technically
|
|
169
|
+
* subsumed under the variants `0b0` and `0b111`, respectively.
|
|
170
|
+
*/
|
|
171
|
+
getVariant() {
|
|
172
|
+
const n = this.bytes[8] >>> 4;
|
|
173
|
+
if (n < 0) {
|
|
174
|
+
throw new Error("unreachable");
|
|
175
|
+
}
|
|
176
|
+
else if (n <= 0b0111) {
|
|
177
|
+
return this.bytes.every((e) => e === 0) ? "NIL" : "VAR_0";
|
|
178
|
+
}
|
|
179
|
+
else if (n <= 0b1011) {
|
|
180
|
+
return "VAR_10";
|
|
181
|
+
}
|
|
182
|
+
else if (n <= 0b1101) {
|
|
183
|
+
return "VAR_110";
|
|
184
|
+
}
|
|
185
|
+
else if (n <= 0b1111) {
|
|
186
|
+
return this.bytes.every((e) => e === 0xff) ? "MAX" : "VAR_RESERVED";
|
|
187
|
+
}
|
|
188
|
+
else {
|
|
189
|
+
throw new Error("unreachable");
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* Returns the version field value of the UUID or `undefined` if the UUID does
|
|
194
|
+
* not have the variant field value of `0b10`.
|
|
195
|
+
*/
|
|
196
|
+
getVersion() {
|
|
197
|
+
return this.getVariant() === "VAR_10" ? this.bytes[6] >>> 4 : undefined;
|
|
198
|
+
}
|
|
199
|
+
/** Creates an object from `this`. */
|
|
200
|
+
clone() {
|
|
201
|
+
return new UUID(this.bytes.slice(0));
|
|
202
|
+
}
|
|
203
|
+
/** Returns true if `this` is equivalent to `other`. */
|
|
204
|
+
equals(other) {
|
|
205
|
+
return this.compareTo(other) === 0;
|
|
206
|
+
}
|
|
207
|
+
/**
|
|
208
|
+
* Returns a negative integer, zero, or positive integer if `this` is less
|
|
209
|
+
* than, equal to, or greater than `other`, respectively.
|
|
210
|
+
*/
|
|
211
|
+
compareTo(other) {
|
|
212
|
+
for (let i = 0; i < 16; i++) {
|
|
213
|
+
const diff = this.bytes[i] - other.bytes[i];
|
|
214
|
+
if (diff !== 0) {
|
|
215
|
+
return Math.sign(diff);
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
return 0;
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
/**
|
|
222
|
+
* Encapsulates the monotonic counter state.
|
|
223
|
+
*
|
|
224
|
+
* This class provides APIs to utilize a separate counter state from that of the
|
|
225
|
+
* global generator used by {@link uuidv7} and {@link uuidv7obj}. In addition to
|
|
226
|
+
* the default {@link generate} method, this class has {@link generateOrAbort}
|
|
227
|
+
* that is useful to absolutely guarantee the monotonically increasing order of
|
|
228
|
+
* generated UUIDs. See their respective documentation for details.
|
|
229
|
+
*/
|
|
230
|
+
class V7Generator {
|
|
231
|
+
/**
|
|
232
|
+
* Creates a generator object with the default random number generator, or
|
|
233
|
+
* with the specified one if passed as an argument. The specified random
|
|
234
|
+
* number generator should be cryptographically strong and securely seeded.
|
|
235
|
+
*/
|
|
236
|
+
constructor(randomNumberGenerator) {
|
|
237
|
+
this.timestamp = 0;
|
|
238
|
+
this.counter = 0;
|
|
239
|
+
this.random = randomNumberGenerator !== null && randomNumberGenerator !== void 0 ? randomNumberGenerator : getDefaultRandom();
|
|
240
|
+
}
|
|
241
|
+
/**
|
|
242
|
+
* Generates a new UUIDv7 object from the current timestamp, or resets the
|
|
243
|
+
* generator upon significant timestamp rollback.
|
|
244
|
+
*
|
|
245
|
+
* This method returns a monotonically increasing UUID by reusing the previous
|
|
246
|
+
* timestamp even if the up-to-date timestamp is smaller than the immediately
|
|
247
|
+
* preceding UUID's. However, when such a clock rollback is considered
|
|
248
|
+
* significant (i.e., by more than ten seconds), this method resets the
|
|
249
|
+
* generator and returns a new UUID based on the given timestamp, breaking the
|
|
250
|
+
* increasing order of UUIDs.
|
|
251
|
+
*
|
|
252
|
+
* See {@link generateOrAbort} for the other mode of generation and
|
|
253
|
+
* {@link generateOrResetCore} for the low-level primitive.
|
|
254
|
+
*/
|
|
255
|
+
generate() {
|
|
256
|
+
return this.generateOrResetCore(Date.now(), 10000);
|
|
257
|
+
}
|
|
258
|
+
/**
|
|
259
|
+
* Generates a new UUIDv7 object from the current timestamp, or returns
|
|
260
|
+
* `undefined` upon significant timestamp rollback.
|
|
261
|
+
*
|
|
262
|
+
* This method returns a monotonically increasing UUID by reusing the previous
|
|
263
|
+
* timestamp even if the up-to-date timestamp is smaller than the immediately
|
|
264
|
+
* preceding UUID's. However, when such a clock rollback is considered
|
|
265
|
+
* significant (i.e., by more than ten seconds), this method aborts and
|
|
266
|
+
* returns `undefined` immediately.
|
|
267
|
+
*
|
|
268
|
+
* See {@link generate} for the other mode of generation and
|
|
269
|
+
* {@link generateOrAbortCore} for the low-level primitive.
|
|
270
|
+
*/
|
|
271
|
+
generateOrAbort() {
|
|
272
|
+
return this.generateOrAbortCore(Date.now(), 10000);
|
|
273
|
+
}
|
|
274
|
+
/**
|
|
275
|
+
* Generates a new UUIDv7 object from the `unixTsMs` passed, or resets the
|
|
276
|
+
* generator upon significant timestamp rollback.
|
|
277
|
+
*
|
|
278
|
+
* This method is equivalent to {@link generate} except that it takes a custom
|
|
279
|
+
* timestamp and clock rollback allowance.
|
|
280
|
+
*
|
|
281
|
+
* @param rollbackAllowance - The amount of `unixTsMs` rollback that is
|
|
282
|
+
* considered significant. A suggested value is `10_000` (milliseconds).
|
|
283
|
+
* @throws RangeError if `unixTsMs` is not a 48-bit positive integer.
|
|
284
|
+
*/
|
|
285
|
+
generateOrResetCore(unixTsMs, rollbackAllowance) {
|
|
286
|
+
let value = this.generateOrAbortCore(unixTsMs, rollbackAllowance);
|
|
287
|
+
if (value === undefined) {
|
|
288
|
+
// reset state and resume
|
|
289
|
+
this.timestamp = 0;
|
|
290
|
+
value = this.generateOrAbortCore(unixTsMs, rollbackAllowance);
|
|
291
|
+
}
|
|
292
|
+
return value;
|
|
293
|
+
}
|
|
294
|
+
/**
|
|
295
|
+
* Generates a new UUIDv7 object from the `unixTsMs` passed, or returns
|
|
296
|
+
* `undefined` upon significant timestamp rollback.
|
|
297
|
+
*
|
|
298
|
+
* This method is equivalent to {@link generateOrAbort} except that it takes a
|
|
299
|
+
* custom timestamp and clock rollback allowance.
|
|
300
|
+
*
|
|
301
|
+
* @param rollbackAllowance - The amount of `unixTsMs` rollback that is
|
|
302
|
+
* considered significant. A suggested value is `10_000` (milliseconds).
|
|
303
|
+
* @throws RangeError if `unixTsMs` is not a 48-bit positive integer.
|
|
304
|
+
*/
|
|
305
|
+
generateOrAbortCore(unixTsMs, rollbackAllowance) {
|
|
306
|
+
const MAX_COUNTER = 4398046511103;
|
|
307
|
+
if (!Number.isInteger(unixTsMs) ||
|
|
308
|
+
unixTsMs < 1 ||
|
|
309
|
+
unixTsMs > 281474976710655) {
|
|
310
|
+
throw new RangeError("`unixTsMs` must be a 48-bit positive integer");
|
|
311
|
+
}
|
|
312
|
+
else if (rollbackAllowance < 0 || rollbackAllowance > 281474976710655) {
|
|
313
|
+
throw new RangeError("`rollbackAllowance` out of reasonable range");
|
|
314
|
+
}
|
|
315
|
+
if (unixTsMs > this.timestamp) {
|
|
316
|
+
this.timestamp = unixTsMs;
|
|
317
|
+
this.resetCounter();
|
|
318
|
+
}
|
|
319
|
+
else if (unixTsMs + rollbackAllowance >= this.timestamp) {
|
|
320
|
+
// go on with previous timestamp if new one is not much smaller
|
|
321
|
+
this.counter++;
|
|
322
|
+
if (this.counter > MAX_COUNTER) {
|
|
323
|
+
// increment timestamp at counter overflow
|
|
324
|
+
this.timestamp++;
|
|
325
|
+
this.resetCounter();
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
else {
|
|
329
|
+
// abort if clock went backwards to unbearable extent
|
|
330
|
+
return undefined;
|
|
331
|
+
}
|
|
332
|
+
return UUID.fromFieldsV7(this.timestamp, Math.trunc(this.counter / 2 ** 30), this.counter & (2 ** 30 - 1), this.random.nextUint32());
|
|
333
|
+
}
|
|
334
|
+
/** Initializes the counter at a 42-bit random integer. */
|
|
335
|
+
resetCounter() {
|
|
336
|
+
this.counter =
|
|
337
|
+
this.random.nextUint32() * 0x400 + (this.random.nextUint32() & 0x3ff);
|
|
338
|
+
}
|
|
339
|
+
/**
|
|
340
|
+
* Generates a new UUIDv4 object utilizing the random number generator inside.
|
|
341
|
+
*
|
|
342
|
+
* @internal
|
|
343
|
+
*/
|
|
344
|
+
generateV4() {
|
|
345
|
+
const bytes = new Uint8Array(Uint32Array.of(this.random.nextUint32(), this.random.nextUint32(), this.random.nextUint32(), this.random.nextUint32()).buffer);
|
|
346
|
+
bytes[6] = 0x40 | (bytes[6] >>> 4);
|
|
347
|
+
bytes[8] = 0x80 | (bytes[8] >>> 2);
|
|
348
|
+
return UUID.ofInner(bytes);
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
/** Returns the default random number generator available in the environment. */
|
|
352
|
+
const getDefaultRandom = () => {
|
|
353
|
+
// detect Web Crypto API
|
|
354
|
+
if (typeof crypto !== "undefined" &&
|
|
355
|
+
typeof crypto.getRandomValues !== "undefined") {
|
|
356
|
+
return new BufferedCryptoRandom();
|
|
357
|
+
}
|
|
358
|
+
else {
|
|
359
|
+
// fall back on Math.random() unless the flag is set to true
|
|
360
|
+
if (typeof UUIDV7_DENY_WEAK_RNG !== "undefined" && UUIDV7_DENY_WEAK_RNG) {
|
|
361
|
+
throw new Error("no cryptographically strong RNG available");
|
|
362
|
+
}
|
|
363
|
+
return {
|
|
364
|
+
nextUint32: () => Math.trunc(Math.random() * 65536) * 65536 +
|
|
365
|
+
Math.trunc(Math.random() * 65536),
|
|
366
|
+
};
|
|
367
|
+
}
|
|
368
|
+
};
|
|
369
|
+
/**
|
|
370
|
+
* Wraps `crypto.getRandomValues()` to enable buffering; this uses a small
|
|
371
|
+
* buffer by default to avoid both unbearable throughput decline in some
|
|
372
|
+
* environments and the waste of time and space for unused values.
|
|
373
|
+
*/
|
|
374
|
+
class BufferedCryptoRandom {
|
|
375
|
+
constructor() {
|
|
376
|
+
this.buffer = new Uint32Array(8);
|
|
377
|
+
this.cursor = 0xffff;
|
|
378
|
+
}
|
|
379
|
+
nextUint32() {
|
|
380
|
+
if (this.cursor >= this.buffer.length) {
|
|
381
|
+
crypto.getRandomValues(this.buffer);
|
|
382
|
+
this.cursor = 0;
|
|
383
|
+
}
|
|
384
|
+
return this.buffer[this.cursor++];
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
let defaultGenerator;
|
|
388
|
+
/**
|
|
389
|
+
* Generates a UUIDv7 string.
|
|
390
|
+
*
|
|
391
|
+
* @returns The 8-4-4-4-12 canonical hexadecimal string representation
|
|
392
|
+
* ("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx").
|
|
393
|
+
*/
|
|
394
|
+
const uuidv7 = () => uuidv7obj().toString();
|
|
395
|
+
/** Generates a UUIDv7 object. */
|
|
396
|
+
const uuidv7obj = () => (defaultGenerator || (defaultGenerator = new V7Generator())).generate();
|
|
397
|
+
const generateUuidv7 = () => {
|
|
398
|
+
return uuidv7();
|
|
399
|
+
};
|
|
400
|
+
const getCurrentTimestamp = () => {
|
|
401
|
+
return new Date().toISOString();
|
|
402
|
+
};
|
|
403
|
+
const getCurrentUrl = () => {
|
|
404
|
+
if (typeof window !== 'undefined') {
|
|
405
|
+
return window.location.href;
|
|
406
|
+
}
|
|
407
|
+
return '';
|
|
408
|
+
};
|
|
409
|
+
const getPageTitle = () => {
|
|
410
|
+
if (typeof document !== 'undefined') {
|
|
411
|
+
return document.title;
|
|
412
|
+
}
|
|
413
|
+
return '';
|
|
414
|
+
};
|
|
415
|
+
const getReferrer = () => {
|
|
416
|
+
if (typeof document !== 'undefined') {
|
|
417
|
+
return document.referrer;
|
|
418
|
+
}
|
|
419
|
+
return '';
|
|
420
|
+
};
|
|
421
|
+
const isBrowser = () => {
|
|
422
|
+
return typeof window !== 'undefined';
|
|
423
|
+
};
|
|
424
|
+
const isNode = () => {
|
|
425
|
+
var _a;
|
|
426
|
+
return typeof process !== 'undefined' && !!((_a = process.versions) === null || _a === void 0 ? void 0 : _a.node);
|
|
427
|
+
};
|
|
428
|
+
const fetchRemoteOptions = async (apiHost, publishableKey, fetchFn) => {
|
|
429
|
+
const endpoint = '/v1/configs';
|
|
430
|
+
const url = `${apiHost}${endpoint}?ingestion_key=${encodeURIComponent(publishableKey)}`;
|
|
431
|
+
try {
|
|
432
|
+
let fetch = fetchFn;
|
|
433
|
+
if (!fetch) {
|
|
434
|
+
if (isNode()) {
|
|
435
|
+
// For Node.js environments, expect fetch to be passed in
|
|
436
|
+
throw new Error('Fetch function must be provided in Node.js environment');
|
|
437
|
+
}
|
|
438
|
+
else {
|
|
439
|
+
// Use native fetch in browser
|
|
440
|
+
fetch = window.fetch;
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
const response = await fetch(url, {
|
|
444
|
+
method: 'GET',
|
|
445
|
+
headers: {
|
|
446
|
+
'Content-Type': 'application/json',
|
|
447
|
+
},
|
|
448
|
+
});
|
|
449
|
+
// if (!response.ok) {
|
|
450
|
+
// throw new Error(`Options fetch failed: ${response.status} ${response.statusText}`);
|
|
451
|
+
// }
|
|
452
|
+
const data = await response.json();
|
|
453
|
+
return data;
|
|
454
|
+
}
|
|
455
|
+
catch (error) {
|
|
456
|
+
console.warn('Failed to fetch remote options:', error);
|
|
457
|
+
return null;
|
|
458
|
+
}
|
|
459
|
+
};
|
|
460
|
+
const mergeOptions = (localOptions, remoteOptions) => {
|
|
461
|
+
if (!remoteOptions && !localOptions) {
|
|
462
|
+
return {};
|
|
463
|
+
}
|
|
464
|
+
if (!remoteOptions) {
|
|
465
|
+
return localOptions;
|
|
466
|
+
}
|
|
467
|
+
if (!localOptions) {
|
|
468
|
+
return remoteOptions;
|
|
469
|
+
}
|
|
470
|
+
// Deep merge local options into remote options
|
|
471
|
+
// Local options takes precedence over remote options
|
|
472
|
+
const merged = { ...remoteOptions };
|
|
473
|
+
// Handle primitive values
|
|
474
|
+
Object.keys(localOptions).forEach(key => {
|
|
475
|
+
const localValue = localOptions[key];
|
|
476
|
+
if (localValue !== undefined && localValue !== null) {
|
|
477
|
+
if (typeof localValue === 'object' && !Array.isArray(localValue)) {
|
|
478
|
+
// Deep merge objects - local options overrides remote
|
|
479
|
+
merged[key] = {
|
|
480
|
+
...(merged[key] || {}),
|
|
481
|
+
...localValue
|
|
482
|
+
};
|
|
483
|
+
}
|
|
484
|
+
else {
|
|
485
|
+
// Override primitive values and arrays with local options
|
|
486
|
+
merged[key] = localValue;
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
});
|
|
490
|
+
return merged;
|
|
491
|
+
};
|
|
492
|
+
|
|
493
|
+
const DEFAULT_SESSION_TIMEOUT = 30 * 60 * 1000; // 30 minutes in ms
|
|
494
|
+
class BrowserIdentityManager {
|
|
495
|
+
constructor(sessionTimeout, publishableKey) {
|
|
496
|
+
this.identity = null;
|
|
497
|
+
this.sessionTimeout = DEFAULT_SESSION_TIMEOUT;
|
|
498
|
+
if (sessionTimeout) {
|
|
499
|
+
this.sessionTimeout = sessionTimeout;
|
|
500
|
+
}
|
|
501
|
+
// Generate storage key with publishableKey pattern: jrnm_<publishableKey>_journium
|
|
502
|
+
this.storageKey = publishableKey ? `jrnm_${publishableKey}_journium` : '__journium_identity';
|
|
503
|
+
this.loadOrCreateIdentity();
|
|
504
|
+
}
|
|
505
|
+
loadOrCreateIdentity() {
|
|
506
|
+
if (!this.isBrowser())
|
|
507
|
+
return;
|
|
508
|
+
try {
|
|
509
|
+
const stored = localStorage.getItem(this.storageKey);
|
|
510
|
+
if (stored) {
|
|
511
|
+
const parsedIdentity = JSON.parse(stored);
|
|
512
|
+
// Check if session is expired
|
|
513
|
+
const now = Date.now();
|
|
514
|
+
const sessionAge = now - parsedIdentity.session_timestamp;
|
|
515
|
+
if (sessionAge > this.sessionTimeout) {
|
|
516
|
+
// Session expired, create new session but keep device and distinct IDs
|
|
517
|
+
this.identity = {
|
|
518
|
+
distinct_id: parsedIdentity.distinct_id,
|
|
519
|
+
$device_id: parsedIdentity.$device_id,
|
|
520
|
+
$session_id: generateUuidv7(),
|
|
521
|
+
session_timestamp: now,
|
|
522
|
+
$user_state: parsedIdentity.$user_state || 'anonymous',
|
|
523
|
+
};
|
|
524
|
+
}
|
|
525
|
+
else {
|
|
526
|
+
// Session still valid
|
|
527
|
+
this.identity = parsedIdentity;
|
|
528
|
+
// Ensure $user_state exists for backward compatibility
|
|
529
|
+
if (!this.identity.$user_state) {
|
|
530
|
+
this.identity.$user_state = 'anonymous';
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
else {
|
|
535
|
+
// First time, create all new IDs
|
|
536
|
+
const newId = generateUuidv7();
|
|
537
|
+
this.identity = {
|
|
538
|
+
distinct_id: newId,
|
|
539
|
+
$device_id: newId,
|
|
540
|
+
$session_id: newId,
|
|
541
|
+
session_timestamp: Date.now(),
|
|
542
|
+
$user_state: 'anonymous',
|
|
543
|
+
};
|
|
544
|
+
}
|
|
545
|
+
// Save to localStorage
|
|
546
|
+
this.saveIdentity();
|
|
547
|
+
}
|
|
548
|
+
catch (error) {
|
|
549
|
+
console.warn('Journium: Failed to load/create identity:', error);
|
|
550
|
+
// Fallback: create temporary identity without localStorage
|
|
551
|
+
const newId = generateUuidv7();
|
|
552
|
+
this.identity = {
|
|
553
|
+
distinct_id: newId,
|
|
554
|
+
$device_id: newId,
|
|
555
|
+
$session_id: newId,
|
|
556
|
+
session_timestamp: Date.now(),
|
|
557
|
+
$user_state: 'anonymous',
|
|
558
|
+
};
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
saveIdentity() {
|
|
562
|
+
if (!this.isBrowser() || !this.identity)
|
|
563
|
+
return;
|
|
564
|
+
try {
|
|
565
|
+
localStorage.setItem(this.storageKey, JSON.stringify(this.identity));
|
|
566
|
+
}
|
|
567
|
+
catch (error) {
|
|
568
|
+
console.warn('Journium: Failed to save identity to localStorage:', error);
|
|
569
|
+
}
|
|
570
|
+
}
|
|
571
|
+
isBrowser() {
|
|
572
|
+
return typeof window !== 'undefined' && typeof localStorage !== 'undefined';
|
|
573
|
+
}
|
|
574
|
+
getIdentity() {
|
|
575
|
+
return this.identity;
|
|
576
|
+
}
|
|
577
|
+
updateSessionTimeout(timeoutMs) {
|
|
578
|
+
this.sessionTimeout = timeoutMs;
|
|
579
|
+
}
|
|
580
|
+
refreshSession() {
|
|
581
|
+
if (!this.identity)
|
|
582
|
+
return;
|
|
583
|
+
this.identity = {
|
|
584
|
+
...this.identity,
|
|
585
|
+
$session_id: generateUuidv7(),
|
|
586
|
+
session_timestamp: Date.now(),
|
|
587
|
+
};
|
|
588
|
+
this.saveIdentity();
|
|
589
|
+
}
|
|
590
|
+
identify(distinctId, _attributes = {}) {
|
|
591
|
+
if (!this.identity)
|
|
592
|
+
return { previousDistinctId: null };
|
|
593
|
+
const previousDistinctId = this.identity.distinct_id;
|
|
594
|
+
// Update the distinct ID and mark user as identified
|
|
595
|
+
this.identity = {
|
|
596
|
+
...this.identity,
|
|
597
|
+
distinct_id: distinctId,
|
|
598
|
+
$user_state: 'identified',
|
|
599
|
+
};
|
|
600
|
+
this.saveIdentity();
|
|
601
|
+
return { previousDistinctId };
|
|
602
|
+
}
|
|
603
|
+
reset() {
|
|
604
|
+
if (!this.identity)
|
|
605
|
+
return;
|
|
606
|
+
// Generate new distinct ID but keep device ID
|
|
607
|
+
this.identity = {
|
|
608
|
+
...this.identity,
|
|
609
|
+
distinct_id: generateUuidv7(),
|
|
610
|
+
$user_state: 'anonymous',
|
|
611
|
+
};
|
|
612
|
+
this.saveIdentity();
|
|
613
|
+
}
|
|
614
|
+
getUserAgentInfo() {
|
|
615
|
+
if (!this.isBrowser()) {
|
|
616
|
+
return {
|
|
617
|
+
$raw_user_agent: '',
|
|
618
|
+
$browser: 'Unknown',
|
|
619
|
+
$os: 'Unknown',
|
|
620
|
+
$device_type: 'Unknown',
|
|
621
|
+
};
|
|
622
|
+
}
|
|
623
|
+
const userAgent = navigator.userAgent;
|
|
624
|
+
return {
|
|
625
|
+
$raw_user_agent: userAgent,
|
|
626
|
+
$browser: this.parseBrowser(userAgent),
|
|
627
|
+
$os: this.parseOS(userAgent),
|
|
628
|
+
$device_type: this.parseDeviceType(userAgent),
|
|
629
|
+
};
|
|
630
|
+
}
|
|
631
|
+
parseBrowser(userAgent) {
|
|
632
|
+
if (userAgent.includes('Chrome') && !userAgent.includes('Edg'))
|
|
633
|
+
return 'Chrome';
|
|
634
|
+
if (userAgent.includes('Firefox'))
|
|
635
|
+
return 'Firefox';
|
|
636
|
+
if (userAgent.includes('Safari') && !userAgent.includes('Chrome'))
|
|
637
|
+
return 'Safari';
|
|
638
|
+
if (userAgent.includes('Edg'))
|
|
639
|
+
return 'Edge';
|
|
640
|
+
if (userAgent.includes('Opera') || userAgent.includes('OPR'))
|
|
641
|
+
return 'Opera';
|
|
642
|
+
return 'Unknown';
|
|
643
|
+
}
|
|
644
|
+
parseOS(userAgent) {
|
|
645
|
+
if (userAgent.includes('Windows'))
|
|
646
|
+
return 'Windows';
|
|
647
|
+
if (userAgent.includes('Macintosh') || userAgent.includes('Mac OS'))
|
|
648
|
+
return 'Mac OS';
|
|
649
|
+
if (userAgent.includes('Linux'))
|
|
650
|
+
return 'Linux';
|
|
651
|
+
if (userAgent.includes('Android'))
|
|
652
|
+
return 'Android';
|
|
653
|
+
if (userAgent.includes('iPhone') || userAgent.includes('iPad'))
|
|
654
|
+
return 'iOS';
|
|
655
|
+
return 'Unknown';
|
|
656
|
+
}
|
|
657
|
+
parseDeviceType(userAgent) {
|
|
658
|
+
if (userAgent.includes('Mobile') || userAgent.includes('Android') || userAgent.includes('iPhone')) {
|
|
659
|
+
return 'Mobile';
|
|
660
|
+
}
|
|
661
|
+
if (userAgent.includes('iPad') || userAgent.includes('Tablet')) {
|
|
662
|
+
return 'Tablet';
|
|
663
|
+
}
|
|
664
|
+
return 'Desktop';
|
|
665
|
+
}
|
|
666
|
+
}
|
|
667
|
+
|
|
668
|
+
class Logger {
|
|
669
|
+
static setDebug(enabled) {
|
|
670
|
+
this.isDebugEnabled = enabled;
|
|
671
|
+
}
|
|
672
|
+
static isDebug() {
|
|
673
|
+
return this.isDebugEnabled;
|
|
674
|
+
}
|
|
675
|
+
static log(...args) {
|
|
676
|
+
if (this.isDebugEnabled) {
|
|
677
|
+
console.log(...args);
|
|
678
|
+
}
|
|
679
|
+
}
|
|
680
|
+
static warn(...args) {
|
|
681
|
+
if (this.isDebugEnabled) {
|
|
682
|
+
console.warn(...args);
|
|
683
|
+
}
|
|
684
|
+
}
|
|
685
|
+
static error(...args) {
|
|
686
|
+
if (this.isDebugEnabled) {
|
|
687
|
+
console.error(...args);
|
|
688
|
+
}
|
|
689
|
+
}
|
|
690
|
+
static info(...args) {
|
|
691
|
+
if (this.isDebugEnabled) {
|
|
692
|
+
console.info(...args);
|
|
693
|
+
}
|
|
694
|
+
}
|
|
695
|
+
}
|
|
696
|
+
Logger.isDebugEnabled = false;
|
|
697
|
+
|
|
698
|
+
class JourniumClient {
|
|
699
|
+
constructor(config) {
|
|
700
|
+
var _a, _b, _c, _d;
|
|
701
|
+
this.queue = [];
|
|
702
|
+
this.stagedEvents = [];
|
|
703
|
+
this.flushTimer = null;
|
|
704
|
+
this.remoteOptionsRefreshTimer = null;
|
|
705
|
+
this.isRefreshing = false;
|
|
706
|
+
this.lastRemoteOptions = null;
|
|
707
|
+
this.initializationComplete = false;
|
|
708
|
+
this.initializationFailed = false;
|
|
709
|
+
this.optionsChangeCallbacks = new Set();
|
|
710
|
+
// Validate required configuration
|
|
711
|
+
if (!config.publishableKey || config.publishableKey.trim() === '') {
|
|
712
|
+
// Reject initialization with clear error
|
|
713
|
+
const errorMsg = 'Journium: publishableKey is required but not provided or is empty. SDK cannot be initialized.';
|
|
714
|
+
Logger.setDebug(true);
|
|
715
|
+
Logger.error(errorMsg);
|
|
716
|
+
throw new Error(errorMsg);
|
|
717
|
+
}
|
|
718
|
+
// Set default apiHost if not provided
|
|
719
|
+
this.config = {
|
|
720
|
+
...config,
|
|
721
|
+
apiHost: config.apiHost || 'https://events.journium.app'
|
|
722
|
+
};
|
|
723
|
+
// Generate storage key for options caching
|
|
724
|
+
this.optionsStorageKey = `jrnm_${config.publishableKey}_options`;
|
|
725
|
+
// Initialize with minimal defaults for identity manager
|
|
726
|
+
const fallbackSessionTimeout = 30 * 60 * 1000; // 30 minutes
|
|
727
|
+
this.effectiveOptions = {}; // Will be set after remote config
|
|
728
|
+
// Initialize Logger with local debug setting or false
|
|
729
|
+
Logger.setDebug((_b = (_a = this.config.options) === null || _a === void 0 ? void 0 : _a.debug) !== null && _b !== void 0 ? _b : false);
|
|
730
|
+
// Initialize identity manager with fallback timeout
|
|
731
|
+
this.identityManager = new BrowserIdentityManager((_d = (_c = this.config.options) === null || _c === void 0 ? void 0 : _c.sessionTimeout) !== null && _d !== void 0 ? _d : fallbackSessionTimeout, this.config.publishableKey);
|
|
732
|
+
// Initialize asynchronously - wait for remote config first
|
|
733
|
+
this.initializeAsync();
|
|
734
|
+
}
|
|
735
|
+
loadCachedOptions() {
|
|
736
|
+
if (typeof window === 'undefined' || !window.localStorage) {
|
|
737
|
+
return null;
|
|
738
|
+
}
|
|
739
|
+
try {
|
|
740
|
+
const cached = window.localStorage.getItem(this.optionsStorageKey);
|
|
741
|
+
return cached ? JSON.parse(cached) : null;
|
|
742
|
+
}
|
|
743
|
+
catch (error) {
|
|
744
|
+
Logger.warn('Journium: Failed to load cached config:', error);
|
|
745
|
+
return null;
|
|
746
|
+
}
|
|
747
|
+
}
|
|
748
|
+
saveCachedOptions(options) {
|
|
749
|
+
if (typeof window === 'undefined' || !window.localStorage) {
|
|
750
|
+
return;
|
|
751
|
+
}
|
|
752
|
+
try {
|
|
753
|
+
window.localStorage.setItem(this.optionsStorageKey, JSON.stringify(options));
|
|
754
|
+
}
|
|
755
|
+
catch (error) {
|
|
756
|
+
Logger.warn('Journium: Failed to save config to cache:', error);
|
|
757
|
+
}
|
|
758
|
+
}
|
|
759
|
+
async initializeAsync() {
|
|
760
|
+
try {
|
|
761
|
+
Logger.log('Journium: Starting initialization - fetching fresh remote config...');
|
|
762
|
+
const remoteOptions = await this.fetchRemoteOptionsWithRetry();
|
|
763
|
+
if (!remoteOptions) {
|
|
764
|
+
Logger.error('Journium: Initialization failed - no remote config available');
|
|
765
|
+
this.initializationFailed = true;
|
|
766
|
+
return;
|
|
767
|
+
}
|
|
768
|
+
this.applyRemoteOptions(remoteOptions);
|
|
769
|
+
Logger.log('Journium: Effective options after init:', this.effectiveOptions);
|
|
770
|
+
this.initializationComplete = true;
|
|
771
|
+
this.initializationFailed = false;
|
|
772
|
+
this.processStagedEvents();
|
|
773
|
+
if (this.effectiveOptions.flushInterval && this.effectiveOptions.flushInterval > 0) {
|
|
774
|
+
this.startFlushTimer();
|
|
775
|
+
}
|
|
776
|
+
this.startRemoteOptionsRefreshTimer();
|
|
777
|
+
Logger.log('Journium: Initialization complete');
|
|
778
|
+
this.notifyOptionsChange();
|
|
779
|
+
}
|
|
780
|
+
catch (error) {
|
|
781
|
+
Logger.error('Journium: Initialization failed:', error);
|
|
782
|
+
this.initializationFailed = true;
|
|
783
|
+
this.initializationComplete = false;
|
|
784
|
+
}
|
|
785
|
+
}
|
|
786
|
+
async fetchRemoteOptionsWithRetry() {
|
|
787
|
+
const maxRetries = 2;
|
|
788
|
+
const timeoutMs = 15000; // 15 seconds
|
|
789
|
+
for (let attempt = 1; attempt <= maxRetries; attempt++) {
|
|
790
|
+
try {
|
|
791
|
+
Logger.log(`Journium: Fetching remote config (attempt ${attempt}/${maxRetries})...`);
|
|
792
|
+
// Create timeout promise
|
|
793
|
+
const timeoutPromise = new Promise((_, reject) => {
|
|
794
|
+
setTimeout(() => reject(new Error('Timeout')), timeoutMs);
|
|
795
|
+
});
|
|
796
|
+
// Race fetch against timeout
|
|
797
|
+
const fetchPromise = fetchRemoteOptions(this.config.apiHost, this.config.publishableKey);
|
|
798
|
+
const remoteOptionsResponse = await Promise.race([fetchPromise, timeoutPromise]);
|
|
799
|
+
if (remoteOptionsResponse && remoteOptionsResponse.status === 'success') {
|
|
800
|
+
Logger.log('Journium: Successfully fetched fresh remote config:', remoteOptionsResponse.config);
|
|
801
|
+
return remoteOptionsResponse.config || null;
|
|
802
|
+
}
|
|
803
|
+
else if (remoteOptionsResponse && remoteOptionsResponse.status === 'error' && remoteOptionsResponse.errorCode === 'J_ERR_TENANT_NOT_FOUND') {
|
|
804
|
+
Logger.error('Journium: Invalid publishableKey is being used.');
|
|
805
|
+
return null;
|
|
806
|
+
}
|
|
807
|
+
{
|
|
808
|
+
throw new Error('Remote config fetch unsuccessful');
|
|
809
|
+
}
|
|
810
|
+
}
|
|
811
|
+
catch (error) {
|
|
812
|
+
Logger.warn(`Journium: Remote config fetch attempt ${attempt} failed:`, error);
|
|
813
|
+
if (attempt === maxRetries) {
|
|
814
|
+
Logger.warn('Journium: All remote config fetch attempts failed, falling back to cached config');
|
|
815
|
+
return null;
|
|
816
|
+
}
|
|
817
|
+
// Wait 1 second before retry (except on last attempt)
|
|
818
|
+
if (attempt < maxRetries) {
|
|
819
|
+
await new Promise(resolve => setTimeout(resolve, 1000));
|
|
820
|
+
}
|
|
821
|
+
}
|
|
822
|
+
}
|
|
823
|
+
return null;
|
|
824
|
+
}
|
|
825
|
+
/**
|
|
826
|
+
* Register a callback to be notified when effective options change (e.g., when remote options are fetched)
|
|
827
|
+
*/
|
|
828
|
+
onOptionsChange(callback) {
|
|
829
|
+
this.optionsChangeCallbacks.add(callback);
|
|
830
|
+
// Return unsubscribe function
|
|
831
|
+
return () => {
|
|
832
|
+
this.optionsChangeCallbacks.delete(callback);
|
|
833
|
+
};
|
|
834
|
+
}
|
|
835
|
+
notifyOptionsChange() {
|
|
836
|
+
this.optionsChangeCallbacks.forEach(callback => {
|
|
837
|
+
try {
|
|
838
|
+
callback(this.effectiveOptions);
|
|
839
|
+
}
|
|
840
|
+
catch (error) {
|
|
841
|
+
Logger.warn('Journium: Error in options change callback:', error);
|
|
842
|
+
}
|
|
843
|
+
});
|
|
844
|
+
}
|
|
845
|
+
processStagedEvents() {
|
|
846
|
+
if (this.stagedEvents.length === 0)
|
|
847
|
+
return;
|
|
848
|
+
if (this.ingestionPaused) {
|
|
849
|
+
Logger.warn(`Journium: Ingestion is paused — discarding ${this.stagedEvents.length} staged events`);
|
|
850
|
+
this.stagedEvents = [];
|
|
851
|
+
return;
|
|
852
|
+
}
|
|
853
|
+
Logger.log(`Journium: Processing ${this.stagedEvents.length} staged events`);
|
|
854
|
+
for (const stagedEvent of this.stagedEvents) {
|
|
855
|
+
this.queue.push({
|
|
856
|
+
...stagedEvent,
|
|
857
|
+
properties: this.buildIdentityProperties(stagedEvent.properties),
|
|
858
|
+
});
|
|
859
|
+
}
|
|
860
|
+
this.stagedEvents = [];
|
|
861
|
+
Logger.log('Journium: Staged events processed and moved to main queue');
|
|
862
|
+
if (this.queue.length >= this.effectiveOptions.flushAt) {
|
|
863
|
+
this.flush();
|
|
864
|
+
}
|
|
865
|
+
}
|
|
866
|
+
startFlushTimer() {
|
|
867
|
+
if (this.flushTimer) {
|
|
868
|
+
clearInterval(this.flushTimer);
|
|
869
|
+
}
|
|
870
|
+
this.flushTimer = setInterval(() => {
|
|
871
|
+
this.flush();
|
|
872
|
+
}, this.effectiveOptions.flushInterval);
|
|
873
|
+
}
|
|
874
|
+
startRemoteOptionsRefreshTimer() {
|
|
875
|
+
// Clear any existing timer to prevent duplicate intervals
|
|
876
|
+
if (this.remoteOptionsRefreshTimer) {
|
|
877
|
+
clearInterval(this.remoteOptionsRefreshTimer);
|
|
878
|
+
this.remoteOptionsRefreshTimer = null;
|
|
879
|
+
}
|
|
880
|
+
this.remoteOptionsRefreshTimer = setInterval(() => {
|
|
881
|
+
this.refreshRemoteOptions();
|
|
882
|
+
}, JourniumClient.REMOTE_OPTIONS_REFRESH_INTERVAL);
|
|
883
|
+
Logger.log(`Journium: Scheduling remote options refresh every ${JourniumClient.REMOTE_OPTIONS_REFRESH_INTERVAL}ms`);
|
|
884
|
+
}
|
|
885
|
+
async refreshRemoteOptions() {
|
|
886
|
+
if (this.isRefreshing) {
|
|
887
|
+
Logger.log('Journium: Remote options refresh already in progress, skipping');
|
|
888
|
+
return;
|
|
889
|
+
}
|
|
890
|
+
this.isRefreshing = true;
|
|
891
|
+
Logger.log('Journium: Periodic remote options refresh triggered');
|
|
892
|
+
try {
|
|
893
|
+
const remoteOptions = await this.fetchRemoteOptionsWithRetry();
|
|
894
|
+
if (!remoteOptions) {
|
|
895
|
+
Logger.warn('Journium: Periodic remote options refresh failed, keeping current options');
|
|
896
|
+
return;
|
|
897
|
+
}
|
|
898
|
+
const prevRemoteSnapshot = JSON.stringify(this.lastRemoteOptions);
|
|
899
|
+
const prevFlushInterval = this.effectiveOptions.flushInterval;
|
|
900
|
+
this.applyRemoteOptions(remoteOptions);
|
|
901
|
+
if (prevRemoteSnapshot === JSON.stringify(this.lastRemoteOptions)) {
|
|
902
|
+
Logger.log('Journium: Remote options unchanged after refresh, no update needed');
|
|
903
|
+
return;
|
|
904
|
+
}
|
|
905
|
+
Logger.log('Journium: Remote options updated after refresh:', this.effectiveOptions);
|
|
906
|
+
if (this.effectiveOptions.flushInterval !== prevFlushInterval) {
|
|
907
|
+
if (this.effectiveOptions.flushInterval && this.effectiveOptions.flushInterval > 0) {
|
|
908
|
+
this.startFlushTimer();
|
|
909
|
+
}
|
|
910
|
+
else if (this.flushTimer) {
|
|
911
|
+
clearInterval(this.flushTimer);
|
|
912
|
+
this.flushTimer = null;
|
|
913
|
+
}
|
|
914
|
+
}
|
|
915
|
+
this.notifyOptionsChange();
|
|
916
|
+
}
|
|
917
|
+
catch (error) {
|
|
918
|
+
Logger.error('Journium: Periodic remote options refresh encountered an error:', error);
|
|
919
|
+
}
|
|
920
|
+
finally {
|
|
921
|
+
this.isRefreshing = false;
|
|
922
|
+
}
|
|
923
|
+
}
|
|
924
|
+
applyRemoteOptions(remoteOptions) {
|
|
925
|
+
var _a;
|
|
926
|
+
this.lastRemoteOptions = remoteOptions;
|
|
927
|
+
this.effectiveOptions = this.config.options
|
|
928
|
+
? mergeOptions(this.config.options, remoteOptions)
|
|
929
|
+
: remoteOptions;
|
|
930
|
+
this.saveCachedOptions(remoteOptions);
|
|
931
|
+
if (this.effectiveOptions.sessionTimeout) {
|
|
932
|
+
this.identityManager.updateSessionTimeout(this.effectiveOptions.sessionTimeout);
|
|
933
|
+
}
|
|
934
|
+
Logger.setDebug((_a = this.effectiveOptions.debug) !== null && _a !== void 0 ? _a : false);
|
|
935
|
+
}
|
|
936
|
+
buildIdentityProperties(userProperties = {}) {
|
|
937
|
+
const identity = this.identityManager.getIdentity();
|
|
938
|
+
const userAgentInfo = this.identityManager.getUserAgentInfo();
|
|
939
|
+
return {
|
|
940
|
+
$device_id: identity === null || identity === void 0 ? void 0 : identity.$device_id,
|
|
941
|
+
distinct_id: identity === null || identity === void 0 ? void 0 : identity.distinct_id,
|
|
942
|
+
$session_id: identity === null || identity === void 0 ? void 0 : identity.$session_id,
|
|
943
|
+
$is_identified: (identity === null || identity === void 0 ? void 0 : identity.$user_state) === 'identified',
|
|
944
|
+
$current_url: typeof window !== 'undefined' ? window.location.href : '',
|
|
945
|
+
$pathname: typeof window !== 'undefined' ? window.location.pathname : '',
|
|
946
|
+
...userAgentInfo,
|
|
947
|
+
$lib_version: '0.1.0', // TODO: Get from package.json
|
|
948
|
+
$platform: 'web',
|
|
949
|
+
...userProperties,
|
|
950
|
+
};
|
|
951
|
+
}
|
|
952
|
+
async sendEvents(events) {
|
|
953
|
+
if (!events.length)
|
|
954
|
+
return;
|
|
955
|
+
try {
|
|
956
|
+
const response = await fetch(`${this.config.apiHost}/v1/ingest_event`, {
|
|
957
|
+
method: 'POST',
|
|
958
|
+
headers: {
|
|
959
|
+
'Content-Type': 'application/json',
|
|
960
|
+
'Authorization': `Bearer ${this.config.publishableKey}`,
|
|
961
|
+
},
|
|
962
|
+
body: JSON.stringify({
|
|
963
|
+
events,
|
|
964
|
+
}),
|
|
965
|
+
});
|
|
966
|
+
if (!response.ok) {
|
|
967
|
+
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
|
|
968
|
+
}
|
|
969
|
+
Logger.log('Journium: Successfully sent events', events);
|
|
970
|
+
}
|
|
971
|
+
catch (error) {
|
|
972
|
+
Logger.error('Journium: Failed to send events', error);
|
|
973
|
+
throw error;
|
|
974
|
+
}
|
|
975
|
+
}
|
|
976
|
+
identify(distinctId, attributes = {}) {
|
|
977
|
+
// Don't identify if initialization failed
|
|
978
|
+
if (this.initializationFailed) {
|
|
979
|
+
Logger.warn('Journium: identify() call rejected - initialization failed');
|
|
980
|
+
return;
|
|
981
|
+
}
|
|
982
|
+
// Call identify on identity manager to get previous distinct ID
|
|
983
|
+
const { previousDistinctId } = this.identityManager.identify(distinctId, attributes);
|
|
984
|
+
// Track $identify event with previous distinct ID
|
|
985
|
+
const identifyProperties = {
|
|
986
|
+
...attributes,
|
|
987
|
+
$anon_distinct_id: previousDistinctId,
|
|
988
|
+
};
|
|
989
|
+
this.track('$identify', identifyProperties);
|
|
990
|
+
Logger.log('Journium: User identified', { distinctId, attributes, previousDistinctId });
|
|
991
|
+
}
|
|
992
|
+
reset() {
|
|
993
|
+
// Don't reset if initialization failed
|
|
994
|
+
if (this.initializationFailed) {
|
|
995
|
+
Logger.warn('Journium: reset() call rejected - initialization failed');
|
|
996
|
+
return;
|
|
997
|
+
}
|
|
998
|
+
// Reset identity in identity manager
|
|
999
|
+
this.identityManager.reset();
|
|
1000
|
+
Logger.log('Journium: User identity reset');
|
|
1001
|
+
}
|
|
1002
|
+
track(event, properties = {}) {
|
|
1003
|
+
// Create minimal event without identity properties (will be added later if staging)
|
|
1004
|
+
const journiumEvent = {
|
|
1005
|
+
uuid: generateUuidv7(),
|
|
1006
|
+
ingestion_key: this.config.publishableKey,
|
|
1007
|
+
client_timestamp: getCurrentTimestamp(),
|
|
1008
|
+
event,
|
|
1009
|
+
properties: { ...properties }, // Only user properties for now
|
|
1010
|
+
};
|
|
1011
|
+
if (!this.initializationComplete) {
|
|
1012
|
+
if (this.initializationFailed) {
|
|
1013
|
+
Logger.warn('Journium: track() call rejected - initialization failed');
|
|
1014
|
+
return;
|
|
1015
|
+
}
|
|
1016
|
+
this.stagedEvents.push(journiumEvent);
|
|
1017
|
+
Logger.log('Journium: Event staged during initialization', journiumEvent);
|
|
1018
|
+
return;
|
|
1019
|
+
}
|
|
1020
|
+
if (this.ingestionPaused) {
|
|
1021
|
+
Logger.warn('Journium: Ingestion is paused — event dropped:', journiumEvent.event);
|
|
1022
|
+
return;
|
|
1023
|
+
}
|
|
1024
|
+
const eventWithIdentity = {
|
|
1025
|
+
...journiumEvent,
|
|
1026
|
+
properties: this.buildIdentityProperties(properties),
|
|
1027
|
+
};
|
|
1028
|
+
this.queue.push(eventWithIdentity);
|
|
1029
|
+
Logger.log('Journium: Event tracked', eventWithIdentity);
|
|
1030
|
+
if (this.effectiveOptions.flushAt && this.queue.length >= this.effectiveOptions.flushAt) {
|
|
1031
|
+
this.flush();
|
|
1032
|
+
}
|
|
1033
|
+
}
|
|
1034
|
+
async flush() {
|
|
1035
|
+
// Don't flush if initialization failed
|
|
1036
|
+
if (this.initializationFailed) {
|
|
1037
|
+
Logger.warn('Journium: flush() call rejected - initialization failed');
|
|
1038
|
+
return;
|
|
1039
|
+
}
|
|
1040
|
+
if (this.queue.length === 0)
|
|
1041
|
+
return;
|
|
1042
|
+
const events = [...this.queue];
|
|
1043
|
+
this.queue = [];
|
|
1044
|
+
try {
|
|
1045
|
+
await this.sendEvents(events);
|
|
1046
|
+
}
|
|
1047
|
+
catch (error) {
|
|
1048
|
+
this.queue.unshift(...events);
|
|
1049
|
+
throw error;
|
|
1050
|
+
}
|
|
1051
|
+
}
|
|
1052
|
+
destroy() {
|
|
1053
|
+
if (this.flushTimer) {
|
|
1054
|
+
clearInterval(this.flushTimer);
|
|
1055
|
+
this.flushTimer = null;
|
|
1056
|
+
}
|
|
1057
|
+
if (this.remoteOptionsRefreshTimer) {
|
|
1058
|
+
clearInterval(this.remoteOptionsRefreshTimer);
|
|
1059
|
+
this.remoteOptionsRefreshTimer = null;
|
|
1060
|
+
}
|
|
1061
|
+
this.flush();
|
|
1062
|
+
}
|
|
1063
|
+
getEffectiveOptions() {
|
|
1064
|
+
return this.effectiveOptions;
|
|
1065
|
+
}
|
|
1066
|
+
get ingestionPaused() {
|
|
1067
|
+
return this.effectiveOptions['ingestionPaused'] === true;
|
|
1068
|
+
}
|
|
1069
|
+
}
|
|
1070
|
+
JourniumClient.REMOTE_OPTIONS_REFRESH_INTERVAL = 15 * 60 * 1000; // 15 minutes
|
|
1071
|
+
|
|
1072
|
+
class PageviewTracker {
|
|
1073
|
+
constructor(client) {
|
|
1074
|
+
this.lastUrl = '';
|
|
1075
|
+
this.originalPushState = null;
|
|
1076
|
+
this.originalReplaceState = null;
|
|
1077
|
+
this.popStateHandler = null;
|
|
1078
|
+
this.client = client;
|
|
1079
|
+
}
|
|
1080
|
+
capturePageview(customProperties = {}) {
|
|
1081
|
+
const currentUrl = getCurrentUrl();
|
|
1082
|
+
const url = new URL(currentUrl);
|
|
1083
|
+
const properties = {
|
|
1084
|
+
$current_url: currentUrl,
|
|
1085
|
+
$host: url.host,
|
|
1086
|
+
$pathname: url.pathname,
|
|
1087
|
+
$search: url.search,
|
|
1088
|
+
$title: getPageTitle(),
|
|
1089
|
+
$referrer: getReferrer(),
|
|
1090
|
+
...customProperties,
|
|
1091
|
+
};
|
|
1092
|
+
this.client.track('$pageview', properties);
|
|
1093
|
+
this.lastUrl = currentUrl;
|
|
1094
|
+
}
|
|
1095
|
+
/**
|
|
1096
|
+
* Start automatic autocapture for pageviews
|
|
1097
|
+
* @param captureInitialPageview - whether to fire a $pageview immediately on start (default: true).
|
|
1098
|
+
* Pass false when restarting after a remote options update to avoid a spurious pageview.
|
|
1099
|
+
*/
|
|
1100
|
+
startAutoPageviewTracking(captureInitialPageview = true) {
|
|
1101
|
+
if (captureInitialPageview) {
|
|
1102
|
+
this.capturePageview();
|
|
1103
|
+
}
|
|
1104
|
+
if (typeof window !== 'undefined') {
|
|
1105
|
+
// Store original methods for cleanup
|
|
1106
|
+
this.originalPushState = window.history.pushState;
|
|
1107
|
+
this.originalReplaceState = window.history.replaceState;
|
|
1108
|
+
window.history.pushState = (...args) => {
|
|
1109
|
+
this.originalPushState.apply(window.history, args);
|
|
1110
|
+
setTimeout(() => this.capturePageview(), 0);
|
|
1111
|
+
};
|
|
1112
|
+
window.history.replaceState = (...args) => {
|
|
1113
|
+
this.originalReplaceState.apply(window.history, args);
|
|
1114
|
+
setTimeout(() => this.capturePageview(), 0);
|
|
1115
|
+
};
|
|
1116
|
+
this.popStateHandler = () => {
|
|
1117
|
+
setTimeout(() => this.capturePageview(), 0);
|
|
1118
|
+
};
|
|
1119
|
+
window.addEventListener('popstate', this.popStateHandler);
|
|
1120
|
+
}
|
|
1121
|
+
}
|
|
1122
|
+
/**
|
|
1123
|
+
* Stop automatic autocapture for pageviews
|
|
1124
|
+
* @returns void
|
|
1125
|
+
*/
|
|
1126
|
+
stopAutocapture() {
|
|
1127
|
+
if (typeof window !== 'undefined') {
|
|
1128
|
+
// Restore original methods
|
|
1129
|
+
if (this.originalPushState) {
|
|
1130
|
+
window.history.pushState = this.originalPushState;
|
|
1131
|
+
this.originalPushState = null;
|
|
1132
|
+
}
|
|
1133
|
+
if (this.originalReplaceState) {
|
|
1134
|
+
window.history.replaceState = this.originalReplaceState;
|
|
1135
|
+
this.originalReplaceState = null;
|
|
1136
|
+
}
|
|
1137
|
+
if (this.popStateHandler) {
|
|
1138
|
+
window.removeEventListener('popstate', this.popStateHandler);
|
|
1139
|
+
this.popStateHandler = null;
|
|
1140
|
+
}
|
|
1141
|
+
}
|
|
1142
|
+
}
|
|
1143
|
+
}
|
|
1144
|
+
|
|
1145
|
+
/**
|
|
1146
|
+
* AutocaptureTracker is responsible for tracking user interactions and capturing them as events.
|
|
1147
|
+
*/
|
|
1148
|
+
class AutocaptureTracker {
|
|
1149
|
+
constructor(client, options = {}) {
|
|
1150
|
+
this.listeners = new Map();
|
|
1151
|
+
this.isActive = false;
|
|
1152
|
+
this.client = client;
|
|
1153
|
+
this.options = {
|
|
1154
|
+
captureClicks: true,
|
|
1155
|
+
captureFormSubmits: true,
|
|
1156
|
+
captureFormChanges: true,
|
|
1157
|
+
captureTextSelection: false,
|
|
1158
|
+
ignoreClasses: ['journium-ignore'],
|
|
1159
|
+
ignoreElements: ['script', 'style', 'noscript'],
|
|
1160
|
+
captureContentText: true,
|
|
1161
|
+
...options,
|
|
1162
|
+
};
|
|
1163
|
+
}
|
|
1164
|
+
/**
|
|
1165
|
+
* Update autocapture options and restart if currently active
|
|
1166
|
+
*/
|
|
1167
|
+
updateOptions(options) {
|
|
1168
|
+
const wasActive = this.isActive;
|
|
1169
|
+
// Stop if currently active
|
|
1170
|
+
if (wasActive) {
|
|
1171
|
+
this.stop();
|
|
1172
|
+
}
|
|
1173
|
+
// Update options
|
|
1174
|
+
this.options = {
|
|
1175
|
+
captureClicks: true,
|
|
1176
|
+
captureFormSubmits: true,
|
|
1177
|
+
captureFormChanges: true,
|
|
1178
|
+
captureTextSelection: false,
|
|
1179
|
+
ignoreClasses: ['journium-ignore'],
|
|
1180
|
+
ignoreElements: ['script', 'style', 'noscript'],
|
|
1181
|
+
captureContentText: true,
|
|
1182
|
+
...options,
|
|
1183
|
+
};
|
|
1184
|
+
// Restart if it was active before
|
|
1185
|
+
if (wasActive) {
|
|
1186
|
+
this.start();
|
|
1187
|
+
}
|
|
1188
|
+
}
|
|
1189
|
+
start() {
|
|
1190
|
+
if (!isBrowser() || this.isActive) {
|
|
1191
|
+
return;
|
|
1192
|
+
}
|
|
1193
|
+
this.isActive = true;
|
|
1194
|
+
if (this.options.captureClicks) {
|
|
1195
|
+
this.addClickListener();
|
|
1196
|
+
}
|
|
1197
|
+
if (this.options.captureFormSubmits) {
|
|
1198
|
+
this.addFormSubmitListener();
|
|
1199
|
+
}
|
|
1200
|
+
if (this.options.captureFormChanges) {
|
|
1201
|
+
this.addFormChangeListener();
|
|
1202
|
+
}
|
|
1203
|
+
if (this.options.captureTextSelection) {
|
|
1204
|
+
this.addTextSelectionListener();
|
|
1205
|
+
}
|
|
1206
|
+
}
|
|
1207
|
+
stop() {
|
|
1208
|
+
if (!isBrowser() || !this.isActive) {
|
|
1209
|
+
return;
|
|
1210
|
+
}
|
|
1211
|
+
this.isActive = false;
|
|
1212
|
+
this.listeners.forEach((listener, event) => {
|
|
1213
|
+
document.removeEventListener(event, listener, true);
|
|
1214
|
+
});
|
|
1215
|
+
this.listeners.clear();
|
|
1216
|
+
}
|
|
1217
|
+
addClickListener() {
|
|
1218
|
+
const clickListener = (event) => {
|
|
1219
|
+
const target = event.target;
|
|
1220
|
+
if (this.shouldIgnoreElement(target)) {
|
|
1221
|
+
return;
|
|
1222
|
+
}
|
|
1223
|
+
const properties = this.getElementProperties(target, 'click');
|
|
1224
|
+
this.client.track('$autocapture', {
|
|
1225
|
+
$event_type: 'click',
|
|
1226
|
+
...properties,
|
|
1227
|
+
});
|
|
1228
|
+
};
|
|
1229
|
+
document.addEventListener('click', clickListener, true);
|
|
1230
|
+
this.listeners.set('click', clickListener);
|
|
1231
|
+
}
|
|
1232
|
+
addFormSubmitListener() {
|
|
1233
|
+
const submitListener = (event) => {
|
|
1234
|
+
const target = event.target;
|
|
1235
|
+
if (this.shouldIgnoreElement(target)) {
|
|
1236
|
+
return;
|
|
1237
|
+
}
|
|
1238
|
+
const properties = this.getFormProperties(target, 'submit');
|
|
1239
|
+
this.client.track('$autocapture', {
|
|
1240
|
+
$event_type: 'submit',
|
|
1241
|
+
...properties,
|
|
1242
|
+
});
|
|
1243
|
+
};
|
|
1244
|
+
document.addEventListener('submit', submitListener, true);
|
|
1245
|
+
this.listeners.set('submit', submitListener);
|
|
1246
|
+
}
|
|
1247
|
+
addFormChangeListener() {
|
|
1248
|
+
const changeListener = (event) => {
|
|
1249
|
+
const target = event.target;
|
|
1250
|
+
if (this.shouldIgnoreElement(target) || !this.isFormElement(target)) {
|
|
1251
|
+
return;
|
|
1252
|
+
}
|
|
1253
|
+
const properties = this.getInputProperties(target, 'change');
|
|
1254
|
+
this.client.track('$autocapture', {
|
|
1255
|
+
$event_type: 'change',
|
|
1256
|
+
...properties,
|
|
1257
|
+
});
|
|
1258
|
+
};
|
|
1259
|
+
document.addEventListener('change', changeListener, true);
|
|
1260
|
+
this.listeners.set('change', changeListener);
|
|
1261
|
+
}
|
|
1262
|
+
addTextSelectionListener() {
|
|
1263
|
+
const selectionListener = () => {
|
|
1264
|
+
const selection = window.getSelection();
|
|
1265
|
+
if (!selection || selection.toString().trim().length === 0) {
|
|
1266
|
+
return;
|
|
1267
|
+
}
|
|
1268
|
+
const selectedText = selection.toString().trim();
|
|
1269
|
+
if (selectedText.length < 3) { // Ignore very short selections
|
|
1270
|
+
return;
|
|
1271
|
+
}
|
|
1272
|
+
this.client.track('$autocapture', {
|
|
1273
|
+
$event_type: 'text_selection',
|
|
1274
|
+
$selected_text: selectedText.substring(0, 200), // Limit text length
|
|
1275
|
+
$selection_length: selectedText.length,
|
|
1276
|
+
});
|
|
1277
|
+
};
|
|
1278
|
+
document.addEventListener('mouseup', selectionListener);
|
|
1279
|
+
this.listeners.set('mouseup', selectionListener);
|
|
1280
|
+
}
|
|
1281
|
+
shouldIgnoreElement(element) {
|
|
1282
|
+
var _a, _b, _c;
|
|
1283
|
+
if (!element || !element.tagName) {
|
|
1284
|
+
return true;
|
|
1285
|
+
}
|
|
1286
|
+
// Check if element should be ignored by tag name
|
|
1287
|
+
if ((_a = this.options.ignoreElements) === null || _a === void 0 ? void 0 : _a.includes(element.tagName.toLowerCase())) {
|
|
1288
|
+
return true;
|
|
1289
|
+
}
|
|
1290
|
+
// Check if element has ignore classes
|
|
1291
|
+
if ((_b = this.options.ignoreClasses) === null || _b === void 0 ? void 0 : _b.some(cls => element.classList.contains(cls))) {
|
|
1292
|
+
return true;
|
|
1293
|
+
}
|
|
1294
|
+
// Check parent elements for ignore classes
|
|
1295
|
+
let parent = element.parentElement;
|
|
1296
|
+
while (parent) {
|
|
1297
|
+
if ((_c = this.options.ignoreClasses) === null || _c === void 0 ? void 0 : _c.some(cls => parent.classList.contains(cls))) {
|
|
1298
|
+
return true;
|
|
1299
|
+
}
|
|
1300
|
+
parent = parent.parentElement;
|
|
1301
|
+
}
|
|
1302
|
+
return false;
|
|
1303
|
+
}
|
|
1304
|
+
isFormElement(element) {
|
|
1305
|
+
const formElements = ['input', 'select', 'textarea'];
|
|
1306
|
+
return formElements.includes(element.tagName.toLowerCase());
|
|
1307
|
+
}
|
|
1308
|
+
getElementProperties(element, eventType) {
|
|
1309
|
+
const properties = {
|
|
1310
|
+
$element_tag: element.tagName.toLowerCase(),
|
|
1311
|
+
$element_type: this.getElementType(element),
|
|
1312
|
+
};
|
|
1313
|
+
// Element identifiers
|
|
1314
|
+
if (element.id) {
|
|
1315
|
+
properties.$element_id = element.id;
|
|
1316
|
+
}
|
|
1317
|
+
if (element.className) {
|
|
1318
|
+
properties.$element_classes = Array.from(element.classList);
|
|
1319
|
+
}
|
|
1320
|
+
// Element attributes
|
|
1321
|
+
const relevantAttributes = ['name', 'role', 'aria-label', 'data-testid', 'data-track'];
|
|
1322
|
+
relevantAttributes.forEach(attr => {
|
|
1323
|
+
const value = element.getAttribute(attr);
|
|
1324
|
+
if (value) {
|
|
1325
|
+
properties[`$element_${attr.replace('-', '_')}`] = value;
|
|
1326
|
+
}
|
|
1327
|
+
});
|
|
1328
|
+
// Element content
|
|
1329
|
+
if (this.options.captureContentText) {
|
|
1330
|
+
const text = this.getElementText(element);
|
|
1331
|
+
if (text) {
|
|
1332
|
+
properties.$element_text = text.substring(0, 200); // Limit text length
|
|
1333
|
+
}
|
|
1334
|
+
}
|
|
1335
|
+
// Elements chain data
|
|
1336
|
+
const elementsChain = this.getElementsChain(element);
|
|
1337
|
+
properties.$elements_chain = elementsChain.chain;
|
|
1338
|
+
properties.$elements_chain_href = elementsChain.href;
|
|
1339
|
+
properties.$elements_chain_elements = elementsChain.elements;
|
|
1340
|
+
properties.$elements_chain_texts = elementsChain.texts;
|
|
1341
|
+
properties.$elements_chain_ids = elementsChain.ids;
|
|
1342
|
+
// Position information
|
|
1343
|
+
const rect = element.getBoundingClientRect();
|
|
1344
|
+
properties.$element_position = {
|
|
1345
|
+
x: Math.round(rect.left),
|
|
1346
|
+
y: Math.round(rect.top),
|
|
1347
|
+
width: Math.round(rect.width),
|
|
1348
|
+
height: Math.round(rect.height),
|
|
1349
|
+
};
|
|
1350
|
+
// Parent information
|
|
1351
|
+
if (element.parentElement) {
|
|
1352
|
+
properties.$parent_tag = element.parentElement.tagName.toLowerCase();
|
|
1353
|
+
if (element.parentElement.id) {
|
|
1354
|
+
properties.$parent_id = element.parentElement.id;
|
|
1355
|
+
}
|
|
1356
|
+
}
|
|
1357
|
+
// URL information
|
|
1358
|
+
properties.$current_url = window.location.href;
|
|
1359
|
+
properties.$host = window.location.host;
|
|
1360
|
+
properties.$pathname = window.location.pathname;
|
|
1361
|
+
return properties;
|
|
1362
|
+
}
|
|
1363
|
+
getFormProperties(form, eventType) {
|
|
1364
|
+
const properties = this.getElementProperties(form, eventType);
|
|
1365
|
+
// Form-specific properties
|
|
1366
|
+
properties.$form_method = form.method || 'get';
|
|
1367
|
+
properties.$form_action = form.action || '';
|
|
1368
|
+
// Count form elements
|
|
1369
|
+
const inputs = form.querySelectorAll('input, select, textarea');
|
|
1370
|
+
properties.$form_elements_count = inputs.length;
|
|
1371
|
+
// Form element types
|
|
1372
|
+
const elementTypes = {};
|
|
1373
|
+
inputs.forEach(input => {
|
|
1374
|
+
const type = this.getElementType(input);
|
|
1375
|
+
elementTypes[type] = (elementTypes[type] || 0) + 1;
|
|
1376
|
+
});
|
|
1377
|
+
properties.$form_element_types = elementTypes;
|
|
1378
|
+
return properties;
|
|
1379
|
+
}
|
|
1380
|
+
getInputProperties(input, eventType) {
|
|
1381
|
+
const properties = this.getElementProperties(input, eventType);
|
|
1382
|
+
// Input-specific properties
|
|
1383
|
+
properties.$input_type = input.type || 'text';
|
|
1384
|
+
if (input.name) {
|
|
1385
|
+
properties.$input_name = input.name;
|
|
1386
|
+
}
|
|
1387
|
+
if (input.placeholder) {
|
|
1388
|
+
properties.$input_placeholder = input.placeholder;
|
|
1389
|
+
}
|
|
1390
|
+
// Value information (be careful with sensitive data)
|
|
1391
|
+
if (this.isSafeInputType(input.type)) {
|
|
1392
|
+
if (input.type === 'checkbox' || input.type === 'radio') {
|
|
1393
|
+
properties.$input_checked = input.checked;
|
|
1394
|
+
}
|
|
1395
|
+
else if (input.value) {
|
|
1396
|
+
// For safe inputs, capture value length and basic characteristics
|
|
1397
|
+
properties.$input_value_length = input.value.length;
|
|
1398
|
+
properties.$input_has_value = input.value.length > 0;
|
|
1399
|
+
// For select elements, capture the selected value
|
|
1400
|
+
if (input.tagName.toLowerCase() === 'select') {
|
|
1401
|
+
properties.$input_selected_value = input.value;
|
|
1402
|
+
}
|
|
1403
|
+
}
|
|
1404
|
+
}
|
|
1405
|
+
// Form context
|
|
1406
|
+
const form = input.closest('form');
|
|
1407
|
+
if (form && form.id) {
|
|
1408
|
+
properties.$form_id = form.id;
|
|
1409
|
+
}
|
|
1410
|
+
return properties;
|
|
1411
|
+
}
|
|
1412
|
+
getElementType(element) {
|
|
1413
|
+
const tag = element.tagName.toLowerCase();
|
|
1414
|
+
if (tag === 'input') {
|
|
1415
|
+
return element.type || 'text';
|
|
1416
|
+
}
|
|
1417
|
+
if (tag === 'button') {
|
|
1418
|
+
return element.type || 'button';
|
|
1419
|
+
}
|
|
1420
|
+
return tag;
|
|
1421
|
+
}
|
|
1422
|
+
getElementText(element) {
|
|
1423
|
+
var _a, _b;
|
|
1424
|
+
// For buttons and links, get the visible text
|
|
1425
|
+
if (['button', 'a'].includes(element.tagName.toLowerCase())) {
|
|
1426
|
+
return ((_a = element.textContent) === null || _a === void 0 ? void 0 : _a.trim()) || '';
|
|
1427
|
+
}
|
|
1428
|
+
// For inputs, get placeholder or label
|
|
1429
|
+
if (element.tagName.toLowerCase() === 'input') {
|
|
1430
|
+
const input = element;
|
|
1431
|
+
return input.placeholder || input.value || '';
|
|
1432
|
+
}
|
|
1433
|
+
// For other elements, get text content but limit it
|
|
1434
|
+
const text = ((_b = element.textContent) === null || _b === void 0 ? void 0 : _b.trim()) || '';
|
|
1435
|
+
return text.length > 50 ? text.substring(0, 47) + '...' : text;
|
|
1436
|
+
}
|
|
1437
|
+
getElementsChain(element) {
|
|
1438
|
+
var _a;
|
|
1439
|
+
const elements = [];
|
|
1440
|
+
const texts = [];
|
|
1441
|
+
const ids = [];
|
|
1442
|
+
let href = '';
|
|
1443
|
+
let current = element;
|
|
1444
|
+
while (current && current !== document.body) {
|
|
1445
|
+
// Element selector
|
|
1446
|
+
let selector = current.tagName.toLowerCase();
|
|
1447
|
+
// Add ID if present
|
|
1448
|
+
if (current.id) {
|
|
1449
|
+
selector += `#${current.id}`;
|
|
1450
|
+
ids.push(current.id);
|
|
1451
|
+
}
|
|
1452
|
+
else {
|
|
1453
|
+
ids.push('');
|
|
1454
|
+
}
|
|
1455
|
+
// Add classes if present
|
|
1456
|
+
if (current.className && typeof current.className === 'string') {
|
|
1457
|
+
const classes = current.className.trim().split(/\s+/).slice(0, 3); // Limit to first 3 classes
|
|
1458
|
+
if (classes.length > 0 && classes[0] !== '') {
|
|
1459
|
+
selector += '.' + classes.join('.');
|
|
1460
|
+
}
|
|
1461
|
+
}
|
|
1462
|
+
// Add nth-child if no ID (to make selector more specific)
|
|
1463
|
+
if (!current.id && current.parentElement) {
|
|
1464
|
+
const siblings = Array.from(current.parentElement.children)
|
|
1465
|
+
.filter(child => child.tagName === current.tagName);
|
|
1466
|
+
if (siblings.length > 1) {
|
|
1467
|
+
const index = siblings.indexOf(current) + 1;
|
|
1468
|
+
selector += `:nth-child(${index})`;
|
|
1469
|
+
}
|
|
1470
|
+
}
|
|
1471
|
+
elements.push(selector);
|
|
1472
|
+
// Extract text content
|
|
1473
|
+
let text = '';
|
|
1474
|
+
if (current.tagName.toLowerCase() === 'a') {
|
|
1475
|
+
text = ((_a = current.textContent) === null || _a === void 0 ? void 0 : _a.trim()) || '';
|
|
1476
|
+
// Capture href for links
|
|
1477
|
+
if (!href && current.getAttribute('href')) {
|
|
1478
|
+
href = current.getAttribute('href') || '';
|
|
1479
|
+
}
|
|
1480
|
+
}
|
|
1481
|
+
else if (['button', 'span', 'div'].includes(current.tagName.toLowerCase())) {
|
|
1482
|
+
// For buttons and text elements, get direct text content (not including children)
|
|
1483
|
+
const directText = Array.from(current.childNodes)
|
|
1484
|
+
.filter(node => node.nodeType === Node.TEXT_NODE)
|
|
1485
|
+
.map(node => { var _a; return (_a = node.textContent) === null || _a === void 0 ? void 0 : _a.trim(); })
|
|
1486
|
+
.join(' ')
|
|
1487
|
+
.trim();
|
|
1488
|
+
text = directText || '';
|
|
1489
|
+
}
|
|
1490
|
+
else if (current.tagName.toLowerCase() === 'input') {
|
|
1491
|
+
const input = current;
|
|
1492
|
+
text = input.placeholder || input.value || '';
|
|
1493
|
+
}
|
|
1494
|
+
// Limit text length and clean it
|
|
1495
|
+
text = text.substring(0, 100).replace(/\s+/g, ' ').trim();
|
|
1496
|
+
texts.push(text);
|
|
1497
|
+
current = current.parentElement;
|
|
1498
|
+
}
|
|
1499
|
+
// Build the chain string (reverse order so it goes from parent to child)
|
|
1500
|
+
const chain = elements.reverse().join(' > ');
|
|
1501
|
+
return {
|
|
1502
|
+
chain,
|
|
1503
|
+
href,
|
|
1504
|
+
elements: elements,
|
|
1505
|
+
texts: texts.reverse(),
|
|
1506
|
+
ids: ids.reverse()
|
|
1507
|
+
};
|
|
1508
|
+
}
|
|
1509
|
+
isSafeInputType(type) {
|
|
1510
|
+
// Don't capture values for sensitive input types
|
|
1511
|
+
const sensitiveTypes = ['password', 'email', 'tel', 'credit-card-number'];
|
|
1512
|
+
return !sensitiveTypes.includes(type.toLowerCase());
|
|
1513
|
+
}
|
|
1514
|
+
}
|
|
1515
|
+
|
|
1516
|
+
class JourniumAnalytics {
|
|
1517
|
+
constructor(config) {
|
|
1518
|
+
this.autocaptureStarted = false;
|
|
1519
|
+
this.config = config;
|
|
1520
|
+
this.client = new JourniumClient(config);
|
|
1521
|
+
this.pageviewTracker = new PageviewTracker(this.client);
|
|
1522
|
+
// Initialize autocapture tracker with effective options (may include cached remote options)
|
|
1523
|
+
// This ensures we use the correct initial state even if cached remote options exist
|
|
1524
|
+
const initialEffectiveOptions = this.client.getEffectiveOptions();
|
|
1525
|
+
const initialAutocaptureOptions = this.resolveAutocaptureOptions(initialEffectiveOptions.autocapture);
|
|
1526
|
+
this.autocaptureTracker = new AutocaptureTracker(this.client, initialAutocaptureOptions);
|
|
1527
|
+
// Listen for options changes (e.g., when fresh remote options are fetched)
|
|
1528
|
+
this.unsubscribeOptionsChange = this.client.onOptionsChange((effectiveOptions) => {
|
|
1529
|
+
this.handleOptionsChange(effectiveOptions);
|
|
1530
|
+
});
|
|
1531
|
+
// Start automatic autocapture immediately if initial options support it
|
|
1532
|
+
// This handles cached remote options or local options with autocapture enabled
|
|
1533
|
+
this.startAutocaptureIfEnabled(initialEffectiveOptions);
|
|
1534
|
+
}
|
|
1535
|
+
resolveAutocaptureOptions(autocapture) {
|
|
1536
|
+
if (autocapture === false) {
|
|
1537
|
+
return {
|
|
1538
|
+
captureClicks: false,
|
|
1539
|
+
captureFormSubmits: false,
|
|
1540
|
+
captureFormChanges: false,
|
|
1541
|
+
captureTextSelection: false,
|
|
1542
|
+
};
|
|
1543
|
+
}
|
|
1544
|
+
if (autocapture === true || autocapture === undefined) {
|
|
1545
|
+
return {}; // Use default configuration (enabled by default)
|
|
1546
|
+
}
|
|
1547
|
+
return autocapture;
|
|
1548
|
+
}
|
|
1549
|
+
track(event, properties) {
|
|
1550
|
+
this.client.track(event, properties);
|
|
1551
|
+
}
|
|
1552
|
+
identify(distinctId, attributes) {
|
|
1553
|
+
this.client.identify(distinctId, attributes);
|
|
1554
|
+
}
|
|
1555
|
+
reset() {
|
|
1556
|
+
this.client.reset();
|
|
1557
|
+
}
|
|
1558
|
+
capturePageview(properties) {
|
|
1559
|
+
this.pageviewTracker.capturePageview(properties);
|
|
1560
|
+
}
|
|
1561
|
+
startAutocapture() {
|
|
1562
|
+
// Always check effective options (which may include remote options)
|
|
1563
|
+
const effectiveOptions = this.client.getEffectiveOptions();
|
|
1564
|
+
// Only enable if effectiveOptions are loaded and autoTrackPageviews is not explicitly false
|
|
1565
|
+
const autoTrackPageviews = effectiveOptions && Object.keys(effectiveOptions).length > 0
|
|
1566
|
+
? effectiveOptions.autoTrackPageviews !== false
|
|
1567
|
+
: false;
|
|
1568
|
+
const autocaptureEnabled = effectiveOptions && Object.keys(effectiveOptions).length > 0
|
|
1569
|
+
? effectiveOptions.autocapture !== false
|
|
1570
|
+
: false;
|
|
1571
|
+
// Update autocapture tracker options if they've changed
|
|
1572
|
+
const autocaptureOptions = this.resolveAutocaptureOptions(effectiveOptions.autocapture);
|
|
1573
|
+
this.autocaptureTracker.updateOptions(autocaptureOptions);
|
|
1574
|
+
if (autoTrackPageviews) {
|
|
1575
|
+
this.pageviewTracker.startAutoPageviewTracking();
|
|
1576
|
+
}
|
|
1577
|
+
if (autocaptureEnabled) {
|
|
1578
|
+
this.autocaptureTracker.start();
|
|
1579
|
+
}
|
|
1580
|
+
this.autocaptureStarted = true;
|
|
1581
|
+
}
|
|
1582
|
+
stopAutocapture() {
|
|
1583
|
+
this.pageviewTracker.stopAutocapture();
|
|
1584
|
+
this.autocaptureTracker.stop();
|
|
1585
|
+
this.autocaptureStarted = false;
|
|
1586
|
+
}
|
|
1587
|
+
/**
|
|
1588
|
+
* Automatically start autocapture if enabled in options
|
|
1589
|
+
* Handles both initial options and empty options during remote-first initialization
|
|
1590
|
+
*/
|
|
1591
|
+
startAutocaptureIfEnabled(effectiveOptions) {
|
|
1592
|
+
// Skip if autocapture was already started manually
|
|
1593
|
+
if (this.autocaptureStarted) {
|
|
1594
|
+
return;
|
|
1595
|
+
}
|
|
1596
|
+
// During remote-first initialization, effective options might be empty initially
|
|
1597
|
+
// Only auto-start if we have actual options loaded, not empty options
|
|
1598
|
+
const hasActualOptions = effectiveOptions && Object.keys(effectiveOptions).length > 0;
|
|
1599
|
+
if (hasActualOptions) {
|
|
1600
|
+
// Use same logic as manual startAutocapture() but only start automatically
|
|
1601
|
+
const autoTrackPageviews = effectiveOptions.autoTrackPageviews !== false;
|
|
1602
|
+
const autocaptureEnabled = effectiveOptions.autocapture !== false;
|
|
1603
|
+
// Update autocapture tracker options
|
|
1604
|
+
const autocaptureOptions = this.resolveAutocaptureOptions(effectiveOptions.autocapture);
|
|
1605
|
+
this.autocaptureTracker.updateOptions(autocaptureOptions);
|
|
1606
|
+
if (autoTrackPageviews) {
|
|
1607
|
+
this.pageviewTracker.startAutoPageviewTracking();
|
|
1608
|
+
}
|
|
1609
|
+
if (autocaptureEnabled) {
|
|
1610
|
+
this.autocaptureTracker.start();
|
|
1611
|
+
}
|
|
1612
|
+
if (autoTrackPageviews || autocaptureEnabled) {
|
|
1613
|
+
this.autocaptureStarted = true;
|
|
1614
|
+
}
|
|
1615
|
+
}
|
|
1616
|
+
// If options are empty (during initialization), wait for options change callback
|
|
1617
|
+
}
|
|
1618
|
+
/**
|
|
1619
|
+
* Handle effective options change (e.g., when remote options are fetched)
|
|
1620
|
+
*/
|
|
1621
|
+
handleOptionsChange(effectiveOptions) {
|
|
1622
|
+
// If autocapture was never started before, this is the initial options application
|
|
1623
|
+
// (async init completed) — treat it like a page load and capture a pageview.
|
|
1624
|
+
// If it was already started, this is a periodic remote options update — only
|
|
1625
|
+
// re-register listeners without emitting a spurious pageview.
|
|
1626
|
+
const isFirstStart = !this.autocaptureStarted;
|
|
1627
|
+
if (this.autocaptureStarted) {
|
|
1628
|
+
this.pageviewTracker.stopAutocapture();
|
|
1629
|
+
this.autocaptureTracker.stop();
|
|
1630
|
+
this.autocaptureStarted = false;
|
|
1631
|
+
}
|
|
1632
|
+
const autoTrackPageviews = effectiveOptions.autoTrackPageviews !== false;
|
|
1633
|
+
const autocaptureEnabled = effectiveOptions.autocapture !== false;
|
|
1634
|
+
const autocaptureOptions = this.resolveAutocaptureOptions(effectiveOptions.autocapture);
|
|
1635
|
+
this.autocaptureTracker.updateOptions(autocaptureOptions);
|
|
1636
|
+
if (autoTrackPageviews) {
|
|
1637
|
+
this.pageviewTracker.startAutoPageviewTracking(isFirstStart);
|
|
1638
|
+
}
|
|
1639
|
+
if (autocaptureEnabled) {
|
|
1640
|
+
this.autocaptureTracker.start();
|
|
1641
|
+
}
|
|
1642
|
+
this.autocaptureStarted = autoTrackPageviews || autocaptureEnabled;
|
|
1643
|
+
}
|
|
1644
|
+
async flush() {
|
|
1645
|
+
return this.client.flush();
|
|
1646
|
+
}
|
|
1647
|
+
getEffectiveOptions() {
|
|
1648
|
+
return this.client.getEffectiveOptions();
|
|
1649
|
+
}
|
|
1650
|
+
/**
|
|
1651
|
+
* Register a callback to be notified when effective options change
|
|
1652
|
+
*/
|
|
1653
|
+
onOptionsChange(callback) {
|
|
1654
|
+
return this.client.onOptionsChange(callback);
|
|
1655
|
+
}
|
|
1656
|
+
destroy() {
|
|
1657
|
+
this.pageviewTracker.stopAutocapture();
|
|
1658
|
+
this.autocaptureTracker.stop();
|
|
1659
|
+
if (this.unsubscribeOptionsChange) {
|
|
1660
|
+
this.unsubscribeOptionsChange();
|
|
1661
|
+
}
|
|
1662
|
+
this.client.destroy();
|
|
1663
|
+
}
|
|
1664
|
+
}
|
|
1665
|
+
const init = (config) => {
|
|
1666
|
+
return new JourniumAnalytics(config);
|
|
1667
|
+
};
|
|
1668
|
+
|
|
1669
|
+
/**
|
|
1670
|
+
* CDN Entry Point for Journium Analytics
|
|
1671
|
+
* This file provides global browser integration for script snippet usage
|
|
1672
|
+
* Features: auto method stubbing, error handling, improved queue processing
|
|
1673
|
+
*/
|
|
1674
|
+
// Available public methods for auto-stubbing
|
|
1675
|
+
const STUBBED_METHODS = [
|
|
1676
|
+
'track', 'identify', 'reset', 'capturePageview', 'startAutocapture',
|
|
1677
|
+
'stopAutocapture', 'flush', 'getEffectiveOptions', 'onOptionsChange', 'destroy'
|
|
1678
|
+
];
|
|
1679
|
+
/**
|
|
1680
|
+
* Enhanced queue processing with better error handling and edge cases
|
|
1681
|
+
*/
|
|
1682
|
+
function processQueuedCalls(instance, queue) {
|
|
1683
|
+
if (!Array.isArray(queue) || queue.length === 0) {
|
|
1684
|
+
return;
|
|
1685
|
+
}
|
|
1686
|
+
const processedCount = { success: 0, error: 0, skipped: 0 };
|
|
1687
|
+
queue.forEach(([method, ...args], index) => {
|
|
1688
|
+
try {
|
|
1689
|
+
// Validate method call structure
|
|
1690
|
+
if (typeof method !== 'string') {
|
|
1691
|
+
console.warn(`Journium: Invalid method call at index ${index}:`, method);
|
|
1692
|
+
processedCount.skipped++;
|
|
1693
|
+
return;
|
|
1694
|
+
}
|
|
1695
|
+
// Check if method exists and is callable
|
|
1696
|
+
if (method in instance && typeof instance[method] === 'function') {
|
|
1697
|
+
// Handle async methods properly
|
|
1698
|
+
const result = instance[method](...args);
|
|
1699
|
+
// If it's a promise, handle potential rejections
|
|
1700
|
+
if (result && typeof result.catch === 'function') {
|
|
1701
|
+
result.catch((error) => {
|
|
1702
|
+
console.warn(`Journium: Async method '${method}' failed:`, error);
|
|
1703
|
+
});
|
|
1704
|
+
}
|
|
1705
|
+
processedCount.success++;
|
|
1706
|
+
}
|
|
1707
|
+
else {
|
|
1708
|
+
console.warn(`Journium: Unknown method '${method}' called from queue`);
|
|
1709
|
+
processedCount.skipped++;
|
|
1710
|
+
}
|
|
1711
|
+
}
|
|
1712
|
+
catch (error) {
|
|
1713
|
+
console.warn(`Journium: Error executing queued method '${method}':`, error);
|
|
1714
|
+
processedCount.error++;
|
|
1715
|
+
}
|
|
1716
|
+
});
|
|
1717
|
+
// Log summary for debugging
|
|
1718
|
+
if (processedCount.success > 0 || processedCount.error > 0) {
|
|
1719
|
+
console.log(`Journium: Processed ${processedCount.success} calls, ${processedCount.error} errors, ${processedCount.skipped} skipped`);
|
|
1720
|
+
}
|
|
1721
|
+
}
|
|
1722
|
+
/**
|
|
1723
|
+
* Dynamically stub all available methods
|
|
1724
|
+
*/
|
|
1725
|
+
function createMethodStubs(target, queueTarget = []) {
|
|
1726
|
+
STUBBED_METHODS.forEach(method => {
|
|
1727
|
+
target[method] = function (...args) {
|
|
1728
|
+
queueTarget.push([method, ...args]);
|
|
1729
|
+
return target; // Enable method chaining
|
|
1730
|
+
};
|
|
1731
|
+
});
|
|
1732
|
+
}
|
|
1733
|
+
/**
|
|
1734
|
+
* Dynamically bind all available methods from instance to target
|
|
1735
|
+
*/
|
|
1736
|
+
function bindInstanceMethods(instance, target) {
|
|
1737
|
+
STUBBED_METHODS.forEach(method => {
|
|
1738
|
+
if (method in instance && typeof instance[method] === 'function') {
|
|
1739
|
+
target[method] = instance[method].bind(instance);
|
|
1740
|
+
}
|
|
1741
|
+
});
|
|
1742
|
+
}
|
|
1743
|
+
/**
|
|
1744
|
+
* Create fallback object when initialization fails
|
|
1745
|
+
*/
|
|
1746
|
+
function createFallbackObject(error) {
|
|
1747
|
+
const fallbackQueue = [];
|
|
1748
|
+
const fallback = {
|
|
1749
|
+
init: (config, instanceName) => {
|
|
1750
|
+
console.warn('Journium: Fallback mode - init calls will be queued');
|
|
1751
|
+
fallbackQueue.push(['init', config, instanceName]);
|
|
1752
|
+
return {};
|
|
1753
|
+
},
|
|
1754
|
+
_error: error || null,
|
|
1755
|
+
_fallback: true
|
|
1756
|
+
};
|
|
1757
|
+
// Create method stubs that queue calls
|
|
1758
|
+
createMethodStubs(fallback, fallbackQueue);
|
|
1759
|
+
return fallback;
|
|
1760
|
+
}
|
|
1761
|
+
/**
|
|
1762
|
+
* Create the global journium object with enhanced error handling
|
|
1763
|
+
*/
|
|
1764
|
+
function createGlobalJournium() {
|
|
1765
|
+
let defaultInstance = null;
|
|
1766
|
+
const globalJournium = {
|
|
1767
|
+
init: (config, instanceName) => {
|
|
1768
|
+
try {
|
|
1769
|
+
// Validate config object
|
|
1770
|
+
if (!config || typeof config !== 'object') {
|
|
1771
|
+
throw new Error('Config object is required');
|
|
1772
|
+
}
|
|
1773
|
+
if (!config.publishableKey) {
|
|
1774
|
+
throw new Error('publishableKey is required in config');
|
|
1775
|
+
}
|
|
1776
|
+
// Build the config for the init function with proper structure
|
|
1777
|
+
const initConfig = {
|
|
1778
|
+
publishableKey: config.publishableKey
|
|
1779
|
+
};
|
|
1780
|
+
// Add apiHost if provided
|
|
1781
|
+
if (config.apiHost) {
|
|
1782
|
+
initConfig.apiHost = config.apiHost;
|
|
1783
|
+
}
|
|
1784
|
+
// Add options as nested object if provided
|
|
1785
|
+
if (config.options && typeof config.options === 'object') {
|
|
1786
|
+
initConfig.options = config.options;
|
|
1787
|
+
}
|
|
1788
|
+
// Create the instance using the merged config
|
|
1789
|
+
const instance = init(initConfig);
|
|
1790
|
+
if (instanceName) {
|
|
1791
|
+
// Multi-instance: store as journium[instanceName]
|
|
1792
|
+
globalJournium[instanceName] = instance;
|
|
1793
|
+
return instance;
|
|
1794
|
+
}
|
|
1795
|
+
else {
|
|
1796
|
+
// Single instance: dynamically bind all methods
|
|
1797
|
+
defaultInstance = instance;
|
|
1798
|
+
bindInstanceMethods(instance, globalJournium);
|
|
1799
|
+
return instance;
|
|
1800
|
+
}
|
|
1801
|
+
}
|
|
1802
|
+
catch (error) {
|
|
1803
|
+
console.error('Journium: Failed to initialize:', error);
|
|
1804
|
+
// Return fallback object that queues method calls
|
|
1805
|
+
const fallback = createFallbackObject(error);
|
|
1806
|
+
Object.assign(globalJournium, fallback);
|
|
1807
|
+
return {};
|
|
1808
|
+
}
|
|
1809
|
+
}
|
|
1810
|
+
};
|
|
1811
|
+
return globalJournium;
|
|
1812
|
+
}
|
|
1813
|
+
/**
|
|
1814
|
+
* Initialize from snippet with comprehensive error handling and retry logic
|
|
1815
|
+
*/
|
|
1816
|
+
function initializeFromSnippet() {
|
|
1817
|
+
if (typeof window === 'undefined') {
|
|
1818
|
+
return;
|
|
1819
|
+
}
|
|
1820
|
+
const snippet = window.journium;
|
|
1821
|
+
let globalJournium = createGlobalJournium();
|
|
1822
|
+
try {
|
|
1823
|
+
if (snippet && snippet._i) {
|
|
1824
|
+
// Extract snippet initialization parameters
|
|
1825
|
+
const [config, instanceName] = snippet._i;
|
|
1826
|
+
const queuedCalls = snippet._q || [];
|
|
1827
|
+
// Validate config object structure
|
|
1828
|
+
if (!config || typeof config !== 'object') {
|
|
1829
|
+
throw new Error('Invalid or missing config object');
|
|
1830
|
+
}
|
|
1831
|
+
if (!config.publishableKey) {
|
|
1832
|
+
throw new Error('publishableKey is required in config');
|
|
1833
|
+
}
|
|
1834
|
+
// Initialize the instance with timeout protection
|
|
1835
|
+
const instance = globalJournium.init(config, instanceName);
|
|
1836
|
+
// Process queued method calls
|
|
1837
|
+
if (queuedCalls.length > 0) {
|
|
1838
|
+
// Always use the instance for processing queued calls
|
|
1839
|
+
setTimeout(() => processQueuedCalls(instance, queuedCalls), 0);
|
|
1840
|
+
}
|
|
1841
|
+
// Preserve snippet metadata
|
|
1842
|
+
globalJournium.__JV = snippet.__JV;
|
|
1843
|
+
globalJournium._q = [];
|
|
1844
|
+
globalJournium._i = snippet._i;
|
|
1845
|
+
globalJournium._error = null;
|
|
1846
|
+
}
|
|
1847
|
+
else if (snippet && snippet._q) {
|
|
1848
|
+
// Handle case where queue exists but no initialization
|
|
1849
|
+
console.warn('Journium: Found queued calls but no initialization data');
|
|
1850
|
+
globalJournium._q = snippet._q;
|
|
1851
|
+
}
|
|
1852
|
+
}
|
|
1853
|
+
catch (error) {
|
|
1854
|
+
console.error('Journium: Critical initialization failure:', error);
|
|
1855
|
+
// Create fallback with error tracking
|
|
1856
|
+
globalJournium = createFallbackObject(error);
|
|
1857
|
+
// Preserve original queue for potential retry
|
|
1858
|
+
if (snippet && snippet._q) {
|
|
1859
|
+
globalJournium._q = snippet._q;
|
|
1860
|
+
}
|
|
1861
|
+
// Enable retry mechanism
|
|
1862
|
+
globalJournium._retry = true;
|
|
1863
|
+
}
|
|
1864
|
+
finally {
|
|
1865
|
+
// Always replace window.journium, even in failure cases
|
|
1866
|
+
window.journium = globalJournium;
|
|
1867
|
+
// Set up retry mechanism if needed
|
|
1868
|
+
if (globalJournium._retry && !globalJournium._error) {
|
|
1869
|
+
setTimeout(() => {
|
|
1870
|
+
if (window.journium && window.journium._retry) {
|
|
1871
|
+
console.log('Journium: Attempting retry...');
|
|
1872
|
+
initializeFromSnippet();
|
|
1873
|
+
}
|
|
1874
|
+
}, 2000);
|
|
1875
|
+
}
|
|
1876
|
+
}
|
|
1877
|
+
}
|
|
1878
|
+
// Auto-initialize when script loads
|
|
1879
|
+
if (typeof window !== 'undefined') {
|
|
1880
|
+
initializeFromSnippet();
|
|
1881
|
+
}
|
|
1882
|
+
var cdn = { init };
|
|
1883
|
+
|
|
1884
|
+
exports.default = cdn;
|
|
1885
|
+
exports.init = init;
|
|
1886
|
+
|
|
1887
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
1888
|
+
|
|
1889
|
+
return exports;
|
|
1890
|
+
|
|
1891
|
+
})({});
|
|
1892
|
+
//# sourceMappingURL=journium.js.map
|