@journium/js 1.0.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.
@@ -0,0 +1,1431 @@
1
+ (function (global, factory) {
2
+ typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
3
+ typeof define === 'function' && define.amd ? define(['exports'], factory) :
4
+ (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.Journium = {}));
5
+ })(this, (function (exports) { 'use strict';
6
+
7
+ /**
8
+ * uuidv7: A JavaScript implementation of UUID version 7
9
+ *
10
+ * Copyright 2021-2024 LiosK
11
+ *
12
+ * @license Apache-2.0
13
+ * @packageDocumentation
14
+ */
15
+ const DIGITS = "0123456789abcdef";
16
+ /** Represents a UUID as a 16-byte byte array. */
17
+ class UUID {
18
+ /** @param bytes - The 16-byte byte array representation. */
19
+ constructor(bytes) {
20
+ this.bytes = bytes;
21
+ }
22
+ /**
23
+ * Creates an object from the internal representation, a 16-byte byte array
24
+ * containing the binary UUID representation in the big-endian byte order.
25
+ *
26
+ * This method does NOT shallow-copy the argument, and thus the created object
27
+ * holds the reference to the underlying buffer.
28
+ *
29
+ * @throws TypeError if the length of the argument is not 16.
30
+ */
31
+ static ofInner(bytes) {
32
+ if (bytes.length !== 16) {
33
+ throw new TypeError("not 128-bit length");
34
+ }
35
+ else {
36
+ return new UUID(bytes);
37
+ }
38
+ }
39
+ /**
40
+ * Builds a byte array from UUIDv7 field values.
41
+ *
42
+ * @param unixTsMs - A 48-bit `unix_ts_ms` field value.
43
+ * @param randA - A 12-bit `rand_a` field value.
44
+ * @param randBHi - The higher 30 bits of 62-bit `rand_b` field value.
45
+ * @param randBLo - The lower 32 bits of 62-bit `rand_b` field value.
46
+ * @throws RangeError if any field value is out of the specified range.
47
+ */
48
+ static fromFieldsV7(unixTsMs, randA, randBHi, randBLo) {
49
+ if (!Number.isInteger(unixTsMs) ||
50
+ !Number.isInteger(randA) ||
51
+ !Number.isInteger(randBHi) ||
52
+ !Number.isInteger(randBLo) ||
53
+ unixTsMs < 0 ||
54
+ randA < 0 ||
55
+ randBHi < 0 ||
56
+ randBLo < 0 ||
57
+ unixTsMs > 281474976710655 ||
58
+ randA > 0xfff ||
59
+ randBHi > 1073741823 ||
60
+ randBLo > 4294967295) {
61
+ throw new RangeError("invalid field value");
62
+ }
63
+ const bytes = new Uint8Array(16);
64
+ bytes[0] = unixTsMs / 2 ** 40;
65
+ bytes[1] = unixTsMs / 2 ** 32;
66
+ bytes[2] = unixTsMs / 2 ** 24;
67
+ bytes[3] = unixTsMs / 2 ** 16;
68
+ bytes[4] = unixTsMs / 2 ** 8;
69
+ bytes[5] = unixTsMs;
70
+ bytes[6] = 0x70 | (randA >>> 8);
71
+ bytes[7] = randA;
72
+ bytes[8] = 0x80 | (randBHi >>> 24);
73
+ bytes[9] = randBHi >>> 16;
74
+ bytes[10] = randBHi >>> 8;
75
+ bytes[11] = randBHi;
76
+ bytes[12] = randBLo >>> 24;
77
+ bytes[13] = randBLo >>> 16;
78
+ bytes[14] = randBLo >>> 8;
79
+ bytes[15] = randBLo;
80
+ return new UUID(bytes);
81
+ }
82
+ /**
83
+ * Builds a byte array from a string representation.
84
+ *
85
+ * This method accepts the following formats:
86
+ *
87
+ * - 32-digit hexadecimal format without hyphens: `0189dcd553117d408db09496a2eef37b`
88
+ * - 8-4-4-4-12 hyphenated format: `0189dcd5-5311-7d40-8db0-9496a2eef37b`
89
+ * - Hyphenated format with surrounding braces: `{0189dcd5-5311-7d40-8db0-9496a2eef37b}`
90
+ * - RFC 9562 URN format: `urn:uuid:0189dcd5-5311-7d40-8db0-9496a2eef37b`
91
+ *
92
+ * Leading and trailing whitespaces represents an error.
93
+ *
94
+ * @throws SyntaxError if the argument could not parse as a valid UUID string.
95
+ */
96
+ static parse(uuid) {
97
+ var _a, _b, _c, _d;
98
+ let hex = undefined;
99
+ switch (uuid.length) {
100
+ case 32:
101
+ hex = (_a = /^[0-9a-f]{32}$/i.exec(uuid)) === null || _a === void 0 ? void 0 : _a[0];
102
+ break;
103
+ case 36:
104
+ hex =
105
+ (_b = /^([0-9a-f]{8})-([0-9a-f]{4})-([0-9a-f]{4})-([0-9a-f]{4})-([0-9a-f]{12})$/i
106
+ .exec(uuid)) === null || _b === void 0 ? void 0 : _b.slice(1, 6).join("");
107
+ break;
108
+ case 38:
109
+ hex =
110
+ (_c = /^\{([0-9a-f]{8})-([0-9a-f]{4})-([0-9a-f]{4})-([0-9a-f]{4})-([0-9a-f]{12})\}$/i
111
+ .exec(uuid)) === null || _c === void 0 ? void 0 : _c.slice(1, 6).join("");
112
+ break;
113
+ case 45:
114
+ hex =
115
+ (_d = /^urn:uuid:([0-9a-f]{8})-([0-9a-f]{4})-([0-9a-f]{4})-([0-9a-f]{4})-([0-9a-f]{12})$/i
116
+ .exec(uuid)) === null || _d === void 0 ? void 0 : _d.slice(1, 6).join("");
117
+ break;
118
+ }
119
+ if (hex) {
120
+ const inner = new Uint8Array(16);
121
+ for (let i = 0; i < 16; i += 4) {
122
+ const n = parseInt(hex.substring(2 * i, 2 * i + 8), 16);
123
+ inner[i + 0] = n >>> 24;
124
+ inner[i + 1] = n >>> 16;
125
+ inner[i + 2] = n >>> 8;
126
+ inner[i + 3] = n;
127
+ }
128
+ return new UUID(inner);
129
+ }
130
+ else {
131
+ throw new SyntaxError("could not parse UUID string");
132
+ }
133
+ }
134
+ /**
135
+ * @returns The 8-4-4-4-12 canonical hexadecimal string representation
136
+ * (`0189dcd5-5311-7d40-8db0-9496a2eef37b`).
137
+ */
138
+ toString() {
139
+ let text = "";
140
+ for (let i = 0; i < this.bytes.length; i++) {
141
+ text += DIGITS.charAt(this.bytes[i] >>> 4);
142
+ text += DIGITS.charAt(this.bytes[i] & 0xf);
143
+ if (i === 3 || i === 5 || i === 7 || i === 9) {
144
+ text += "-";
145
+ }
146
+ }
147
+ return text;
148
+ }
149
+ /**
150
+ * @returns The 32-digit hexadecimal representation without hyphens
151
+ * (`0189dcd553117d408db09496a2eef37b`).
152
+ */
153
+ toHex() {
154
+ let text = "";
155
+ for (let i = 0; i < this.bytes.length; i++) {
156
+ text += DIGITS.charAt(this.bytes[i] >>> 4);
157
+ text += DIGITS.charAt(this.bytes[i] & 0xf);
158
+ }
159
+ return text;
160
+ }
161
+ /** @returns The 8-4-4-4-12 canonical hexadecimal string representation. */
162
+ toJSON() {
163
+ return this.toString();
164
+ }
165
+ /**
166
+ * Reports the variant field value of the UUID or, if appropriate, "NIL" or
167
+ * "MAX".
168
+ *
169
+ * For convenience, this method reports "NIL" or "MAX" if `this` represents
170
+ * the Nil or Max UUID, although the Nil and Max UUIDs are technically
171
+ * subsumed under the variants `0b0` and `0b111`, respectively.
172
+ */
173
+ getVariant() {
174
+ const n = this.bytes[8] >>> 4;
175
+ if (n < 0) {
176
+ throw new Error("unreachable");
177
+ }
178
+ else if (n <= 0b0111) {
179
+ return this.bytes.every((e) => e === 0) ? "NIL" : "VAR_0";
180
+ }
181
+ else if (n <= 0b1011) {
182
+ return "VAR_10";
183
+ }
184
+ else if (n <= 0b1101) {
185
+ return "VAR_110";
186
+ }
187
+ else if (n <= 0b1111) {
188
+ return this.bytes.every((e) => e === 0xff) ? "MAX" : "VAR_RESERVED";
189
+ }
190
+ else {
191
+ throw new Error("unreachable");
192
+ }
193
+ }
194
+ /**
195
+ * Returns the version field value of the UUID or `undefined` if the UUID does
196
+ * not have the variant field value of `0b10`.
197
+ */
198
+ getVersion() {
199
+ return this.getVariant() === "VAR_10" ? this.bytes[6] >>> 4 : undefined;
200
+ }
201
+ /** Creates an object from `this`. */
202
+ clone() {
203
+ return new UUID(this.bytes.slice(0));
204
+ }
205
+ /** Returns true if `this` is equivalent to `other`. */
206
+ equals(other) {
207
+ return this.compareTo(other) === 0;
208
+ }
209
+ /**
210
+ * Returns a negative integer, zero, or positive integer if `this` is less
211
+ * than, equal to, or greater than `other`, respectively.
212
+ */
213
+ compareTo(other) {
214
+ for (let i = 0; i < 16; i++) {
215
+ const diff = this.bytes[i] - other.bytes[i];
216
+ if (diff !== 0) {
217
+ return Math.sign(diff);
218
+ }
219
+ }
220
+ return 0;
221
+ }
222
+ }
223
+ /**
224
+ * Encapsulates the monotonic counter state.
225
+ *
226
+ * This class provides APIs to utilize a separate counter state from that of the
227
+ * global generator used by {@link uuidv7} and {@link uuidv7obj}. In addition to
228
+ * the default {@link generate} method, this class has {@link generateOrAbort}
229
+ * that is useful to absolutely guarantee the monotonically increasing order of
230
+ * generated UUIDs. See their respective documentation for details.
231
+ */
232
+ class V7Generator {
233
+ /**
234
+ * Creates a generator object with the default random number generator, or
235
+ * with the specified one if passed as an argument. The specified random
236
+ * number generator should be cryptographically strong and securely seeded.
237
+ */
238
+ constructor(randomNumberGenerator) {
239
+ this.timestamp = 0;
240
+ this.counter = 0;
241
+ this.random = randomNumberGenerator !== null && randomNumberGenerator !== void 0 ? randomNumberGenerator : getDefaultRandom();
242
+ }
243
+ /**
244
+ * Generates a new UUIDv7 object from the current timestamp, or resets the
245
+ * generator upon significant timestamp rollback.
246
+ *
247
+ * This method returns a monotonically increasing UUID by reusing the previous
248
+ * timestamp even if the up-to-date timestamp is smaller than the immediately
249
+ * preceding UUID's. However, when such a clock rollback is considered
250
+ * significant (i.e., by more than ten seconds), this method resets the
251
+ * generator and returns a new UUID based on the given timestamp, breaking the
252
+ * increasing order of UUIDs.
253
+ *
254
+ * See {@link generateOrAbort} for the other mode of generation and
255
+ * {@link generateOrResetCore} for the low-level primitive.
256
+ */
257
+ generate() {
258
+ return this.generateOrResetCore(Date.now(), 10000);
259
+ }
260
+ /**
261
+ * Generates a new UUIDv7 object from the current timestamp, or returns
262
+ * `undefined` upon significant timestamp rollback.
263
+ *
264
+ * This method returns a monotonically increasing UUID by reusing the previous
265
+ * timestamp even if the up-to-date timestamp is smaller than the immediately
266
+ * preceding UUID's. However, when such a clock rollback is considered
267
+ * significant (i.e., by more than ten seconds), this method aborts and
268
+ * returns `undefined` immediately.
269
+ *
270
+ * See {@link generate} for the other mode of generation and
271
+ * {@link generateOrAbortCore} for the low-level primitive.
272
+ */
273
+ generateOrAbort() {
274
+ return this.generateOrAbortCore(Date.now(), 10000);
275
+ }
276
+ /**
277
+ * Generates a new UUIDv7 object from the `unixTsMs` passed, or resets the
278
+ * generator upon significant timestamp rollback.
279
+ *
280
+ * This method is equivalent to {@link generate} except that it takes a custom
281
+ * timestamp and clock rollback allowance.
282
+ *
283
+ * @param rollbackAllowance - The amount of `unixTsMs` rollback that is
284
+ * considered significant. A suggested value is `10_000` (milliseconds).
285
+ * @throws RangeError if `unixTsMs` is not a 48-bit positive integer.
286
+ */
287
+ generateOrResetCore(unixTsMs, rollbackAllowance) {
288
+ let value = this.generateOrAbortCore(unixTsMs, rollbackAllowance);
289
+ if (value === undefined) {
290
+ // reset state and resume
291
+ this.timestamp = 0;
292
+ value = this.generateOrAbortCore(unixTsMs, rollbackAllowance);
293
+ }
294
+ return value;
295
+ }
296
+ /**
297
+ * Generates a new UUIDv7 object from the `unixTsMs` passed, or returns
298
+ * `undefined` upon significant timestamp rollback.
299
+ *
300
+ * This method is equivalent to {@link generateOrAbort} except that it takes a
301
+ * custom timestamp and clock rollback allowance.
302
+ *
303
+ * @param rollbackAllowance - The amount of `unixTsMs` rollback that is
304
+ * considered significant. A suggested value is `10_000` (milliseconds).
305
+ * @throws RangeError if `unixTsMs` is not a 48-bit positive integer.
306
+ */
307
+ generateOrAbortCore(unixTsMs, rollbackAllowance) {
308
+ const MAX_COUNTER = 4398046511103;
309
+ if (!Number.isInteger(unixTsMs) ||
310
+ unixTsMs < 1 ||
311
+ unixTsMs > 281474976710655) {
312
+ throw new RangeError("`unixTsMs` must be a 48-bit positive integer");
313
+ }
314
+ else if (rollbackAllowance < 0 || rollbackAllowance > 281474976710655) {
315
+ throw new RangeError("`rollbackAllowance` out of reasonable range");
316
+ }
317
+ if (unixTsMs > this.timestamp) {
318
+ this.timestamp = unixTsMs;
319
+ this.resetCounter();
320
+ }
321
+ else if (unixTsMs + rollbackAllowance >= this.timestamp) {
322
+ // go on with previous timestamp if new one is not much smaller
323
+ this.counter++;
324
+ if (this.counter > MAX_COUNTER) {
325
+ // increment timestamp at counter overflow
326
+ this.timestamp++;
327
+ this.resetCounter();
328
+ }
329
+ }
330
+ else {
331
+ // abort if clock went backwards to unbearable extent
332
+ return undefined;
333
+ }
334
+ return UUID.fromFieldsV7(this.timestamp, Math.trunc(this.counter / 2 ** 30), this.counter & (2 ** 30 - 1), this.random.nextUint32());
335
+ }
336
+ /** Initializes the counter at a 42-bit random integer. */
337
+ resetCounter() {
338
+ this.counter =
339
+ this.random.nextUint32() * 0x400 + (this.random.nextUint32() & 0x3ff);
340
+ }
341
+ /**
342
+ * Generates a new UUIDv4 object utilizing the random number generator inside.
343
+ *
344
+ * @internal
345
+ */
346
+ generateV4() {
347
+ const bytes = new Uint8Array(Uint32Array.of(this.random.nextUint32(), this.random.nextUint32(), this.random.nextUint32(), this.random.nextUint32()).buffer);
348
+ bytes[6] = 0x40 | (bytes[6] >>> 4);
349
+ bytes[8] = 0x80 | (bytes[8] >>> 2);
350
+ return UUID.ofInner(bytes);
351
+ }
352
+ }
353
+ /** Returns the default random number generator available in the environment. */
354
+ const getDefaultRandom = () => {
355
+ // detect Web Crypto API
356
+ if (typeof crypto !== "undefined" &&
357
+ typeof crypto.getRandomValues !== "undefined") {
358
+ return new BufferedCryptoRandom();
359
+ }
360
+ else {
361
+ // fall back on Math.random() unless the flag is set to true
362
+ if (typeof UUIDV7_DENY_WEAK_RNG !== "undefined" && UUIDV7_DENY_WEAK_RNG) {
363
+ throw new Error("no cryptographically strong RNG available");
364
+ }
365
+ return {
366
+ nextUint32: () => Math.trunc(Math.random() * 65536) * 65536 +
367
+ Math.trunc(Math.random() * 65536),
368
+ };
369
+ }
370
+ };
371
+ /**
372
+ * Wraps `crypto.getRandomValues()` to enable buffering; this uses a small
373
+ * buffer by default to avoid both unbearable throughput decline in some
374
+ * environments and the waste of time and space for unused values.
375
+ */
376
+ class BufferedCryptoRandom {
377
+ constructor() {
378
+ this.buffer = new Uint32Array(8);
379
+ this.cursor = 0xffff;
380
+ }
381
+ nextUint32() {
382
+ if (this.cursor >= this.buffer.length) {
383
+ crypto.getRandomValues(this.buffer);
384
+ this.cursor = 0;
385
+ }
386
+ return this.buffer[this.cursor++];
387
+ }
388
+ }
389
+ let defaultGenerator;
390
+ /**
391
+ * Generates a UUIDv7 string.
392
+ *
393
+ * @returns The 8-4-4-4-12 canonical hexadecimal string representation
394
+ * ("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx").
395
+ */
396
+ const uuidv7 = () => uuidv7obj().toString();
397
+ /** Generates a UUIDv7 object. */
398
+ const uuidv7obj = () => (defaultGenerator || (defaultGenerator = new V7Generator())).generate();
399
+
400
+ const generateId = () => {
401
+ return Math.random().toString(36).substring(2) + Date.now().toString(36);
402
+ };
403
+ const generateUuidv7 = () => {
404
+ return uuidv7();
405
+ };
406
+ const getCurrentTimestamp = () => {
407
+ return new Date().toISOString();
408
+ };
409
+ const getCurrentUrl = () => {
410
+ if (typeof window !== 'undefined') {
411
+ return window.location.href;
412
+ }
413
+ return '';
414
+ };
415
+ const getPageTitle = () => {
416
+ if (typeof document !== 'undefined') {
417
+ return document.title;
418
+ }
419
+ return '';
420
+ };
421
+ const getReferrer = () => {
422
+ if (typeof document !== 'undefined') {
423
+ return document.referrer;
424
+ }
425
+ return '';
426
+ };
427
+ const isBrowser = () => {
428
+ return typeof window !== 'undefined';
429
+ };
430
+ const isNode = () => {
431
+ var _a;
432
+ return typeof process !== 'undefined' && !!((_a = process.versions) === null || _a === void 0 ? void 0 : _a.node);
433
+ };
434
+ const fetchRemoteConfig = async (apiHost, token, fetchFn) => {
435
+ const endpoint = '/v1/configs';
436
+ const url = `${apiHost}${endpoint}?ingestion_key=${encodeURIComponent(token)}`;
437
+ try {
438
+ let fetch = fetchFn;
439
+ if (!fetch) {
440
+ if (isNode()) {
441
+ // For Node.js environments, expect fetch to be passed in
442
+ throw new Error('Fetch function must be provided in Node.js environment');
443
+ }
444
+ else {
445
+ // Use native fetch in browser
446
+ fetch = window.fetch;
447
+ }
448
+ }
449
+ const response = await fetch(url, {
450
+ method: 'GET',
451
+ headers: {
452
+ 'Content-Type': 'application/json',
453
+ },
454
+ });
455
+ if (!response.ok) {
456
+ throw new Error(`Config fetch failed: ${response.status} ${response.statusText}`);
457
+ }
458
+ const data = await response.json();
459
+ return data;
460
+ }
461
+ catch (error) {
462
+ console.warn('Failed to fetch remote config:', error);
463
+ return null;
464
+ }
465
+ };
466
+ const mergeConfigs = (localConfig, remoteConfig) => {
467
+ if (!remoteConfig) {
468
+ return localConfig;
469
+ }
470
+ // Deep merge remote config into local config
471
+ // Remote config takes precedence over local config
472
+ const merged = { ...localConfig };
473
+ // Handle primitive values
474
+ Object.keys(remoteConfig).forEach(key => {
475
+ if (remoteConfig[key] !== undefined && remoteConfig[key] !== null) {
476
+ if (typeof remoteConfig[key] === 'object' && !Array.isArray(remoteConfig[key])) {
477
+ // Deep merge objects
478
+ merged[key] = {
479
+ ...(merged[key] || {}),
480
+ ...remoteConfig[key]
481
+ };
482
+ }
483
+ else {
484
+ // Override primitive values and arrays
485
+ merged[key] = remoteConfig[key];
486
+ }
487
+ }
488
+ });
489
+ return merged;
490
+ };
491
+
492
+ const DEFAULT_SESSION_TIMEOUT = 30 * 60 * 1000; // 30 minutes in ms
493
+ class BrowserIdentityManager {
494
+ constructor(sessionTimeout, token) {
495
+ this.identity = null;
496
+ this.sessionTimeout = DEFAULT_SESSION_TIMEOUT;
497
+ if (sessionTimeout) {
498
+ this.sessionTimeout = sessionTimeout;
499
+ }
500
+ // Generate storage key with token pattern: jrnm_<token>_journium
501
+ this.storageKey = token ? `jrnm_${token}_journium` : '__journium_identity';
502
+ this.loadOrCreateIdentity();
503
+ }
504
+ loadOrCreateIdentity() {
505
+ if (!this.isBrowser())
506
+ return;
507
+ try {
508
+ const stored = localStorage.getItem(this.storageKey);
509
+ if (stored) {
510
+ const parsedIdentity = JSON.parse(stored);
511
+ // Check if session is expired
512
+ const now = Date.now();
513
+ const sessionAge = now - parsedIdentity.session_timestamp;
514
+ if (sessionAge > this.sessionTimeout) {
515
+ // Session expired, create new session but keep device and distinct IDs
516
+ this.identity = {
517
+ distinct_id: parsedIdentity.distinct_id,
518
+ $device_id: parsedIdentity.$device_id,
519
+ $session_id: generateUuidv7(),
520
+ session_timestamp: now,
521
+ $user_state: parsedIdentity.$user_state || 'anonymous',
522
+ };
523
+ }
524
+ else {
525
+ // Session still valid
526
+ this.identity = parsedIdentity;
527
+ // Ensure $user_state exists for backward compatibility
528
+ if (!this.identity.$user_state) {
529
+ this.identity.$user_state = 'anonymous';
530
+ }
531
+ }
532
+ }
533
+ else {
534
+ // First time, create all new IDs
535
+ const newId = generateUuidv7();
536
+ this.identity = {
537
+ distinct_id: newId,
538
+ $device_id: newId,
539
+ $session_id: newId,
540
+ session_timestamp: Date.now(),
541
+ $user_state: 'anonymous',
542
+ };
543
+ }
544
+ // Save to localStorage
545
+ this.saveIdentity();
546
+ }
547
+ catch (error) {
548
+ console.warn('Journium: Failed to load/create identity:', error);
549
+ // Fallback: create temporary identity without localStorage
550
+ const newId = generateUuidv7();
551
+ this.identity = {
552
+ distinct_id: newId,
553
+ $device_id: newId,
554
+ $session_id: newId,
555
+ session_timestamp: Date.now(),
556
+ $user_state: 'anonymous',
557
+ };
558
+ }
559
+ }
560
+ saveIdentity() {
561
+ if (!this.isBrowser() || !this.identity)
562
+ return;
563
+ try {
564
+ localStorage.setItem(this.storageKey, JSON.stringify(this.identity));
565
+ }
566
+ catch (error) {
567
+ console.warn('Journium: Failed to save identity to localStorage:', error);
568
+ }
569
+ }
570
+ isBrowser() {
571
+ return typeof window !== 'undefined' && typeof localStorage !== 'undefined';
572
+ }
573
+ getIdentity() {
574
+ return this.identity;
575
+ }
576
+ updateSessionTimeout(timeoutMs) {
577
+ this.sessionTimeout = timeoutMs;
578
+ }
579
+ refreshSession() {
580
+ if (!this.identity)
581
+ return;
582
+ this.identity = {
583
+ ...this.identity,
584
+ $session_id: generateUuidv7(),
585
+ session_timestamp: Date.now(),
586
+ };
587
+ this.saveIdentity();
588
+ }
589
+ identify(distinctId, attributes = {}) {
590
+ if (!this.identity)
591
+ return { previousDistinctId: null };
592
+ const previousDistinctId = this.identity.distinct_id;
593
+ // Update the distinct ID and mark user as identified
594
+ this.identity = {
595
+ ...this.identity,
596
+ distinct_id: distinctId,
597
+ $user_state: 'identified',
598
+ };
599
+ this.saveIdentity();
600
+ return { previousDistinctId };
601
+ }
602
+ reset() {
603
+ if (!this.identity)
604
+ return;
605
+ // Generate new distinct ID but keep device ID
606
+ this.identity = {
607
+ ...this.identity,
608
+ distinct_id: generateUuidv7(),
609
+ $user_state: 'anonymous',
610
+ };
611
+ this.saveIdentity();
612
+ }
613
+ getUserAgentInfo() {
614
+ if (!this.isBrowser()) {
615
+ return {
616
+ $raw_user_agent: '',
617
+ $browser: 'Unknown',
618
+ $os: 'Unknown',
619
+ $device_type: 'Unknown',
620
+ };
621
+ }
622
+ const userAgent = navigator.userAgent;
623
+ return {
624
+ $raw_user_agent: userAgent,
625
+ $browser: this.parseBrowser(userAgent),
626
+ $os: this.parseOS(userAgent),
627
+ $device_type: this.parseDeviceType(userAgent),
628
+ };
629
+ }
630
+ parseBrowser(userAgent) {
631
+ if (userAgent.includes('Chrome') && !userAgent.includes('Edg'))
632
+ return 'Chrome';
633
+ if (userAgent.includes('Firefox'))
634
+ return 'Firefox';
635
+ if (userAgent.includes('Safari') && !userAgent.includes('Chrome'))
636
+ return 'Safari';
637
+ if (userAgent.includes('Edg'))
638
+ return 'Edge';
639
+ if (userAgent.includes('Opera') || userAgent.includes('OPR'))
640
+ return 'Opera';
641
+ return 'Unknown';
642
+ }
643
+ parseOS(userAgent) {
644
+ if (userAgent.includes('Windows'))
645
+ return 'Windows';
646
+ if (userAgent.includes('Macintosh') || userAgent.includes('Mac OS'))
647
+ return 'Mac OS';
648
+ if (userAgent.includes('Linux'))
649
+ return 'Linux';
650
+ if (userAgent.includes('Android'))
651
+ return 'Android';
652
+ if (userAgent.includes('iPhone') || userAgent.includes('iPad'))
653
+ return 'iOS';
654
+ return 'Unknown';
655
+ }
656
+ parseDeviceType(userAgent) {
657
+ if (userAgent.includes('Mobile') || userAgent.includes('Android') || userAgent.includes('iPhone')) {
658
+ return 'Mobile';
659
+ }
660
+ if (userAgent.includes('iPad') || userAgent.includes('Tablet')) {
661
+ return 'Tablet';
662
+ }
663
+ return 'Desktop';
664
+ }
665
+ }
666
+
667
+ class JourniumClient {
668
+ constructor(config) {
669
+ this.queue = [];
670
+ this.flushTimer = null;
671
+ this.initialized = false;
672
+ // Validate required configuration
673
+ if (!config.token) {
674
+ console.error('Journium: token is required but not provided. SDK will not function.');
675
+ return;
676
+ }
677
+ if (!config.apiHost) {
678
+ console.error('Journium: apiHost is required but not provided. SDK will not function.');
679
+ return;
680
+ }
681
+ this.config = config;
682
+ // Generate storage key for config caching
683
+ this.configStorageKey = `jrnm_${config.token}_config`;
684
+ // Initialize identity manager
685
+ this.identityManager = new BrowserIdentityManager(this.config.sessionTimeout, this.config.token);
686
+ // Initialize synchronously with cached config, fetch fresh config in background
687
+ this.initializeSync();
688
+ this.fetchRemoteConfigAsync();
689
+ }
690
+ loadCachedConfig() {
691
+ if (typeof window === 'undefined' || !window.localStorage) {
692
+ return null;
693
+ }
694
+ try {
695
+ const cached = window.localStorage.getItem(this.configStorageKey);
696
+ return cached ? JSON.parse(cached) : null;
697
+ }
698
+ catch (error) {
699
+ if (this.config.debug) {
700
+ console.warn('Journium: Failed to load cached config:', error);
701
+ }
702
+ return null;
703
+ }
704
+ }
705
+ saveCachedConfig(config) {
706
+ if (typeof window === 'undefined' || !window.localStorage) {
707
+ return;
708
+ }
709
+ try {
710
+ window.localStorage.setItem(this.configStorageKey, JSON.stringify(config));
711
+ }
712
+ catch (error) {
713
+ if (this.config.debug) {
714
+ console.warn('Journium: Failed to save config to cache:', error);
715
+ }
716
+ }
717
+ }
718
+ initializeSync() {
719
+ var _a, _b, _c;
720
+ // Step 1: Load cached config from localStorage (synchronous)
721
+ const cachedConfig = this.loadCachedConfig();
722
+ // Step 2: Apply cached config immediately, or use defaults
723
+ const localOnlyConfig = {
724
+ apiHost: this.config.apiHost,
725
+ token: this.config.token,
726
+ };
727
+ if (cachedConfig) {
728
+ // Use cached remote config
729
+ this.config = {
730
+ ...localOnlyConfig,
731
+ ...cachedConfig,
732
+ };
733
+ if (this.config.debug) {
734
+ console.log('Journium: Using cached configuration:', cachedConfig);
735
+ }
736
+ }
737
+ else {
738
+ // Use defaults for first-time initialization
739
+ this.config = {
740
+ ...this.config,
741
+ debug: (_a = this.config.debug) !== null && _a !== void 0 ? _a : false,
742
+ flushAt: (_b = this.config.flushAt) !== null && _b !== void 0 ? _b : 20,
743
+ flushInterval: (_c = this.config.flushInterval) !== null && _c !== void 0 ? _c : 10000,
744
+ };
745
+ if (this.config.debug) {
746
+ console.log('Journium: No cached config found, using defaults');
747
+ }
748
+ }
749
+ // Update session timeout from config
750
+ if (this.config.sessionTimeout) {
751
+ this.identityManager.updateSessionTimeout(this.config.sessionTimeout);
752
+ }
753
+ // Step 3: Mark as initialized immediately - no need to wait for remote fetch
754
+ this.initialized = true;
755
+ // Step 4: Start flush timer immediately
756
+ if (this.config.flushInterval && this.config.flushInterval > 0) {
757
+ this.startFlushTimer();
758
+ }
759
+ if (this.config.debug) {
760
+ console.log('Journium: Client initialized and ready to track events');
761
+ }
762
+ }
763
+ async fetchRemoteConfigAsync() {
764
+ // Fetch fresh config in background
765
+ if (this.config.token) {
766
+ await this.fetchAndCacheRemoteConfig();
767
+ }
768
+ }
769
+ async fetchAndCacheRemoteConfig() {
770
+ try {
771
+ if (this.config.debug) {
772
+ console.log('Journium: Fetching remote configuration in background...');
773
+ }
774
+ const remoteConfigResponse = await fetchRemoteConfig(this.config.apiHost, this.config.token);
775
+ if (remoteConfigResponse && remoteConfigResponse.success) {
776
+ // Save to cache for next session
777
+ this.saveCachedConfig(remoteConfigResponse.config);
778
+ // Apply fresh config to current session
779
+ const localOnlyConfig = {
780
+ apiHost: this.config.apiHost,
781
+ token: this.config.token,
782
+ };
783
+ this.config = {
784
+ ...localOnlyConfig,
785
+ ...remoteConfigResponse.config,
786
+ };
787
+ // Update session timeout if provided in fresh config
788
+ if (remoteConfigResponse.config.sessionTimeout) {
789
+ this.identityManager.updateSessionTimeout(remoteConfigResponse.config.sessionTimeout);
790
+ }
791
+ if (this.config.debug) {
792
+ console.log('Journium: Background remote configuration applied:', remoteConfigResponse.config);
793
+ }
794
+ }
795
+ }
796
+ catch (error) {
797
+ if (this.config.debug) {
798
+ console.warn('Journium: Background remote config fetch failed:', error);
799
+ }
800
+ }
801
+ }
802
+ startFlushTimer() {
803
+ if (this.flushTimer) {
804
+ clearInterval(this.flushTimer);
805
+ }
806
+ // Use universal setInterval (works in both browser and Node.js)
807
+ this.flushTimer = setInterval(() => {
808
+ this.flush();
809
+ }, this.config.flushInterval);
810
+ }
811
+ async sendEvents(events) {
812
+ if (!events.length)
813
+ return;
814
+ try {
815
+ const response = await fetch(`${this.config.apiHost}/v1/ingest_event`, {
816
+ method: 'POST',
817
+ headers: {
818
+ 'Content-Type': 'application/json',
819
+ 'Authorization': `Bearer ${this.config.token}`,
820
+ },
821
+ body: JSON.stringify({
822
+ events,
823
+ }),
824
+ });
825
+ if (!response.ok) {
826
+ throw new Error(`HTTP ${response.status}: ${response.statusText}`);
827
+ }
828
+ if (this.config.debug) {
829
+ console.log('Journium: Successfully sent events', events);
830
+ }
831
+ }
832
+ catch (error) {
833
+ if (this.config.debug) {
834
+ console.error('Journium: Failed to send events', error);
835
+ }
836
+ throw error;
837
+ }
838
+ }
839
+ identify(distinctId, attributes = {}) {
840
+ var _a;
841
+ // Don't identify if SDK is not properly configured
842
+ if (!this.config || !this.config.token || !this.config.apiHost || !this.initialized) {
843
+ if ((_a = this.config) === null || _a === void 0 ? void 0 : _a.debug) {
844
+ console.warn('Journium: identify() call rejected - SDK not ready');
845
+ }
846
+ return;
847
+ }
848
+ // Call identify on identity manager to get previous distinct ID
849
+ const { previousDistinctId } = this.identityManager.identify(distinctId, attributes);
850
+ // Track $identify event with previous distinct ID
851
+ const identifyProperties = {
852
+ ...attributes,
853
+ $anon_distinct_id: previousDistinctId,
854
+ };
855
+ this.track('$identify', identifyProperties);
856
+ if (this.config.debug) {
857
+ console.log('Journium: User identified', { distinctId, attributes, previousDistinctId });
858
+ }
859
+ }
860
+ reset() {
861
+ var _a;
862
+ // Don't reset if SDK is not properly configured
863
+ if (!this.config || !this.config.token || !this.config.apiHost || !this.initialized) {
864
+ if ((_a = this.config) === null || _a === void 0 ? void 0 : _a.debug) {
865
+ console.warn('Journium: reset() call rejected - SDK not ready');
866
+ }
867
+ return;
868
+ }
869
+ // Reset identity in identity manager
870
+ this.identityManager.reset();
871
+ if (this.config.debug) {
872
+ console.log('Journium: User identity reset');
873
+ }
874
+ }
875
+ track(event, properties = {}) {
876
+ var _a;
877
+ // Don't track if SDK is not properly configured
878
+ if (!this.config || !this.config.token || !this.config.apiHost || !this.initialized) {
879
+ if ((_a = this.config) === null || _a === void 0 ? void 0 : _a.debug) {
880
+ console.warn('Journium: track() call rejected - SDK not ready');
881
+ }
882
+ return;
883
+ }
884
+ const identity = this.identityManager.getIdentity();
885
+ const userAgentInfo = this.identityManager.getUserAgentInfo();
886
+ // Create standardized event properties
887
+ const eventProperties = {
888
+ $device_id: identity === null || identity === void 0 ? void 0 : identity.$device_id,
889
+ distinct_id: identity === null || identity === void 0 ? void 0 : identity.distinct_id,
890
+ $session_id: identity === null || identity === void 0 ? void 0 : identity.$session_id,
891
+ $is_identified: (identity === null || identity === void 0 ? void 0 : identity.$user_state) === 'identified',
892
+ $current_url: typeof window !== 'undefined' ? window.location.href : '',
893
+ $pathname: typeof window !== 'undefined' ? window.location.pathname : '',
894
+ ...userAgentInfo,
895
+ $lib_version: '0.1.0', // TODO: Get from package.json
896
+ $platform: 'web',
897
+ ...properties, // User-provided properties override defaults
898
+ };
899
+ const journiumEvent = {
900
+ uuid: generateUuidv7(),
901
+ ingestion_key: this.config.token,
902
+ client_timestamp: getCurrentTimestamp(),
903
+ event,
904
+ properties: eventProperties,
905
+ };
906
+ this.queue.push(journiumEvent);
907
+ if (this.config.debug) {
908
+ console.log('Journium: Event tracked', journiumEvent);
909
+ }
910
+ if (this.queue.length >= this.config.flushAt) {
911
+ this.flush();
912
+ }
913
+ }
914
+ async flush() {
915
+ // Don't flush if SDK is not properly configured
916
+ if (!this.config || !this.config.token || !this.config.apiHost) {
917
+ return;
918
+ }
919
+ if (this.queue.length === 0)
920
+ return;
921
+ const events = [...this.queue];
922
+ this.queue = [];
923
+ try {
924
+ await this.sendEvents(events);
925
+ }
926
+ catch (error) {
927
+ this.queue.unshift(...events);
928
+ throw error;
929
+ }
930
+ }
931
+ destroy() {
932
+ if (this.flushTimer) {
933
+ clearInterval(this.flushTimer);
934
+ this.flushTimer = null;
935
+ }
936
+ this.flush();
937
+ }
938
+ }
939
+
940
+ class PageviewTracker {
941
+ constructor(client) {
942
+ this.lastUrl = '';
943
+ this.originalPushState = null;
944
+ this.originalReplaceState = null;
945
+ this.popStateHandler = null;
946
+ this.client = client;
947
+ }
948
+ capturePageview(customProperties = {}) {
949
+ const currentUrl = getCurrentUrl();
950
+ const url = new URL(currentUrl);
951
+ const properties = {
952
+ $current_url: currentUrl,
953
+ $host: url.host,
954
+ $pathname: url.pathname,
955
+ $search: url.search,
956
+ $title: getPageTitle(),
957
+ $referrer: getReferrer(),
958
+ ...customProperties,
959
+ };
960
+ this.client.track('$pageview', properties);
961
+ this.lastUrl = currentUrl;
962
+ }
963
+ startAutoCapture() {
964
+ this.capturePageview();
965
+ if (typeof window !== 'undefined') {
966
+ // Store original methods for cleanup
967
+ this.originalPushState = window.history.pushState;
968
+ this.originalReplaceState = window.history.replaceState;
969
+ window.history.pushState = (...args) => {
970
+ this.originalPushState.apply(window.history, args);
971
+ setTimeout(() => this.capturePageview(), 0);
972
+ };
973
+ window.history.replaceState = (...args) => {
974
+ this.originalReplaceState.apply(window.history, args);
975
+ setTimeout(() => this.capturePageview(), 0);
976
+ };
977
+ this.popStateHandler = () => {
978
+ setTimeout(() => this.capturePageview(), 0);
979
+ };
980
+ window.addEventListener('popstate', this.popStateHandler);
981
+ }
982
+ }
983
+ stopAutoCapture() {
984
+ if (typeof window !== 'undefined') {
985
+ // Restore original methods
986
+ if (this.originalPushState) {
987
+ window.history.pushState = this.originalPushState;
988
+ this.originalPushState = null;
989
+ }
990
+ if (this.originalReplaceState) {
991
+ window.history.replaceState = this.originalReplaceState;
992
+ this.originalReplaceState = null;
993
+ }
994
+ if (this.popStateHandler) {
995
+ window.removeEventListener('popstate', this.popStateHandler);
996
+ this.popStateHandler = null;
997
+ }
998
+ }
999
+ }
1000
+ }
1001
+
1002
+ class AutocaptureTracker {
1003
+ constructor(client, config = {}) {
1004
+ this.listeners = new Map();
1005
+ this.isActive = false;
1006
+ this.client = client;
1007
+ this.config = {
1008
+ captureClicks: true,
1009
+ captureFormSubmits: true,
1010
+ captureFormChanges: true,
1011
+ captureTextSelection: false,
1012
+ ignoreClasses: ['journium-ignore'],
1013
+ ignoreElements: ['script', 'style', 'noscript'],
1014
+ captureContentText: true,
1015
+ ...config,
1016
+ };
1017
+ }
1018
+ start() {
1019
+ if (!isBrowser() || this.isActive) {
1020
+ return;
1021
+ }
1022
+ this.isActive = true;
1023
+ if (this.config.captureClicks) {
1024
+ this.addClickListener();
1025
+ }
1026
+ if (this.config.captureFormSubmits) {
1027
+ this.addFormSubmitListener();
1028
+ }
1029
+ if (this.config.captureFormChanges) {
1030
+ this.addFormChangeListener();
1031
+ }
1032
+ if (this.config.captureTextSelection) {
1033
+ this.addTextSelectionListener();
1034
+ }
1035
+ }
1036
+ stop() {
1037
+ if (!isBrowser() || !this.isActive) {
1038
+ return;
1039
+ }
1040
+ this.isActive = false;
1041
+ this.listeners.forEach((listener, event) => {
1042
+ document.removeEventListener(event, listener, true);
1043
+ });
1044
+ this.listeners.clear();
1045
+ }
1046
+ addClickListener() {
1047
+ const clickListener = (event) => {
1048
+ const target = event.target;
1049
+ if (this.shouldIgnoreElement(target)) {
1050
+ return;
1051
+ }
1052
+ const properties = this.getElementProperties(target, 'click');
1053
+ this.client.track('$autocapture', {
1054
+ $event_type: 'click',
1055
+ ...properties,
1056
+ });
1057
+ };
1058
+ document.addEventListener('click', clickListener, true);
1059
+ this.listeners.set('click', clickListener);
1060
+ }
1061
+ addFormSubmitListener() {
1062
+ const submitListener = (event) => {
1063
+ const target = event.target;
1064
+ if (this.shouldIgnoreElement(target)) {
1065
+ return;
1066
+ }
1067
+ const properties = this.getFormProperties(target, 'submit');
1068
+ this.client.track('$autocapture', {
1069
+ $event_type: 'submit',
1070
+ ...properties,
1071
+ });
1072
+ };
1073
+ document.addEventListener('submit', submitListener, true);
1074
+ this.listeners.set('submit', submitListener);
1075
+ }
1076
+ addFormChangeListener() {
1077
+ const changeListener = (event) => {
1078
+ const target = event.target;
1079
+ if (this.shouldIgnoreElement(target) || !this.isFormElement(target)) {
1080
+ return;
1081
+ }
1082
+ const properties = this.getInputProperties(target, 'change');
1083
+ this.client.track('$autocapture', {
1084
+ $event_type: 'change',
1085
+ ...properties,
1086
+ });
1087
+ };
1088
+ document.addEventListener('change', changeListener, true);
1089
+ this.listeners.set('change', changeListener);
1090
+ }
1091
+ addTextSelectionListener() {
1092
+ const selectionListener = () => {
1093
+ const selection = window.getSelection();
1094
+ if (!selection || selection.toString().trim().length === 0) {
1095
+ return;
1096
+ }
1097
+ const selectedText = selection.toString().trim();
1098
+ if (selectedText.length < 3) { // Ignore very short selections
1099
+ return;
1100
+ }
1101
+ this.client.track('$autocapture', {
1102
+ $event_type: 'text_selection',
1103
+ $selected_text: selectedText.substring(0, 200), // Limit text length
1104
+ $selection_length: selectedText.length,
1105
+ });
1106
+ };
1107
+ document.addEventListener('mouseup', selectionListener);
1108
+ this.listeners.set('mouseup', selectionListener);
1109
+ }
1110
+ shouldIgnoreElement(element) {
1111
+ var _a, _b, _c;
1112
+ if (!element || !element.tagName) {
1113
+ return true;
1114
+ }
1115
+ // Check if element should be ignored by tag name
1116
+ if ((_a = this.config.ignoreElements) === null || _a === void 0 ? void 0 : _a.includes(element.tagName.toLowerCase())) {
1117
+ return true;
1118
+ }
1119
+ // Check if element has ignore classes
1120
+ if ((_b = this.config.ignoreClasses) === null || _b === void 0 ? void 0 : _b.some(cls => element.classList.contains(cls))) {
1121
+ return true;
1122
+ }
1123
+ // Check parent elements for ignore classes
1124
+ let parent = element.parentElement;
1125
+ while (parent) {
1126
+ if ((_c = this.config.ignoreClasses) === null || _c === void 0 ? void 0 : _c.some(cls => parent.classList.contains(cls))) {
1127
+ return true;
1128
+ }
1129
+ parent = parent.parentElement;
1130
+ }
1131
+ return false;
1132
+ }
1133
+ isFormElement(element) {
1134
+ const formElements = ['input', 'select', 'textarea'];
1135
+ return formElements.includes(element.tagName.toLowerCase());
1136
+ }
1137
+ getElementProperties(element, eventType) {
1138
+ const properties = {
1139
+ $element_tag: element.tagName.toLowerCase(),
1140
+ $element_type: this.getElementType(element),
1141
+ };
1142
+ // Element identifiers
1143
+ if (element.id) {
1144
+ properties.$element_id = element.id;
1145
+ }
1146
+ if (element.className) {
1147
+ properties.$element_classes = Array.from(element.classList);
1148
+ }
1149
+ // Element attributes
1150
+ const relevantAttributes = ['name', 'role', 'aria-label', 'data-testid', 'data-track'];
1151
+ relevantAttributes.forEach(attr => {
1152
+ const value = element.getAttribute(attr);
1153
+ if (value) {
1154
+ properties[`$element_${attr.replace('-', '_')}`] = value;
1155
+ }
1156
+ });
1157
+ // Element content
1158
+ if (this.config.captureContentText) {
1159
+ const text = this.getElementText(element);
1160
+ if (text) {
1161
+ properties.$element_text = text.substring(0, 200); // Limit text length
1162
+ }
1163
+ }
1164
+ // Elements chain data
1165
+ const elementsChain = this.getElementsChain(element);
1166
+ properties.$elements_chain = elementsChain.chain;
1167
+ properties.$elements_chain_href = elementsChain.href;
1168
+ properties.$elements_chain_elements = elementsChain.elements;
1169
+ properties.$elements_chain_texts = elementsChain.texts;
1170
+ properties.$elements_chain_ids = elementsChain.ids;
1171
+ // Position information
1172
+ const rect = element.getBoundingClientRect();
1173
+ properties.$element_position = {
1174
+ x: Math.round(rect.left),
1175
+ y: Math.round(rect.top),
1176
+ width: Math.round(rect.width),
1177
+ height: Math.round(rect.height),
1178
+ };
1179
+ // Parent information
1180
+ if (element.parentElement) {
1181
+ properties.$parent_tag = element.parentElement.tagName.toLowerCase();
1182
+ if (element.parentElement.id) {
1183
+ properties.$parent_id = element.parentElement.id;
1184
+ }
1185
+ }
1186
+ // URL information
1187
+ properties.$current_url = window.location.href;
1188
+ properties.$host = window.location.host;
1189
+ properties.$pathname = window.location.pathname;
1190
+ return properties;
1191
+ }
1192
+ getFormProperties(form, eventType) {
1193
+ const properties = this.getElementProperties(form, eventType);
1194
+ // Form-specific properties
1195
+ properties.$form_method = form.method || 'get';
1196
+ properties.$form_action = form.action || '';
1197
+ // Count form elements
1198
+ const inputs = form.querySelectorAll('input, select, textarea');
1199
+ properties.$form_elements_count = inputs.length;
1200
+ // Form element types
1201
+ const elementTypes = {};
1202
+ inputs.forEach(input => {
1203
+ const type = this.getElementType(input);
1204
+ elementTypes[type] = (elementTypes[type] || 0) + 1;
1205
+ });
1206
+ properties.$form_element_types = elementTypes;
1207
+ return properties;
1208
+ }
1209
+ getInputProperties(input, eventType) {
1210
+ const properties = this.getElementProperties(input, eventType);
1211
+ // Input-specific properties
1212
+ properties.$input_type = input.type || 'text';
1213
+ if (input.name) {
1214
+ properties.$input_name = input.name;
1215
+ }
1216
+ if (input.placeholder) {
1217
+ properties.$input_placeholder = input.placeholder;
1218
+ }
1219
+ // Value information (be careful with sensitive data)
1220
+ if (this.isSafeInputType(input.type)) {
1221
+ if (input.type === 'checkbox' || input.type === 'radio') {
1222
+ properties.$input_checked = input.checked;
1223
+ }
1224
+ else if (input.value) {
1225
+ // For safe inputs, capture value length and basic characteristics
1226
+ properties.$input_value_length = input.value.length;
1227
+ properties.$input_has_value = input.value.length > 0;
1228
+ // For select elements, capture the selected value
1229
+ if (input.tagName.toLowerCase() === 'select') {
1230
+ properties.$input_selected_value = input.value;
1231
+ }
1232
+ }
1233
+ }
1234
+ // Form context
1235
+ const form = input.closest('form');
1236
+ if (form && form.id) {
1237
+ properties.$form_id = form.id;
1238
+ }
1239
+ return properties;
1240
+ }
1241
+ getElementType(element) {
1242
+ const tag = element.tagName.toLowerCase();
1243
+ if (tag === 'input') {
1244
+ return element.type || 'text';
1245
+ }
1246
+ if (tag === 'button') {
1247
+ return element.type || 'button';
1248
+ }
1249
+ return tag;
1250
+ }
1251
+ getElementText(element) {
1252
+ var _a, _b;
1253
+ // For buttons and links, get the visible text
1254
+ if (['button', 'a'].includes(element.tagName.toLowerCase())) {
1255
+ return ((_a = element.textContent) === null || _a === void 0 ? void 0 : _a.trim()) || '';
1256
+ }
1257
+ // For inputs, get placeholder or label
1258
+ if (element.tagName.toLowerCase() === 'input') {
1259
+ const input = element;
1260
+ return input.placeholder || input.value || '';
1261
+ }
1262
+ // For other elements, get text content but limit it
1263
+ const text = ((_b = element.textContent) === null || _b === void 0 ? void 0 : _b.trim()) || '';
1264
+ return text.length > 50 ? text.substring(0, 47) + '...' : text;
1265
+ }
1266
+ getElementsChain(element) {
1267
+ var _a;
1268
+ const elements = [];
1269
+ const texts = [];
1270
+ const ids = [];
1271
+ let href = '';
1272
+ let current = element;
1273
+ while (current && current !== document.body) {
1274
+ // Element selector
1275
+ let selector = current.tagName.toLowerCase();
1276
+ // Add ID if present
1277
+ if (current.id) {
1278
+ selector += `#${current.id}`;
1279
+ ids.push(current.id);
1280
+ }
1281
+ else {
1282
+ ids.push('');
1283
+ }
1284
+ // Add classes if present
1285
+ if (current.className && typeof current.className === 'string') {
1286
+ const classes = current.className.trim().split(/\s+/).slice(0, 3); // Limit to first 3 classes
1287
+ if (classes.length > 0 && classes[0] !== '') {
1288
+ selector += '.' + classes.join('.');
1289
+ }
1290
+ }
1291
+ // Add nth-child if no ID (to make selector more specific)
1292
+ if (!current.id && current.parentElement) {
1293
+ const siblings = Array.from(current.parentElement.children)
1294
+ .filter(child => child.tagName === current.tagName);
1295
+ if (siblings.length > 1) {
1296
+ const index = siblings.indexOf(current) + 1;
1297
+ selector += `:nth-child(${index})`;
1298
+ }
1299
+ }
1300
+ elements.push(selector);
1301
+ // Extract text content
1302
+ let text = '';
1303
+ if (current.tagName.toLowerCase() === 'a') {
1304
+ text = ((_a = current.textContent) === null || _a === void 0 ? void 0 : _a.trim()) || '';
1305
+ // Capture href for links
1306
+ if (!href && current.getAttribute('href')) {
1307
+ href = current.getAttribute('href') || '';
1308
+ }
1309
+ }
1310
+ else if (['button', 'span', 'div'].includes(current.tagName.toLowerCase())) {
1311
+ // For buttons and text elements, get direct text content (not including children)
1312
+ const directText = Array.from(current.childNodes)
1313
+ .filter(node => node.nodeType === Node.TEXT_NODE)
1314
+ .map(node => { var _a; return (_a = node.textContent) === null || _a === void 0 ? void 0 : _a.trim(); })
1315
+ .join(' ')
1316
+ .trim();
1317
+ text = directText || '';
1318
+ }
1319
+ else if (current.tagName.toLowerCase() === 'input') {
1320
+ const input = current;
1321
+ text = input.placeholder || input.value || '';
1322
+ }
1323
+ // Limit text length and clean it
1324
+ text = text.substring(0, 100).replace(/\s+/g, ' ').trim();
1325
+ texts.push(text);
1326
+ current = current.parentElement;
1327
+ }
1328
+ // Build the chain string (reverse order so it goes from parent to child)
1329
+ const chain = elements.reverse().join(' > ');
1330
+ return {
1331
+ chain,
1332
+ href,
1333
+ elements: elements,
1334
+ texts: texts.reverse(),
1335
+ ids: ids.reverse()
1336
+ };
1337
+ }
1338
+ isSafeInputType(type) {
1339
+ // Don't capture values for sensitive input types
1340
+ const sensitiveTypes = ['password', 'email', 'tel', 'credit-card-number'];
1341
+ return !sensitiveTypes.includes(type.toLowerCase());
1342
+ }
1343
+ }
1344
+
1345
+ class Journium {
1346
+ constructor(config) {
1347
+ this.config = config;
1348
+ this.client = new JourniumClient(config);
1349
+ this.pageviewTracker = new PageviewTracker(this.client);
1350
+ const autocaptureConfig = this.resolveAutocaptureConfig(config.autocapture);
1351
+ this.autocaptureTracker = new AutocaptureTracker(this.client, autocaptureConfig);
1352
+ // Store resolved autocapture state for startAutoCapture method
1353
+ this.autocaptureEnabled = config.autocapture !== false;
1354
+ }
1355
+ resolveAutocaptureConfig(autocapture) {
1356
+ if (autocapture === false) {
1357
+ return {
1358
+ captureClicks: false,
1359
+ captureFormSubmits: false,
1360
+ captureFormChanges: false,
1361
+ captureTextSelection: false,
1362
+ };
1363
+ }
1364
+ if (autocapture === true || autocapture === undefined) {
1365
+ return {}; // Use default configuration (enabled by default)
1366
+ }
1367
+ return autocapture;
1368
+ }
1369
+ track(event, properties) {
1370
+ this.client.track(event, properties);
1371
+ }
1372
+ identify(distinctId, attributes) {
1373
+ this.client.identify(distinctId, attributes);
1374
+ }
1375
+ reset() {
1376
+ this.client.reset();
1377
+ }
1378
+ capturePageview(properties) {
1379
+ this.pageviewTracker.capturePageview(properties);
1380
+ }
1381
+ startAutoCapture() {
1382
+ this.pageviewTracker.startAutoCapture();
1383
+ if (this.autocaptureEnabled) {
1384
+ this.autocaptureTracker.start();
1385
+ }
1386
+ }
1387
+ stopAutoCapture() {
1388
+ this.pageviewTracker.stopAutoCapture();
1389
+ this.autocaptureTracker.stop();
1390
+ }
1391
+ // Aliases for consistency (deprecated - use startAutoCapture)
1392
+ /** @deprecated Use startAutoCapture() instead */
1393
+ startAutocapture() {
1394
+ this.startAutoCapture();
1395
+ }
1396
+ /** @deprecated Use stopAutoCapture() instead */
1397
+ stopAutocapture() {
1398
+ this.stopAutoCapture();
1399
+ }
1400
+ async flush() {
1401
+ return this.client.flush();
1402
+ }
1403
+ destroy() {
1404
+ this.pageviewTracker.stopAutoCapture();
1405
+ this.autocaptureTracker.stop();
1406
+ this.client.destroy();
1407
+ }
1408
+ }
1409
+ const init = (config) => {
1410
+ return new Journium(config);
1411
+ };
1412
+
1413
+ exports.AutocaptureTracker = AutocaptureTracker;
1414
+ exports.BrowserIdentityManager = BrowserIdentityManager;
1415
+ exports.Journium = Journium;
1416
+ exports.JourniumClient = JourniumClient;
1417
+ exports.PageviewTracker = PageviewTracker;
1418
+ exports.fetchRemoteConfig = fetchRemoteConfig;
1419
+ exports.generateId = generateId;
1420
+ exports.generateUuidv7 = generateUuidv7;
1421
+ exports.getCurrentTimestamp = getCurrentTimestamp;
1422
+ exports.getCurrentUrl = getCurrentUrl;
1423
+ exports.getPageTitle = getPageTitle;
1424
+ exports.getReferrer = getReferrer;
1425
+ exports.init = init;
1426
+ exports.isBrowser = isBrowser;
1427
+ exports.isNode = isNode;
1428
+ exports.mergeConfigs = mergeConfigs;
1429
+
1430
+ }));
1431
+ //# sourceMappingURL=index.umd.js.map