@naturalcycles/internal-web-lib 1.0.2 → 1.1.1

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.
Files changed (61) hide show
  1. package/bundle/analyticsClient.js +19 -19
  2. package/bundle/analyticsClient.js.map +1 -1
  3. package/bundle/cookieMonster.js +863 -0
  4. package/bundle/cookieMonster.js.map +1 -0
  5. package/dist/analytics/analyticsClient.js +674 -804
  6. package/dist/analytics/index.js +2 -1
  7. package/dist/cookie-monster/components/setting-dropdown/setting-dropdown.component.d.ts +17 -0
  8. package/dist/cookie-monster/components/setting-dropdown/setting-dropdown.component.js +4 -0
  9. package/dist/cookie-monster/components/setting-dropdown/setting-dropdown2.component.js +129 -0
  10. package/dist/cookie-monster/components/themed-button/themed-button.component.d.ts +7 -0
  11. package/dist/cookie-monster/components/themed-button/themed-button.component.js +4 -0
  12. package/dist/cookie-monster/components/themed-button/themed-button2.component.js +30 -0
  13. package/dist/cookie-monster/consent/consent.service.d.ts +24 -0
  14. package/dist/cookie-monster/consent/consent.service.js +68 -0
  15. package/dist/cookie-monster/consent/consentLogStorage.service.d.ts +9 -0
  16. package/dist/cookie-monster/consent/consentLogStorage.service.js +38 -0
  17. package/dist/cookie-monster/const/cookieMonster.const.d.ts +4 -0
  18. package/dist/cookie-monster/const/cookieMonster.const.js +11 -0
  19. package/dist/cookie-monster/cookie-monster-element.d.ts +33 -0
  20. package/dist/cookie-monster/cookie-monster-element.js +121 -0
  21. package/dist/cookie-monster/cookieMonsterService.d.ts +19 -0
  22. package/dist/cookie-monster/cookieMonsterService.js +59 -0
  23. package/dist/cookie-monster/css/fonts.js +4 -0
  24. package/dist/cookie-monster/css/general.js +4 -0
  25. package/dist/cookie-monster/css/shared-sheets.d.ts +4 -0
  26. package/dist/cookie-monster/css/shared-sheets.js +14 -0
  27. package/dist/cookie-monster/icons/back.js +4 -0
  28. package/dist/cookie-monster/icons/chevron.js +4 -0
  29. package/dist/cookie-monster/index.d.ts +2 -0
  30. package/dist/cookie-monster/index.js +3 -0
  31. package/dist/cookie-monster/lang/da-DK.js +22 -0
  32. package/dist/cookie-monster/lang/de-DE.js +22 -0
  33. package/dist/cookie-monster/lang/en-GB.js +22 -0
  34. package/dist/cookie-monster/lang/en-US.js +22 -0
  35. package/dist/cookie-monster/lang/es-US.js +22 -0
  36. package/dist/cookie-monster/lang/fi-FI.js +22 -0
  37. package/dist/cookie-monster/lang/fr-FR.js +22 -0
  38. package/dist/cookie-monster/lang/it-IT.js +22 -0
  39. package/dist/cookie-monster/lang/no-NO.js +22 -0
  40. package/dist/cookie-monster/lang/pt-BR.js +22 -0
  41. package/dist/cookie-monster/lang/sv-SE.js +22 -0
  42. package/dist/cookie-monster/types/CookieMonster.types.d.ts +56 -0
  43. package/dist/cookie-monster/types/CookieMonster.types.js +34 -0
  44. package/dist/cookie-monster/utils/createButton.d.ts +1 -0
  45. package/dist/cookie-monster/utils/createButton.js +11 -0
  46. package/dist/cookie-monster/utils/getCookieMonsterEvent.d.ts +2 -0
  47. package/dist/cookie-monster/utils/getCookieMonsterEvent.js +10 -0
  48. package/dist/cookie-monster/utils/handleRootClick.d.ts +1 -0
  49. package/dist/cookie-monster/utils/handleRootClick.js +16 -0
  50. package/dist/cookie-monster/utils/linkify.d.ts +1 -0
  51. package/dist/cookie-monster/utils/linkify.js +9 -0
  52. package/dist/cookie-monster/utils/translationsService.d.ts +7 -0
  53. package/dist/cookie-monster/utils/translationsService.js +56 -0
  54. package/dist/cookie-monster/views/main.view.d.ts +24 -0
  55. package/dist/cookie-monster/views/main.view.js +115 -0
  56. package/dist/cookie-monster/views/settings.view.d.ts +18 -0
  57. package/dist/cookie-monster/views/settings.view.js +96 -0
  58. package/dist/cookie-monster/views/views.js +4 -0
  59. package/dist/index.js +2 -1
  60. package/dist/node_modules/.pnpm/@naturalcycles_ncuikit@1.14.0_react@19.3.0_undici@8.11.2/node_modules/@naturalcycles/ncuikit/dist/styles/_palette.js +4 -0
  61. package/package.json +14 -4
@@ -1,828 +1,698 @@
1
- import { _isEmptyObject, isServerSide } from '@naturalcycles/js-lib';
2
- import { CANONICAL_UTM_PARAMS, ANALYTICS_IDENTIFY_EVENT_NAME, truncateAnalyticsProperty, } from '@naturalcycles/js-lib/analytics';
3
- import { _mapToObject } from '@naturalcycles/js-lib/array';
4
- import { _errorDataAppend, AppError } from '@naturalcycles/js-lib/error';
5
- import { getFetcher } from '@naturalcycles/js-lib/http';
6
- import { nanoidBrowser, nanoidBrowserCustomAlphabet } from '@naturalcycles/js-lib/nanoid';
7
- import { _filterNullishValues, _filterObject } from '@naturalcycles/js-lib/object';
8
- import { _safeJsonStringify } from '@naturalcycles/js-lib/string';
9
- import { _noop } from '@naturalcycles/js-lib/types';
1
+ import { _isEmptyObject, isServerSide } from "@naturalcycles/js-lib";
2
+ import { ANALYTICS_IDENTIFY_EVENT_NAME, CANONICAL_UTM_PARAMS, truncateAnalyticsProperty } from "@naturalcycles/js-lib/analytics";
3
+ import { _mapToObject } from "@naturalcycles/js-lib/array";
4
+ import { AppError, _errorDataAppend } from "@naturalcycles/js-lib/error";
5
+ import { getFetcher } from "@naturalcycles/js-lib/http";
6
+ import { nanoidBrowser, nanoidBrowserCustomAlphabet } from "@naturalcycles/js-lib/nanoid";
7
+ import { _filterNullishValues, _filterObject } from "@naturalcycles/js-lib/object";
8
+ import { _safeJsonStringify } from "@naturalcycles/js-lib/string";
9
+ import { _noop } from "@naturalcycles/js-lib/types";
10
+ //#region src/analytics/analyticsClient.ts
10
11
  /**
11
- * Browsers drop a cookie over 4096 bytes, and the identity goes with it. The 3800 budget also
12
- * has to cover the `; expires=`, `; path=`, `; domain=` and `; secure` attributes that setCookie
13
- * appends (roughly 75 bytes), which cookieLength below does not measure. Keep that in mind
14
- * before raising this toward 4096.
15
- */
16
- const MAX_COOKIE_LENGTH = 3800;
17
- const UTM_QUERY_PARAM_PREFIX = 'utm_';
18
- const CLICK_QUERY_PARAMS = [
19
- 'dclid',
20
- 'fbclid',
21
- 'gclid',
22
- 'ko_click_id',
23
- 'li_fat_id',
24
- 'msclkid',
25
- 'sccid',
26
- 'ttclid',
27
- 'twclid',
28
- 'wbraid',
12
+ * Browsers drop a cookie over 4096 bytes, and the identity goes with it. The 3800 budget also
13
+ * has to cover the `; expires=`, `; path=`, `; domain=` and `; secure` attributes that setCookie
14
+ * appends (roughly 75 bytes), which cookieLength below does not measure. Keep that in mind
15
+ * before raising this toward 4096.
16
+ */
17
+ var MAX_COOKIE_LENGTH = 3800;
18
+ var UTM_QUERY_PARAM_PREFIX = "utm_";
19
+ var CLICK_QUERY_PARAMS = [
20
+ "dclid",
21
+ "fbclid",
22
+ "gclid",
23
+ "ko_click_id",
24
+ "li_fat_id",
25
+ "msclkid",
26
+ "sccid",
27
+ "ttclid",
28
+ "twclid",
29
+ "wbraid"
29
30
  ];
30
- const generateEventId = nanoidBrowserCustomAlphabet('0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz', 16);
31
+ var generateEventId = nanoidBrowserCustomAlphabet("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz", 16);
31
32
  /**
32
- * Self-hosted analytics event client.
33
- *
34
- * Captures client-side analytics events, adds browser page properties, batches them, retries
35
- * transient failures and delivers them to our own Backend via a single REST endpoint.
36
- */
37
- export class AnalyticsClient {
38
- constructor(cfg) {
39
- const localStorageKeyPrefix = cfg.localStorageKeyPrefix || 'nca';
40
- this.cfg = {
41
- onError: _noop,
42
- firstTouchUrl: '',
43
- isEnabled: () => true,
44
- getCommonProps: () => ({}),
45
- flushInterval: 5000,
46
- maxBatchSize: 50,
47
- maxBatchBytes: 60_000,
48
- maxQueueSize: 1000,
49
- maxRetryBackoff: 600_000,
50
- requestTimeout: 30_000,
51
- persistQueue: true,
52
- maxPersistedAge: 24 * 3_600_000,
53
- localStorageKeyPrefix,
54
- logger: console,
55
- debug: false,
56
- ...cfg,
57
- };
58
- this.identity = new AnalyticsIdentity(this.cfg.identity);
59
- // Unlike events, we only fire firstTouch once per pageload, so
60
- // we care about and can tolerate retries
61
- this.firstTouchFetcher = getFetcher({
62
- logger: this.cfg.logger,
63
- retryPost: true,
64
- timeoutSeconds: 10,
65
- });
66
- if (isServerSide())
67
- return;
68
- this.restoreOrphanedQueues();
69
- globalThis.addEventListener('pagehide', this.handlePageHide);
70
- document.addEventListener('visibilitychange', this.handleVisibilityChange);
71
- }
72
- cfg;
73
- firstTouchFetcher;
74
- /**
75
- * The built-in identity, constructed from `cfg.identity`. Also readable by application code,
76
- * e.g to feed the distinctId or acquisition props to other integrations.
77
- */
78
- identity;
79
- /**
80
- * Random id of this AnalyticsClient instance (in practice - of this tab/pageload),
81
- * used to namespace the localStorage queue snapshot.
82
- */
83
- tabId = nanoidBrowser(10);
84
- queue = [];
85
- flushTimer;
86
- isFlushing = false;
87
- consecutiveFailures = 0;
88
- /**
89
- * Requested by the server via `Retry-After` header (already converted to ms).
90
- */
91
- retryAfter = 0;
92
- hasUpdatedAcquisitionProps = false;
93
- eventListeners = new Set();
94
- handlePageHide = () => {
95
- this.flushNow();
96
- };
97
- handleVisibilityChange = () => {
98
- // Fires on mobile when the tab is backgrounded (where pagehide may never fire) -
99
- // the last reliable moment to hand events over to the browser
100
- if (document.visibilityState === 'hidden')
101
- this.flushNow();
102
- };
103
- /**
104
- * Optional eager bootstrapping, to call once at app boot: registers the acquisition props
105
- * into the identity entry while the landing url/referrer are still current - the moment
106
- * an SPA navigation can strip the utm params off the url before the first event fires. Gated by cfg.isEnabled, like the events themselves.
107
- * Without it the registration happens lazily before the first enabled event.
108
- */
109
- init() {
110
- if (!this.canTrack())
111
- return;
112
- this.ensureAcquisitionProps();
113
- this.replayFromStub();
114
- }
115
- track(name, props) {
116
- const event = this.enqueue(name, props);
117
- if (event)
118
- this.notifyEventListeners(event);
119
- }
120
- /**
121
- * Replays the calls a stub recorded on the page before this client loaded, under the timestamps
122
- * they were made at. Tracked in order, so calls made before the stub's `identify()` keep the
123
- * anonymous identity.
124
- */
125
- replayFromStub() {
126
- const stub = globalThis.analyticsClient;
127
- if (!stub || !('q' in stub))
128
- return;
129
- for (const call of stub.q) {
130
- try {
131
- if (call.method === 'identify') {
132
- this.identify(call.args[0]);
133
- }
134
- else {
135
- // Not track(), so the call keeps the timestamp it was originally made at
136
- const event = this.enqueue(call.args[0], call.args[1], call.ts);
137
- if (event)
138
- this.notifyEventListeners(event);
139
- }
140
- }
141
- catch (err) {
142
- this.cfg.logger.warn('[analytics] could not replay a stubbed call', err);
143
- }
144
- }
145
- stub.q.length = 0;
146
- }
147
- /**
148
- * Registers a listener called synchronously for every delivered event, past the gates in
149
- * `track()`. Returns an unsubscribe function. Listeners receive a snapshot and cannot affect
150
- * delivery, and a throwing listener never breaks tracking.
151
- */
152
- onEvent(listener) {
153
- this.eventListeners.add(listener);
154
- return () => this.eventListeners.delete(listener);
155
- }
156
- notifyEventListeners(event) {
157
- if (!this.eventListeners.size)
158
- return;
159
- const distinctId = event.userId || this.identity.getDistinctId();
160
- // Snapshot so a listener cannot mutate the event already queued for delivery. Shallow because
161
- // props may hold circular references (see track()) that a deep JSON copy would throw on.
162
- const snapshot = { ...event, props: { ...event.props } };
163
- for (const listener of this.eventListeners) {
164
- try {
165
- listener(snapshot, distinctId);
166
- }
167
- catch (err) {
168
- this.cfg.logger.warn('[analytics] onEvent listener threw', err);
169
- }
170
- }
171
- }
172
- /**
173
- * Sets the distinctId going forward (persisted), e.g after a successful signup.
174
- * Pending events are kept, each under the identity it was tracked with.
175
- */
176
- identify(userId) {
177
- if (!this.canTrack())
178
- return;
179
- const previousDistinctId = this.identity.getDistinctId();
180
- this.identity.identify(userId);
181
- const distinctId = userId;
182
- if (previousDistinctId === distinctId)
183
- return;
184
- this.enqueue(ANALYTICS_IDENTIFY_EVENT_NAME, { anon_distinct_id: previousDistinctId });
185
- void this.flush();
186
- // The destination merges the event streams of the two ids, but not their profiles
187
- void this.sendFirstTouch();
188
- }
189
- enqueue(name, props, ts) {
190
- if (!this.canTrack())
191
- return;
192
- this.ensureAcquisitionProps();
193
- // Deliberately no client-side normalization: the server owns length limits, while
194
- // circular references are handled at serialization time by _safeJsonStringify.
195
- const event = {
196
- id: generateEventId(),
197
- name,
198
- ts: ts || Date.now(),
199
- props: {
200
- ...this.getDefaultProps(),
201
- ...this.cfg.getCommonProps(),
202
- ...props,
203
- },
204
- userId: this.identity.getDistinctId(),
205
- };
206
- if (this.cfg.debug)
207
- this.cfg.logger.log(`[analytics] ${event.name}`, event.props);
208
- this.queue.push(event);
209
- if (this.queue.length > this.cfg.maxQueueSize) {
210
- this.queue.shift();
211
- this.cfg.logger.warn('[analytics] queue overflow, dropped the oldest event');
212
- }
213
- this.persistQueue();
214
- if (this.queue.length >= this.cfg.maxBatchSize && !this.consecutiveFailures) {
215
- void this.flush();
216
- }
217
- else {
218
- this.scheduleFlush();
219
- }
220
- return event;
221
- }
222
- canTrack() {
223
- return !isServerSide() && this.cfg.isEnabled();
224
- }
225
- /**
226
- * Clears pending events and the persisted identity entry, including its acquisition
227
- * properties. A new identity is generated on next use.
228
- */
229
- reset() {
230
- this.clearPendingEvents();
231
- this.identity.reset();
232
- // The entry was wiped - re-register the acquisition props on the next tracked event
233
- this.hasUpdatedAcquisitionProps = false;
234
- }
235
- /**
236
- * Drains the queue, one batch per request.
237
- * Called automatically (flush interval / batch size / pagehide) - public for manual flushing.
238
- */
239
- async flush() {
240
- if (isServerSide() || this.isFlushing)
241
- return;
242
- this.isFlushing = true;
243
- this.clearFlushTimer();
244
- try {
245
- // The outer loop picks up events tracked while a batch was in-flight
246
- while (this.queue.length) {
247
- for (const batch of this.prepareBatches()) {
248
- const result = await this.sendBatch(batch);
249
- if (result === 'retry') {
250
- this.consecutiveFailures++;
251
- this.scheduleFlush();
252
- return;
253
- }
254
- // 'ok' or 'drop' - the batch is done either way
255
- this.removeFromQueue(batch.events);
256
- this.consecutiveFailures = 0;
257
- this.retryAfter = 0;
258
- }
259
- }
260
- }
261
- finally {
262
- this.isFlushing = false;
263
- }
264
- }
265
- /**
266
- * Immediate flush: hands queued events over to the browser via keepalive fetch.
267
- * Runs automatically on pagehide / visibilitychange:hidden. Public so that app code can
268
- * hand events over right away: events tracked in its own page-lifecycle handlers (which
269
- * run after this client's own), and events tracked right before a navigation (e.g a
270
- * cta click) - unload-time delivery is best-effort and can be lost, while a
271
- * keepalive request from a still-alive page survives the navigation.
272
- * The persisted queue remains untouched because keepalive requests cannot reliably process
273
- * their response; a later regular flush re-sends and deduplicates the events.
274
- */
275
- flushNow() {
276
- if (!this.queue.length)
277
- return;
278
- this.clearFlushTimer();
279
- for (const batch of this.prepareBatches()) {
280
- void this.postBatch(batch, true).catch(err => {
281
- this.cfg.logger.warn('[analytics] lifecycle batch failed to send; retained for retry', err);
282
- });
283
- }
284
- // visibilitychange can fire without unloading the page, so retain normal retry behavior.
285
- this.scheduleFlush();
286
- }
287
- /**
288
- * Removes listeners and pending timers. Only needed when an instance is discarded
289
- * (e.g in tests or HMR) - the app-wide singleton never needs it.
290
- */
291
- destroy() {
292
- this.clearFlushTimer();
293
- if (isServerSide())
294
- return;
295
- globalThis.removeEventListener('pagehide', this.handlePageHide);
296
- document.removeEventListener('visibilitychange', this.handleVisibilityChange);
297
- }
298
- /**
299
- * Registers the acquisition props into the identity entry once per pageload, like
300
- * eagerly from init(), or lazily before the first
301
- * enabled event. Either path is gated by cfg.isEnabled, so bot/e2e/consent gating
302
- * applies to the persistence write too.
303
- */
304
- ensureAcquisitionProps() {
305
- if (this.hasUpdatedAcquisitionProps)
306
- return;
307
- this.hasUpdatedAcquisitionProps = true;
308
- this.identity.updateAcquisitionProps();
309
- void this.sendFirstTouch();
310
- }
311
- /**
312
- * Posts the first-touch props to their own endpoint, once per pageload. Fire-and-forget:
313
- * `$set_once` ignores every write after the first, so a lost or repeated call costs nothing.
314
- */
315
- async sendFirstTouch() {
316
- const { firstTouchUrl } = this.cfg;
317
- if (!firstTouchUrl)
318
- return;
319
- const props = this.identity.getFirstTouchProps();
320
- if (!props)
321
- return;
322
- const input = {
323
- clientId: this.cfg.clientId,
324
- userId: this.identity.getDistinctId(),
325
- props,
326
- };
327
- const res = await this.firstTouchFetcher.doFetch({
328
- url: firstTouchUrl,
329
- method: 'POST',
330
- text: _safeJsonStringify(input),
331
- responseType: 'void',
332
- });
333
- if (!res.err)
334
- return;
335
- try {
336
- this.cfg.onError(_errorDataAppend(res.err, { firstTouch: true }));
337
- }
338
- catch (err) {
339
- this.cfg.logger.warn('[analytics] onError hook threw', err);
340
- }
341
- }
342
- getDefaultProps() {
343
- const referrer = document.referrer;
344
- const url = new URL(globalThis.location.href);
345
- return {
346
- ...(referrer && { referrer }),
347
- current_url: globalThis.location.href,
348
- screen_height: globalThis.screen.height,
349
- screen_width: globalThis.screen.width,
350
- // First-touch initial_referrer and last-touch utm_* come from the identity entry -
351
- // the single acquisition-props store (cross-subdomain when cookie-persisted)
352
- ...this.identity.getAcquisitionProps(),
353
- // Utms of the current url win over the persisted last-touch values
354
- ...getLastTouchUtms(url),
355
- // Click ids are read from the current url only, never persisted
356
- ...getQueryProperties(url, CLICK_QUERY_PARAMS),
357
- };
358
- }
359
- clearPendingEvents() {
360
- this.clearFlushTimer();
361
- this.queue = [];
362
- this.consecutiveFailures = 0;
363
- this.retryAfter = 0;
364
- this.persistQueue();
365
- }
366
- async sendBatch(batch) {
367
- let res;
368
- try {
369
- // Optional chaining: AbortSignal.timeout is missing in older Safari (<16)
370
- res = await this.postBatch(batch, false, AbortSignal.timeout?.(this.cfg.requestTimeout));
371
- }
372
- catch (err) {
373
- // Network error or timeout - eligible for retry
374
- this.cfg.logger.warn('[analytics] batch failed to send, will retry', err);
375
- return 'retry';
376
- }
377
- if (res.ok)
378
- return 'ok';
379
- if (res.status === 429 || res.status >= 500) {
380
- const retryAfter = Number(res.headers.get('retry-after'));
381
- if (retryAfter)
382
- this.retryAfter = retryAfter * 1000;
383
- this.cfg.logger.warn(`[analytics] batch rejected with ${res.status}, will retry`);
384
- return 'retry';
385
- }
386
- // Non-retryable 4xx - drop the batch to avoid a poison-pill retry loop
387
- this.cfg.logger.error(`[analytics] batch rejected with ${res.status}, dropping ${batch.events.length} event(s)`);
388
- try {
389
- this.cfg.onError(new AppError('batch dropped on a non-retryable status', {
390
- status: res.status,
391
- eventCount: batch.events.length,
392
- }));
393
- }
394
- catch (err) {
395
- this.cfg.logger.warn('[analytics] onError hook threw', err);
396
- }
397
- return 'drop';
398
- }
399
- async postBatch(batch, isLifecycleFlush, signal) {
400
- return fetch(this.cfg.url, {
401
- method: 'POST',
402
- // `text/plain` (a CORS-safelisted content-type) avoids a preflight OPTIONS round-trip
403
- // on every batch and keeps pagehide requests deliverable. The body is still a JSON string.
404
- headers: { 'content-type': 'text/plain' },
405
- body: batch.body,
406
- // Regular flushes use ordinary fetch; keepalive is reserved for page lifecycle delivery.
407
- ...(isLifecycleFlush && { keepalive: true }),
408
- signal,
409
- });
410
- }
411
- /**
412
- * Splits the whole queue into request-ready batches of at most maxBatchSize events
413
- * and maxBatchBytes serialized bytes each.
414
- */
415
- prepareBatches() {
416
- const batches = [];
417
- let current;
418
- const userId = this.identity.getDistinctId();
419
- for (const event of this.queue) {
420
- // _safeJsonStringify (here and wherever events are serialized): circular references in
421
- // props degrade to '[Circular ~]' markers instead of throwing - tracking must never
422
- // break the app. Non-circular events take its native JSON.stringify fast path.
423
- const eventBytes = getUtf8ByteLength(_safeJsonStringify(event));
424
- const hasReachedCount = current?.events.length === this.cfg.maxBatchSize;
425
- const separatorBytes = current?.events.length ? 1 : 0;
426
- const hasReachedBytes = !!current && current.bodyBytes + separatorBytes + eventBytes > this.cfg.maxBatchBytes;
427
- if (current && (hasReachedCount || hasReachedBytes)) {
428
- batches.push(this.finalizeBatch(current));
429
- current = undefined;
430
- }
431
- current ||= this.createPendingBatch(userId);
432
- const nextSeparatorBytes = current.events.length ? 1 : 0;
433
- current.events.push(event);
434
- current.bodyBytes += nextSeparatorBytes + eventBytes;
435
- }
436
- if (current?.events.length) {
437
- batches.push(this.finalizeBatch(current));
438
- }
439
- return batches;
440
- }
441
- createPendingBatch(userId) {
442
- const sentAt = Date.now();
443
- const emptyBody = {
444
- sentAt,
445
- clientId: this.cfg.clientId,
446
- userId,
447
- events: [],
448
- };
449
- return {
450
- sentAt,
451
- userId,
452
- events: [],
453
- bodyBytes: getUtf8ByteLength(JSON.stringify(emptyBody)),
454
- };
455
- }
456
- finalizeBatch(batch) {
457
- const input = {
458
- sentAt: batch.sentAt,
459
- clientId: this.cfg.clientId,
460
- userId: batch.userId,
461
- events: batch.events,
462
- };
463
- return {
464
- events: batch.events,
465
- body: _safeJsonStringify(input),
466
- };
467
- }
468
- /**
469
- * Adopts queue snapshots persisted by previous pageloads (crashed/killed tabs) and re-sends them.
470
- * A snapshot of a still-alive tab may be adopted too - the resulting duplicate delivery
471
- * is deduped by the Backend via event ids.
472
- */
473
- restoreOrphanedQueues() {
474
- if (!this.cfg.persistQueue)
475
- return;
476
- try {
477
- const prefix = `${this.cfg.localStorageKeyPrefix}.q.`;
478
- const keys = [];
479
- for (let i = 0; i < localStorage.length; i++) {
480
- const key = localStorage.key(i);
481
- if (key?.startsWith(prefix))
482
- keys.push(key);
483
- }
484
- if (!keys.length)
485
- return;
486
- const minTs = Date.now() - this.cfg.maxPersistedAge;
487
- for (const key of keys) {
488
- try {
489
- const events = JSON.parse(localStorage.getItem(key) || '[]');
490
- localStorage.removeItem(key);
491
- this.queue.push(...events.filter(event => event.ts >= minTs));
492
- }
493
- catch {
494
- // Corrupted snapshot - discard only this key and continue restoring the others.
495
- localStorage.removeItem(key);
496
- }
497
- }
498
- if (this.queue.length > this.cfg.maxQueueSize) {
499
- this.queue.splice(0, this.queue.length - this.cfg.maxQueueSize);
500
- }
501
- if (this.queue.length) {
502
- this.persistQueue();
503
- this.scheduleFlush();
504
- }
505
- }
506
- catch {
507
- // localStorage unavailable - pending in-memory events continue normally
508
- }
509
- }
510
- scheduleFlush() {
511
- if (this.flushTimer || isServerSide())
512
- return;
513
- let delayMs = this.cfg.flushInterval;
514
- if (this.consecutiveFailures) {
515
- delayMs = Math.min(this.cfg.flushInterval * 2 ** this.consecutiveFailures, this.cfg.maxRetryBackoff);
516
- delayMs = Math.max(delayMs, this.retryAfter);
517
- }
518
- this.flushTimer = setTimeout(() => {
519
- this.flushTimer = undefined;
520
- void this.flush();
521
- }, delayMs);
522
- }
523
- clearFlushTimer() {
524
- if (!this.flushTimer)
525
- return;
526
- clearTimeout(this.flushTimer);
527
- this.flushTimer = undefined;
528
- }
529
- removeFromQueue(batch) {
530
- const ids = new Set(batch.map(event => event.id));
531
- this.queue = this.queue.filter(event => !ids.has(event.id));
532
- this.persistQueue();
533
- }
534
- persistQueue() {
535
- if (!this.cfg.persistQueue)
536
- return;
537
- try {
538
- if (this.queue.length) {
539
- localStorage.setItem(this.queueKey, _safeJsonStringify(this.queue));
540
- }
541
- else {
542
- localStorage.removeItem(this.queueKey);
543
- }
544
- }
545
- catch {
546
- // localStorage unavailable/full - analytics must never break the app
547
- }
548
- }
549
- get queueKey() {
550
- return `${this.cfg.localStorageKeyPrefix}.q.${this.tabId}`;
551
- }
552
- }
33
+ * Self-hosted analytics event client.
34
+ *
35
+ * Captures client-side analytics events, adds browser page properties, batches them, retries
36
+ * transient failures and delivers them to our own Backend via a single REST endpoint.
37
+ */
38
+ var AnalyticsClient = class {
39
+ constructor(cfg) {
40
+ const localStorageKeyPrefix = cfg.localStorageKeyPrefix || "nca";
41
+ this.cfg = {
42
+ onError: _noop,
43
+ firstTouchUrl: "",
44
+ isEnabled: () => true,
45
+ getCommonProps: () => ({}),
46
+ flushInterval: 5e3,
47
+ maxBatchSize: 50,
48
+ maxBatchBytes: 6e4,
49
+ maxQueueSize: 1e3,
50
+ maxRetryBackoff: 6e5,
51
+ requestTimeout: 3e4,
52
+ persistQueue: true,
53
+ maxPersistedAge: 864e5,
54
+ localStorageKeyPrefix,
55
+ logger: console,
56
+ debug: false,
57
+ ...cfg
58
+ };
59
+ this.identity = new AnalyticsIdentity(this.cfg.identity);
60
+ this.firstTouchFetcher = getFetcher({
61
+ logger: this.cfg.logger,
62
+ retryPost: true,
63
+ timeoutSeconds: 10
64
+ });
65
+ if (isServerSide()) return;
66
+ this.restoreOrphanedQueues();
67
+ globalThis.addEventListener("pagehide", this.handlePageHide);
68
+ document.addEventListener("visibilitychange", this.handleVisibilityChange);
69
+ }
70
+ cfg;
71
+ firstTouchFetcher;
72
+ /**
73
+ * The built-in identity, constructed from `cfg.identity`. Also readable by application code,
74
+ * e.g to feed the distinctId or acquisition props to other integrations.
75
+ */
76
+ identity;
77
+ /**
78
+ * Random id of this AnalyticsClient instance (in practice - of this tab/pageload),
79
+ * used to namespace the localStorage queue snapshot.
80
+ */
81
+ tabId = nanoidBrowser(10);
82
+ queue = [];
83
+ flushTimer;
84
+ isFlushing = false;
85
+ consecutiveFailures = 0;
86
+ /**
87
+ * Requested by the server via `Retry-After` header (already converted to ms).
88
+ */
89
+ retryAfter = 0;
90
+ hasUpdatedAcquisitionProps = false;
91
+ eventListeners = /* @__PURE__ */ new Set();
92
+ handlePageHide = () => {
93
+ this.flushNow();
94
+ };
95
+ handleVisibilityChange = () => {
96
+ if (document.visibilityState === "hidden") this.flushNow();
97
+ };
98
+ /**
99
+ * Optional eager bootstrapping, to call once at app boot: registers the acquisition props
100
+ * into the identity entry while the landing url/referrer are still current - the moment
101
+ * an SPA navigation can strip the utm params off the url before the first event fires. Gated by cfg.isEnabled, like the events themselves.
102
+ * Without it the registration happens lazily before the first enabled event.
103
+ */
104
+ init() {
105
+ if (!this.canTrack()) return;
106
+ this.ensureAcquisitionProps();
107
+ this.replayFromStub();
108
+ }
109
+ track(name, props) {
110
+ const event = this.enqueue(name, props);
111
+ if (event) this.notifyEventListeners(event);
112
+ }
113
+ /**
114
+ * Replays the calls a stub recorded on the page before this client loaded, under the timestamps
115
+ * they were made at. Tracked in order, so calls made before the stub's `identify()` keep the
116
+ * anonymous identity.
117
+ */
118
+ replayFromStub() {
119
+ const stub = globalThis.analyticsClient;
120
+ if (!stub || !("q" in stub)) return;
121
+ for (const call of stub.q) try {
122
+ if (call.method === "identify") this.identify(call.args[0]);
123
+ else {
124
+ const event = this.enqueue(call.args[0], call.args[1], call.ts);
125
+ if (event) this.notifyEventListeners(event);
126
+ }
127
+ } catch (err) {
128
+ this.cfg.logger.warn("[analytics] could not replay a stubbed call", err);
129
+ }
130
+ stub.q.length = 0;
131
+ }
132
+ /**
133
+ * Registers a listener called synchronously for every delivered event, past the gates in
134
+ * `track()`. Returns an unsubscribe function. Listeners receive a snapshot and cannot affect
135
+ * delivery, and a throwing listener never breaks tracking.
136
+ */
137
+ onEvent(listener) {
138
+ this.eventListeners.add(listener);
139
+ return () => this.eventListeners.delete(listener);
140
+ }
141
+ notifyEventListeners(event) {
142
+ if (!this.eventListeners.size) return;
143
+ const distinctId = event.userId || this.identity.getDistinctId();
144
+ const snapshot = {
145
+ ...event,
146
+ props: { ...event.props }
147
+ };
148
+ for (const listener of this.eventListeners) try {
149
+ listener(snapshot, distinctId);
150
+ } catch (err) {
151
+ this.cfg.logger.warn("[analytics] onEvent listener threw", err);
152
+ }
153
+ }
154
+ /**
155
+ * Sets the distinctId going forward (persisted), e.g after a successful signup.
156
+ * Pending events are kept, each under the identity it was tracked with.
157
+ */
158
+ identify(userId) {
159
+ if (!this.canTrack()) return;
160
+ const previousDistinctId = this.identity.getDistinctId();
161
+ this.identity.identify(userId);
162
+ if (previousDistinctId === userId) return;
163
+ this.enqueue(ANALYTICS_IDENTIFY_EVENT_NAME, { anon_distinct_id: previousDistinctId });
164
+ this.flush();
165
+ this.sendFirstTouch();
166
+ }
167
+ enqueue(name, props, ts) {
168
+ if (!this.canTrack()) return;
169
+ this.ensureAcquisitionProps();
170
+ const event = {
171
+ id: generateEventId(),
172
+ name,
173
+ ts: ts || Date.now(),
174
+ props: {
175
+ ...this.getDefaultProps(),
176
+ ...this.cfg.getCommonProps(),
177
+ ...props
178
+ },
179
+ userId: this.identity.getDistinctId()
180
+ };
181
+ if (this.cfg.debug) this.cfg.logger.log(`[analytics] ${event.name}`, event.props);
182
+ this.queue.push(event);
183
+ if (this.queue.length > this.cfg.maxQueueSize) {
184
+ this.queue.shift();
185
+ this.cfg.logger.warn("[analytics] queue overflow, dropped the oldest event");
186
+ }
187
+ this.persistQueue();
188
+ if (this.queue.length >= this.cfg.maxBatchSize && !this.consecutiveFailures) this.flush();
189
+ else this.scheduleFlush();
190
+ return event;
191
+ }
192
+ canTrack() {
193
+ return !isServerSide() && this.cfg.isEnabled();
194
+ }
195
+ /**
196
+ * Clears pending events and the persisted identity entry, including its acquisition
197
+ * properties. A new identity is generated on next use.
198
+ */
199
+ reset() {
200
+ this.clearPendingEvents();
201
+ this.identity.reset();
202
+ this.hasUpdatedAcquisitionProps = false;
203
+ }
204
+ /**
205
+ * Drains the queue, one batch per request.
206
+ * Called automatically (flush interval / batch size / pagehide) - public for manual flushing.
207
+ */
208
+ async flush() {
209
+ if (isServerSide() || this.isFlushing) return;
210
+ this.isFlushing = true;
211
+ this.clearFlushTimer();
212
+ try {
213
+ while (this.queue.length) for (const batch of this.prepareBatches()) {
214
+ if (await this.sendBatch(batch) === "retry") {
215
+ this.consecutiveFailures++;
216
+ this.scheduleFlush();
217
+ return;
218
+ }
219
+ this.removeFromQueue(batch.events);
220
+ this.consecutiveFailures = 0;
221
+ this.retryAfter = 0;
222
+ }
223
+ } finally {
224
+ this.isFlushing = false;
225
+ }
226
+ }
227
+ /**
228
+ * Immediate flush: hands queued events over to the browser via keepalive fetch.
229
+ * Runs automatically on pagehide / visibilitychange:hidden. Public so that app code can
230
+ * hand events over right away: events tracked in its own page-lifecycle handlers (which
231
+ * run after this client's own), and events tracked right before a navigation (e.g a
232
+ * cta click) - unload-time delivery is best-effort and can be lost, while a
233
+ * keepalive request from a still-alive page survives the navigation.
234
+ * The persisted queue remains untouched because keepalive requests cannot reliably process
235
+ * their response; a later regular flush re-sends and deduplicates the events.
236
+ */
237
+ flushNow() {
238
+ if (!this.queue.length) return;
239
+ this.clearFlushTimer();
240
+ for (const batch of this.prepareBatches()) this.postBatch(batch, true).catch((err) => {
241
+ this.cfg.logger.warn("[analytics] lifecycle batch failed to send; retained for retry", err);
242
+ });
243
+ this.scheduleFlush();
244
+ }
245
+ /**
246
+ * Removes listeners and pending timers. Only needed when an instance is discarded
247
+ * (e.g in tests or HMR) - the app-wide singleton never needs it.
248
+ */
249
+ destroy() {
250
+ this.clearFlushTimer();
251
+ if (isServerSide()) return;
252
+ globalThis.removeEventListener("pagehide", this.handlePageHide);
253
+ document.removeEventListener("visibilitychange", this.handleVisibilityChange);
254
+ }
255
+ /**
256
+ * Registers the acquisition props into the identity entry once per pageload, like
257
+ * eagerly from init(), or lazily before the first
258
+ * enabled event. Either path is gated by cfg.isEnabled, so bot/e2e/consent gating
259
+ * applies to the persistence write too.
260
+ */
261
+ ensureAcquisitionProps() {
262
+ if (this.hasUpdatedAcquisitionProps) return;
263
+ this.hasUpdatedAcquisitionProps = true;
264
+ this.identity.updateAcquisitionProps();
265
+ this.sendFirstTouch();
266
+ }
267
+ /**
268
+ * Posts the first-touch props to their own endpoint, once per pageload. Fire-and-forget:
269
+ * `$set_once` ignores every write after the first, so a lost or repeated call costs nothing.
270
+ */
271
+ async sendFirstTouch() {
272
+ const { firstTouchUrl } = this.cfg;
273
+ if (!firstTouchUrl) return;
274
+ const props = this.identity.getFirstTouchProps();
275
+ if (!props) return;
276
+ const input = {
277
+ clientId: this.cfg.clientId,
278
+ userId: this.identity.getDistinctId(),
279
+ props
280
+ };
281
+ const res = await this.firstTouchFetcher.doFetch({
282
+ url: firstTouchUrl,
283
+ method: "POST",
284
+ text: _safeJsonStringify(input),
285
+ responseType: "void"
286
+ });
287
+ if (!res.err) return;
288
+ try {
289
+ this.cfg.onError(_errorDataAppend(res.err, { firstTouch: true }));
290
+ } catch (err) {
291
+ this.cfg.logger.warn("[analytics] onError hook threw", err);
292
+ }
293
+ }
294
+ getDefaultProps() {
295
+ const referrer = document.referrer;
296
+ const url = new URL(globalThis.location.href);
297
+ return {
298
+ ...referrer && { referrer },
299
+ current_url: globalThis.location.href,
300
+ screen_height: globalThis.screen.height,
301
+ screen_width: globalThis.screen.width,
302
+ ...this.identity.getAcquisitionProps(),
303
+ ...getLastTouchUtms(url),
304
+ ...getQueryProperties(url, CLICK_QUERY_PARAMS)
305
+ };
306
+ }
307
+ clearPendingEvents() {
308
+ this.clearFlushTimer();
309
+ this.queue = [];
310
+ this.consecutiveFailures = 0;
311
+ this.retryAfter = 0;
312
+ this.persistQueue();
313
+ }
314
+ async sendBatch(batch) {
315
+ let res;
316
+ try {
317
+ res = await this.postBatch(batch, false, AbortSignal.timeout?.(this.cfg.requestTimeout));
318
+ } catch (err) {
319
+ this.cfg.logger.warn("[analytics] batch failed to send, will retry", err);
320
+ return "retry";
321
+ }
322
+ if (res.ok) return "ok";
323
+ if (res.status === 429 || res.status >= 500) {
324
+ const retryAfter = Number(res.headers.get("retry-after"));
325
+ if (retryAfter) this.retryAfter = retryAfter * 1e3;
326
+ this.cfg.logger.warn(`[analytics] batch rejected with ${res.status}, will retry`);
327
+ return "retry";
328
+ }
329
+ this.cfg.logger.error(`[analytics] batch rejected with ${res.status}, dropping ${batch.events.length} event(s)`);
330
+ try {
331
+ this.cfg.onError(new AppError("batch dropped on a non-retryable status", {
332
+ status: res.status,
333
+ eventCount: batch.events.length
334
+ }));
335
+ } catch (err) {
336
+ this.cfg.logger.warn("[analytics] onError hook threw", err);
337
+ }
338
+ return "drop";
339
+ }
340
+ async postBatch(batch, isLifecycleFlush, signal) {
341
+ return fetch(this.cfg.url, {
342
+ method: "POST",
343
+ headers: { "content-type": "text/plain" },
344
+ body: batch.body,
345
+ ...isLifecycleFlush && { keepalive: true },
346
+ signal
347
+ });
348
+ }
349
+ /**
350
+ * Splits the whole queue into request-ready batches of at most maxBatchSize events
351
+ * and maxBatchBytes serialized bytes each.
352
+ */
353
+ prepareBatches() {
354
+ const batches = [];
355
+ let current;
356
+ const userId = this.identity.getDistinctId();
357
+ for (const event of this.queue) {
358
+ const eventBytes = getUtf8ByteLength(_safeJsonStringify(event));
359
+ const hasReachedCount = current?.events.length === this.cfg.maxBatchSize;
360
+ const separatorBytes = current?.events.length ? 1 : 0;
361
+ const hasReachedBytes = !!current && current.bodyBytes + separatorBytes + eventBytes > this.cfg.maxBatchBytes;
362
+ if (current && (hasReachedCount || hasReachedBytes)) {
363
+ batches.push(this.finalizeBatch(current));
364
+ current = void 0;
365
+ }
366
+ current ||= this.createPendingBatch(userId);
367
+ const nextSeparatorBytes = current.events.length ? 1 : 0;
368
+ current.events.push(event);
369
+ current.bodyBytes += nextSeparatorBytes + eventBytes;
370
+ }
371
+ if (current?.events.length) batches.push(this.finalizeBatch(current));
372
+ return batches;
373
+ }
374
+ createPendingBatch(userId) {
375
+ const sentAt = Date.now();
376
+ const emptyBody = {
377
+ sentAt,
378
+ clientId: this.cfg.clientId,
379
+ userId,
380
+ events: []
381
+ };
382
+ return {
383
+ sentAt,
384
+ userId,
385
+ events: [],
386
+ bodyBytes: getUtf8ByteLength(JSON.stringify(emptyBody))
387
+ };
388
+ }
389
+ finalizeBatch(batch) {
390
+ const input = {
391
+ sentAt: batch.sentAt,
392
+ clientId: this.cfg.clientId,
393
+ userId: batch.userId,
394
+ events: batch.events
395
+ };
396
+ return {
397
+ events: batch.events,
398
+ body: _safeJsonStringify(input)
399
+ };
400
+ }
401
+ /**
402
+ * Adopts queue snapshots persisted by previous pageloads (crashed/killed tabs) and re-sends them.
403
+ * A snapshot of a still-alive tab may be adopted too - the resulting duplicate delivery
404
+ * is deduped by the Backend via event ids.
405
+ */
406
+ restoreOrphanedQueues() {
407
+ if (!this.cfg.persistQueue) return;
408
+ try {
409
+ const prefix = `${this.cfg.localStorageKeyPrefix}.q.`;
410
+ const keys = [];
411
+ for (let i = 0; i < localStorage.length; i++) {
412
+ const key = localStorage.key(i);
413
+ if (key?.startsWith(prefix)) keys.push(key);
414
+ }
415
+ if (!keys.length) return;
416
+ const minTs = Date.now() - this.cfg.maxPersistedAge;
417
+ for (const key of keys) try {
418
+ const events = JSON.parse(localStorage.getItem(key) || "[]");
419
+ localStorage.removeItem(key);
420
+ this.queue.push(...events.filter((event) => event.ts >= minTs));
421
+ } catch {
422
+ localStorage.removeItem(key);
423
+ }
424
+ if (this.queue.length > this.cfg.maxQueueSize) this.queue.splice(0, this.queue.length - this.cfg.maxQueueSize);
425
+ if (this.queue.length) {
426
+ this.persistQueue();
427
+ this.scheduleFlush();
428
+ }
429
+ } catch {}
430
+ }
431
+ scheduleFlush() {
432
+ if (this.flushTimer || isServerSide()) return;
433
+ let delayMs = this.cfg.flushInterval;
434
+ if (this.consecutiveFailures) {
435
+ delayMs = Math.min(this.cfg.flushInterval * 2 ** this.consecutiveFailures, this.cfg.maxRetryBackoff);
436
+ delayMs = Math.max(delayMs, this.retryAfter);
437
+ }
438
+ this.flushTimer = setTimeout(() => {
439
+ this.flushTimer = void 0;
440
+ this.flush();
441
+ }, delayMs);
442
+ }
443
+ clearFlushTimer() {
444
+ if (!this.flushTimer) return;
445
+ clearTimeout(this.flushTimer);
446
+ this.flushTimer = void 0;
447
+ }
448
+ removeFromQueue(batch) {
449
+ const ids = new Set(batch.map((event) => event.id));
450
+ this.queue = this.queue.filter((event) => !ids.has(event.id));
451
+ this.persistQueue();
452
+ }
453
+ persistQueue() {
454
+ if (!this.cfg.persistQueue) return;
455
+ try {
456
+ if (this.queue.length) localStorage.setItem(this.queueKey, _safeJsonStringify(this.queue));
457
+ else localStorage.removeItem(this.queueKey);
458
+ } catch {}
459
+ }
460
+ get queueKey() {
461
+ return `${this.cfg.localStorageKeyPrefix}.q.${this.tabId}`;
462
+ }
463
+ };
553
464
  /**
554
- * Distinct-id generation and persistence:
555
- *
556
- * - a new identity is a UUID v4 device id, stored as `$device_id`,
557
- * with `distinct_id` derived from it by `generateDistinctId`
558
- * - `identify(userId)` sets `distinct_id` and `user_id`, keeping `$device_id` (same person)
559
- * - `reset()` clears the whole entry and generates a fresh anonymous identity (unrelated person)
560
- * - the identity is stored as a JSON object under a single cookie / localStorage name
561
- *
562
- * Apps sharing a persistenceName and cookie domain read and write the same identity:
563
- * whichever writes first, the others adopt it. Properties they keep in the same entry
564
- * are preserved on write, never interpreted.
565
- *
566
- * Storage failures (SSR, blocked cookies/localStorage) degrade to an in-memory session-scoped
567
- * identity - analytics must never break the app.
568
- */
569
- export class AnalyticsIdentity {
570
- constructor(cfg) {
571
- this.cfg = {
572
- onError: _noop,
573
- cookieDomain: '',
574
- expireDays: 365,
575
- secureCookie: false,
576
- // TODO: make it emit something else by default, like the device id itself
577
- generateDistinctId: deviceId => `$device:${deviceId}`,
578
- ...cfg,
579
- };
580
- }
581
- cfg;
582
- /**
583
- * Fallback identity for when storage is unavailable, and the last-known-good copy
584
- * if storage becomes unreadable later.
585
- */
586
- memoryEntry = {};
587
- hasRefreshedExpiry = false;
588
- getDistinctId() {
589
- return this.ensureIdentity().distinct_id;
590
- }
591
- /**
592
- * The bare (unprefixed) device UUID. Undefined for legacy identities persisted before
593
- * a device id was stored (their `distinct_id` has no device prefix either).
594
- */
595
- getDeviceId() {
596
- return this.ensureIdentity().$device_id;
597
- }
598
- /**
599
- * Reads any property of the persisted entry: the acquisition props maintained by
600
- * updateAcquisitionProps(), or props written by another app sharing the entry.
601
- */
602
- getProperty(key) {
603
- return this.loadEntry()[key];
604
- }
605
- /**
606
- * The acquisition props that can't be derived from event payloads, and are stored separately.
607
- */
608
- getAcquisitionProps() {
609
- const entry = this.loadEntry();
610
- const props = _filterObject(entry, k => String(k).startsWith(UTM_QUERY_PARAM_PREFIX));
611
- const { firstTouch } = entry;
612
- if (firstTouch) {
613
- props['initial_referrer'] = firstTouch.referrer;
614
- }
615
- return props;
616
- }
617
- /** The captured first touch, as the props to `$set_once` on the profile. */
618
- getFirstTouchProps() {
619
- const { firstTouch } = this.loadEntry();
620
- if (!firstTouch)
621
- return;
622
- return { ...firstTouch.utms, initial_referrer: firstTouch.referrer };
623
- }
624
- /**
625
- * Sets the identity going forward, e.g after a successful signup/login.
626
- * `$device_id` is kept, so the destination can merge the pre-identify
627
- * anonymous events into the same user.
628
- */
629
- identify(userId) {
630
- const entry = this.ensureIdentity();
631
- // Identities persisted before a device id was stored adopt the previous
632
- // distinct_id as their device id
633
- entry.$device_id ||= entry.distinct_id;
634
- entry.user_id = userId;
635
- entry.distinct_id = entry.user_id;
636
- this.saveEntry(entry);
637
- }
638
- /**
639
- * Clears the whole persisted entry and generates a fresh anonymous identity.
640
- * Call only when switching to an UNRELATED identity (e.g logout): everything else stored
641
- * in the entry may belong to the previous user, so it goes too.
642
- */
643
- reset() {
644
- const deviceId = crypto.randomUUID();
645
- this.saveEntry({
646
- distinct_id: this.cfg.generateDistinctId(deviceId),
647
- $device_id: deviceId,
648
- });
649
- }
650
- /** Collects properties of the user that can be used to attribute traffic. */
651
- updateAcquisitionProps() {
652
- if (isServerSide())
653
- return;
654
- const entry = this.loadEntry();
655
- const url = new URL(globalThis.location.href);
656
- Object.assign(entry, getLastTouchUtms(url));
657
- entry.firstTouch ||= { referrer: getReferrer() };
658
- entry.firstTouch.utms ||= getFirstTouchUtms(url);
659
- this.saveEntry(entry);
660
- }
661
- ensureIdentity() {
662
- const entry = this.loadEntry();
663
- if (entry.distinct_id) {
664
- this.refreshExpiry(entry);
665
- }
666
- else {
667
- const deviceId = crypto.randomUUID();
668
- entry.distinct_id = this.cfg.generateDistinctId(deviceId);
669
- entry.$device_id = deviceId;
670
- this.saveEntry(entry);
671
- }
672
- return entry;
673
- }
674
- /**
675
- * The cookie expiration window slides: expireDays counts from the LAST visit, not the
676
- * first. Re-saved once per instance (in practice - once per pageload).
677
- */
678
- refreshExpiry(entry) {
679
- if (this.hasRefreshedExpiry || this.cfg.persistence !== 'cookie')
680
- return;
681
- this.saveEntry(entry);
682
- }
683
- loadEntry() {
684
- try {
685
- const raw = this.cfg.persistence === 'cookie'
686
- ? getCookie(this.cfg.persistenceKey)
687
- : localStorage.getItem(this.cfg.persistenceKey);
688
- if (raw)
689
- this.memoryEntry = JSON.parse(raw);
690
- }
691
- catch {
692
- // Storage unavailable (SSR, blocked) or corrupted JSON - keep the in-memory copy
693
- }
694
- return this.memoryEntry;
695
- }
696
- saveEntry(entry) {
697
- this.memoryEntry = entry;
698
- this.hasRefreshedExpiry = true;
699
- // Stays 0 when the failure came before serializing, which distinguishes it from a rejected write
700
- let bytes = 0;
701
- try {
702
- const value = JSON.stringify(entry);
703
- bytes = value.length;
704
- if (this.cfg.persistence === 'cookie') {
705
- bytes = this.cookieLength(entry);
706
- if (bytes > MAX_COOKIE_LENGTH) {
707
- // Writing it would make the browser drop the cookie, and the persisted identity
708
- // with it. The last good cookie is kept instead, and this entry stays in memory.
709
- this.reportError(new AnalyticsClientError('cookie exceeded the length limit', { bytes }));
710
- return;
711
- }
712
- setCookie(this.cfg.persistenceKey, value, this.cfg.expireDays, this.cfg.cookieDomain, this.cfg.secureCookie);
713
- // `document.cookie` swallows a rejected write (cookies disabled, a domain the page is
714
- // not allowed to set, ITP), so reading it back is the only way to notice. A leftover
715
- // cookie of the same name can shadow the one just written, so any match counts.
716
- const persisted = getCookieValues(this.cfg.persistenceKey);
717
- if (!persisted.includes(value)) {
718
- this.reportError(new AnalyticsClientError('cookie write did not persist', {
719
- bytes,
720
- hadPreviousCookie: persisted.length > 0,
721
- }));
722
- }
723
- }
724
- else {
725
- localStorage.setItem(this.cfg.persistenceKey, value);
726
- }
727
- }
728
- catch (err) {
729
- // The identity stays session-scoped in memory
730
- this.reportError(_errorDataAppend(err, { bytes }));
731
- }
732
- }
733
- cookieLength(entry) {
734
- return this.cfg.persistenceKey.length + encodeURIComponent(JSON.stringify(entry)).length;
735
- }
736
- reportError(err) {
737
- try {
738
- this.cfg.onError(err);
739
- }
740
- catch {
741
- // A consumer hook must not break identity updates
742
- }
743
- }
744
- }
465
+ * Distinct-id generation and persistence:
466
+ *
467
+ * - a new identity is a UUID v4 device id, stored as `$device_id`,
468
+ * with `distinct_id` derived from it by `generateDistinctId`
469
+ * - `identify(userId)` sets `distinct_id` and `user_id`, keeping `$device_id` (same person)
470
+ * - `reset()` clears the whole entry and generates a fresh anonymous identity (unrelated person)
471
+ * - the identity is stored as a JSON object under a single cookie / localStorage name
472
+ *
473
+ * Apps sharing a persistenceName and cookie domain read and write the same identity:
474
+ * whichever writes first, the others adopt it. Properties they keep in the same entry
475
+ * are preserved on write, never interpreted.
476
+ *
477
+ * Storage failures (SSR, blocked cookies/localStorage) degrade to an in-memory session-scoped
478
+ * identity - analytics must never break the app.
479
+ */
480
+ var AnalyticsIdentity = class {
481
+ constructor(cfg) {
482
+ this.cfg = {
483
+ onError: _noop,
484
+ cookieDomain: "",
485
+ expireDays: 365,
486
+ secureCookie: false,
487
+ generateDistinctId: (deviceId) => `$device:${deviceId}`,
488
+ ...cfg
489
+ };
490
+ }
491
+ cfg;
492
+ /**
493
+ * Fallback identity for when storage is unavailable, and the last-known-good copy
494
+ * if storage becomes unreadable later.
495
+ */
496
+ memoryEntry = {};
497
+ hasRefreshedExpiry = false;
498
+ getDistinctId() {
499
+ return this.ensureIdentity().distinct_id;
500
+ }
501
+ /**
502
+ * The bare (unprefixed) device UUID. Undefined for legacy identities persisted before
503
+ * a device id was stored (their `distinct_id` has no device prefix either).
504
+ */
505
+ getDeviceId() {
506
+ return this.ensureIdentity().$device_id;
507
+ }
508
+ /**
509
+ * Reads any property of the persisted entry: the acquisition props maintained by
510
+ * updateAcquisitionProps(), or props written by another app sharing the entry.
511
+ */
512
+ getProperty(key) {
513
+ return this.loadEntry()[key];
514
+ }
515
+ /**
516
+ * The acquisition props that can't be derived from event payloads, and are stored separately.
517
+ */
518
+ getAcquisitionProps() {
519
+ const entry = this.loadEntry();
520
+ const props = _filterObject(entry, (k) => String(k).startsWith(UTM_QUERY_PARAM_PREFIX));
521
+ const { firstTouch } = entry;
522
+ if (firstTouch) props["initial_referrer"] = firstTouch.referrer;
523
+ return props;
524
+ }
525
+ /** The captured first touch, as the props to `$set_once` on the profile. */
526
+ getFirstTouchProps() {
527
+ const { firstTouch } = this.loadEntry();
528
+ if (!firstTouch) return;
529
+ return {
530
+ ...firstTouch.utms,
531
+ initial_referrer: firstTouch.referrer
532
+ };
533
+ }
534
+ /**
535
+ * Sets the identity going forward, e.g after a successful signup/login.
536
+ * `$device_id` is kept, so the destination can merge the pre-identify
537
+ * anonymous events into the same user.
538
+ */
539
+ identify(userId) {
540
+ const entry = this.ensureIdentity();
541
+ entry.$device_id ||= entry.distinct_id;
542
+ entry.user_id = userId;
543
+ entry.distinct_id = entry.user_id;
544
+ this.saveEntry(entry);
545
+ }
546
+ /**
547
+ * Clears the whole persisted entry and generates a fresh anonymous identity.
548
+ * Call only when switching to an UNRELATED identity (e.g logout): everything else stored
549
+ * in the entry may belong to the previous user, so it goes too.
550
+ */
551
+ reset() {
552
+ const deviceId = crypto.randomUUID();
553
+ this.saveEntry({
554
+ distinct_id: this.cfg.generateDistinctId(deviceId),
555
+ $device_id: deviceId
556
+ });
557
+ }
558
+ /** Collects properties of the user that can be used to attribute traffic. */
559
+ updateAcquisitionProps() {
560
+ if (isServerSide()) return;
561
+ const entry = this.loadEntry();
562
+ const url = new URL(globalThis.location.href);
563
+ Object.assign(entry, getLastTouchUtms(url));
564
+ entry.firstTouch ||= { referrer: getReferrer() };
565
+ entry.firstTouch.utms ||= getFirstTouchUtms(url);
566
+ this.saveEntry(entry);
567
+ }
568
+ ensureIdentity() {
569
+ const entry = this.loadEntry();
570
+ if (entry.distinct_id) this.refreshExpiry(entry);
571
+ else {
572
+ const deviceId = crypto.randomUUID();
573
+ entry.distinct_id = this.cfg.generateDistinctId(deviceId);
574
+ entry.$device_id = deviceId;
575
+ this.saveEntry(entry);
576
+ }
577
+ return entry;
578
+ }
579
+ /**
580
+ * The cookie expiration window slides: expireDays counts from the LAST visit, not the
581
+ * first. Re-saved once per instance (in practice - once per pageload).
582
+ */
583
+ refreshExpiry(entry) {
584
+ if (this.hasRefreshedExpiry || this.cfg.persistence !== "cookie") return;
585
+ this.saveEntry(entry);
586
+ }
587
+ loadEntry() {
588
+ try {
589
+ const raw = this.cfg.persistence === "cookie" ? getCookie(this.cfg.persistenceKey) : localStorage.getItem(this.cfg.persistenceKey);
590
+ if (raw) this.memoryEntry = JSON.parse(raw);
591
+ } catch {}
592
+ return this.memoryEntry;
593
+ }
594
+ saveEntry(entry) {
595
+ this.memoryEntry = entry;
596
+ this.hasRefreshedExpiry = true;
597
+ let bytes = 0;
598
+ try {
599
+ const value = JSON.stringify(entry);
600
+ bytes = value.length;
601
+ if (this.cfg.persistence === "cookie") {
602
+ bytes = this.cookieLength(entry);
603
+ if (bytes > MAX_COOKIE_LENGTH) {
604
+ this.reportError(new AnalyticsClientError("cookie exceeded the length limit", { bytes }));
605
+ return;
606
+ }
607
+ setCookie(this.cfg.persistenceKey, value, this.cfg.expireDays, this.cfg.cookieDomain, this.cfg.secureCookie);
608
+ const persisted = getCookieValues(this.cfg.persistenceKey);
609
+ if (!persisted.includes(value)) this.reportError(new AnalyticsClientError("cookie write did not persist", {
610
+ bytes,
611
+ hadPreviousCookie: persisted.length > 0
612
+ }));
613
+ } else localStorage.setItem(this.cfg.persistenceKey, value);
614
+ } catch (err) {
615
+ this.reportError(_errorDataAppend(err, { bytes }));
616
+ }
617
+ }
618
+ cookieLength(entry) {
619
+ return this.cfg.persistenceKey.length + encodeURIComponent(JSON.stringify(entry)).length;
620
+ }
621
+ reportError(err) {
622
+ try {
623
+ this.cfg.onError(err);
624
+ } catch {}
625
+ }
626
+ };
745
627
  /** The non-empty canonical `utm_*` params of the given url. */
746
628
  function getLastTouchUtms(url) {
747
- const props = {};
748
- for (const param of CANONICAL_UTM_PARAMS) {
749
- const value = url.searchParams.get(param);
750
- if (value)
751
- props[param] = truncateAnalyticsProperty(value);
752
- }
753
- return props;
629
+ const props = {};
630
+ for (const param of CANONICAL_UTM_PARAMS) {
631
+ const value = url.searchParams.get(param);
632
+ if (value) props[param] = truncateAnalyticsProperty(value);
633
+ }
634
+ return props;
754
635
  }
755
636
  /** The referrer of this pageload, or null when it has none (a direct visit). */
756
637
  function getReferrer() {
757
- return truncateAnalyticsProperty(document.referrer) || null;
638
+ return truncateAnalyticsProperty(document.referrer) || null;
758
639
  }
759
640
  /** Captures all utms if any are set, otherwise none */
760
641
  function getFirstTouchUtms(url) {
761
- // The absent ones are stored as null, so a later campaign cannot fill in the gaps
762
- const utms = _mapToObject(CANONICAL_UTM_PARAMS, param => {
763
- const value = url.searchParams.get(param);
764
- return [`initial_${param}`, value ? truncateAnalyticsProperty(value) : null];
765
- });
766
- if (_isEmptyObject(_filterNullishValues(utms)))
767
- return;
768
- return utms;
642
+ const utms = _mapToObject(CANONICAL_UTM_PARAMS, (param) => {
643
+ const value = url.searchParams.get(param);
644
+ return [`initial_${param}`, value ? truncateAnalyticsProperty(value) : null];
645
+ });
646
+ if (_isEmptyObject(_filterNullishValues(utms))) return;
647
+ return utms;
769
648
  }
770
649
  function getQueryProperties(url, keys) {
771
- return Object.fromEntries(keys.flatMap(key => {
772
- const value = url.searchParams.get(key);
773
- return value ? [[key, value]] : [];
774
- }));
650
+ return Object.fromEntries(keys.flatMap((key) => {
651
+ const value = url.searchParams.get(key);
652
+ return value ? [[key, value]] : [];
653
+ }));
775
654
  }
776
- // Shared instance: runs once per event per batch preparation (not per property).
777
- // we measure to stay under the fetch keepalive body quota.
778
- const textEncoder = new TextEncoder();
655
+ var textEncoder = new TextEncoder();
779
656
  function getUtf8ByteLength(value) {
780
- return textEncoder.encode(value).byteLength;
657
+ return textEncoder.encode(value).byteLength;
781
658
  }
782
659
  /** The first visible value under this name, or null. */
783
- export function getCookie(name) {
784
- if (!name)
785
- return null;
786
- return getCookieValues(name)[0] ?? null;
660
+ function getCookie(name) {
661
+ if (!name) return null;
662
+ return getCookieValues(name)[0] ?? null;
787
663
  }
788
664
  /**
789
- * Every visible value under this name, in `document.cookie` order. A host-only and a
790
- * parent-domain cookie with the same name and path coexist as separate cookies.
791
- */
665
+ * Every visible value under this name, in `document.cookie` order. A host-only and a
666
+ * parent-domain cookie with the same name and path coexist as separate cookies.
667
+ */
792
668
  function getCookieValues(name) {
793
- const nameEq = `${name}=`;
794
- const values = [];
795
- for (let c of document.cookie.split(';')) {
796
- while (c.startsWith(' '))
797
- c = c.slice(1);
798
- if (!c.startsWith(nameEq))
799
- continue;
800
- values.push(decodeURIComponent(c.slice(nameEq.length)));
801
- }
802
- return values;
669
+ const nameEq = `${name}=`;
670
+ const values = [];
671
+ for (let c of document.cookie.split(";")) {
672
+ while (c.startsWith(" ")) c = c.slice(1);
673
+ if (!c.startsWith(nameEq)) continue;
674
+ values.push(decodeURIComponent(c.slice(nameEq.length)));
675
+ }
676
+ return values;
803
677
  }
804
678
  /**
805
- * Writes the cookie and returns what was written, which the tests assert on.
806
- * `domain` is explicit, e.g `.example.com` to share it across subdomains.
807
- */
808
- export function setCookie(name, value, days, domain, isSecure) {
809
- const attrs = [`${name}=${encodeURIComponent(value)}`];
810
- if (days) {
811
- attrs.push(`expires=${new Date(Date.now() + days * 24 * 3600 * 1000).toUTCString()}`);
812
- }
813
- attrs.push('path=/');
814
- if (domain)
815
- attrs.push(`domain=${domain}`);
816
- if (isSecure)
817
- attrs.push('secure');
818
- const cookie = attrs.join('; ');
819
- // The Cookie Store API is async and missing in Safari, so this stays synchronous
820
- // oxlint-disable-next-line unicorn/no-document-cookie
821
- document.cookie = cookie;
822
- return cookie;
823
- }
824
- export class AnalyticsClientError extends AppError {
825
- constructor(message, data) {
826
- super(message, data, { name: 'AnalyticsClientError' });
827
- }
679
+ * Writes the cookie and returns what was written, which the tests assert on.
680
+ * `domain` is explicit, e.g `.example.com` to share it across subdomains.
681
+ */
682
+ function setCookie(name, value, days, domain, isSecure) {
683
+ const attrs = [`${name}=${encodeURIComponent(value)}`];
684
+ if (days) attrs.push(`expires=${new Date(Date.now() + days * 24 * 3600 * 1e3).toUTCString()}`);
685
+ attrs.push("path=/");
686
+ if (domain) attrs.push(`domain=${domain}`);
687
+ if (isSecure) attrs.push("secure");
688
+ const cookie = attrs.join("; ");
689
+ document.cookie = cookie;
690
+ return cookie;
828
691
  }
692
+ var AnalyticsClientError = class extends AppError {
693
+ constructor(message, data) {
694
+ super(message, data, { name: "AnalyticsClientError" });
695
+ }
696
+ };
697
+ //#endregion
698
+ export { AnalyticsClient, AnalyticsClientError, AnalyticsIdentity, getCookie, setCookie };