@posthog/browser 0.1.1 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (64) hide show
  1. package/ARCHITECTURE.md +28 -22
  2. package/README.md +65 -29
  3. package/dist/analytics-api.d.ts +26 -0
  4. package/dist/analytics-api.js +18 -0
  5. package/dist/analytics-api.mjs +1 -0
  6. package/dist/analytics-buffer.d.ts +3 -3
  7. package/dist/analytics-buffer.js +2 -161
  8. package/dist/analytics-buffer.mjs +2 -161
  9. package/dist/analytics-extension.d.ts +76 -0
  10. package/dist/analytics-extension.js +589 -0
  11. package/dist/analytics-extension.mjs +551 -0
  12. package/dist/analytics-internal.d.ts +32 -18
  13. package/dist/analytics-options.d.ts +7 -2
  14. package/dist/analytics-options.js +38 -1
  15. package/dist/analytics-options.mjs +17 -1
  16. package/dist/analytics.d.ts +4 -3
  17. package/dist/autocapture.d.ts +2 -2
  18. package/dist/autocapture.js +21 -10
  19. package/dist/autocapture.mjs +21 -10
  20. package/dist/automatic-analytics.d.ts +2 -2
  21. package/dist/browser-client.d.ts +44 -4
  22. package/dist/capture-properties.d.ts +9 -0
  23. package/dist/capture-properties.js +68 -0
  24. package/dist/capture-properties.mjs +30 -0
  25. package/dist/capture-v1.js +7 -19
  26. package/dist/capture-v1.mjs +1 -13
  27. package/dist/core.d.ts +7 -5
  28. package/dist/extensions/registry.d.ts +42 -7
  29. package/dist/extensions/registry.js +194 -37
  30. package/dist/extensions/registry.mjs +194 -37
  31. package/dist/flags-token.d.ts +4 -4
  32. package/dist/flags.js +15 -2
  33. package/dist/flags.mjs +15 -2
  34. package/dist/immutable.d.ts +1 -0
  35. package/dist/immutable.js +46 -0
  36. package/dist/immutable.mjs +8 -0
  37. package/dist/index.d.ts +7 -5
  38. package/dist/index.js +3 -67
  39. package/dist/index.mjs +3 -67
  40. package/dist/logs-internal.d.ts +2 -2
  41. package/dist/logs.js +5 -2
  42. package/dist/logs.mjs +5 -2
  43. package/dist/person-processing.d.ts +11 -0
  44. package/dist/person-processing.js +84 -0
  45. package/dist/person-processing.mjs +37 -0
  46. package/dist/posthog.d.ts +4 -3
  47. package/dist/posthog.js +153 -353
  48. package/dist/posthog.mjs +153 -353
  49. package/dist/root-composition.d.ts +11 -0
  50. package/dist/root-composition.js +138 -0
  51. package/dist/root-composition.mjs +100 -0
  52. package/dist/state.d.ts +1 -0
  53. package/dist/state.js +4 -0
  54. package/dist/state.mjs +4 -0
  55. package/dist/surveys-extension.js +11 -6
  56. package/dist/surveys-extension.mjs +11 -6
  57. package/dist/surveys-internal.d.ts +3 -2
  58. package/dist/types.d.ts +13 -19
  59. package/dist/utf8.d.ts +1 -0
  60. package/dist/utf8.js +52 -0
  61. package/dist/utf8.mjs +14 -0
  62. package/dist/version.js +1 -1
  63. package/dist/version.mjs +1 -1
  64. package/package.json +5 -5
@@ -0,0 +1,589 @@
1
+ "use strict";
2
+ var __webpack_require__ = {};
3
+ (()=>{
4
+ __webpack_require__.d = (exports1, getters, values)=>{
5
+ var define = (defs, kind)=>{
6
+ for(var key in defs)if (__webpack_require__.o(defs, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
7
+ enumerable: true,
8
+ [kind]: defs[key]
9
+ });
10
+ };
11
+ define(getters, "get");
12
+ define(values, "value");
13
+ };
14
+ })();
15
+ (()=>{
16
+ __webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
17
+ })();
18
+ (()=>{
19
+ __webpack_require__.r = (exports1)=>{
20
+ if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
21
+ value: 'Module'
22
+ });
23
+ Object.defineProperty(exports1, '__esModule', {
24
+ value: true
25
+ });
26
+ };
27
+ })();
28
+ var __webpack_exports__ = {};
29
+ __webpack_require__.r(__webpack_exports__);
30
+ __webpack_require__.d(__webpack_exports__, {
31
+ Analytics: ()=>Analytics
32
+ });
33
+ const external_analytics_internal_js_namespaceObject = require("./analytics-internal.js");
34
+ const external_event_buffer_js_namespaceObject = require("./event-buffer.js");
35
+ const external_capture_summary_js_namespaceObject = require("./capture-summary.js");
36
+ const external_id_js_namespaceObject = require("./id.js");
37
+ const external_immutable_js_namespaceObject = require("./immutable.js");
38
+ const external_rate_limiter_js_namespaceObject = require("./rate-limiter.js");
39
+ const external_capture_properties_js_namespaceObject = require("./capture-properties.js");
40
+ const external_person_processing_js_namespaceObject = require("./person-processing.js");
41
+ const external_version_js_namespaceObject = require("./version.js");
42
+ const external_utf8_js_namespaceObject = require("./utf8.js");
43
+ const numberOption = (read, fallback, minimum, maximum)=>{
44
+ try {
45
+ const value = read();
46
+ return Math.max(minimum, Math.min(maximum, Math.floor('number' == typeof value && Number.isFinite(value) ? value : fallback)));
47
+ } catch {
48
+ return fallback;
49
+ }
50
+ };
51
+ const CLIENT_RATE_LIMIT_WARNING = '$$client_ingestion_warning';
52
+ const isNonEmptyString = (value)=>'string' == typeof value && value.trim().length > 0;
53
+ const isValidDistinctId = (value)=>isNonEmptyString(value) && ![
54
+ '$posthog_cookieless',
55
+ 'distinct_id',
56
+ 'distinctid',
57
+ 'undefined',
58
+ 'null'
59
+ ].includes(value.toLowerCase());
60
+ const isValidGroupKey = (value)=>isNonEmptyString(value) || Number.isFinite(value);
61
+ const eventTimestamp = (timestamp)=>{
62
+ try {
63
+ return (timestamp ?? new Date()).toISOString();
64
+ } catch {
65
+ return new Date().toISOString();
66
+ }
67
+ };
68
+ class Analytics {
69
+ constructor(options = {}, load, deliveryFactory){
70
+ this.name = external_analytics_internal_js_namespaceObject.Analytics;
71
+ this._disposed = false;
72
+ this._isTearingDown = false;
73
+ this._failures = 0;
74
+ this._dynamicEventProperties = [];
75
+ this._disposalWaiters = new Set();
76
+ this._teardownCallbacks = new Set();
77
+ this.bindCaptureHost = (host)=>{
78
+ this._host = host;
79
+ };
80
+ this.setup = (value)=>{
81
+ this._client = value.sharedClient;
82
+ this._kv = value.kv;
83
+ };
84
+ this.initialize = (host)=>{
85
+ this._deliveryHost = host;
86
+ if (this._deliveryFactory) this._driver = this._deliveryFactory(this._buffer, this._client, host, this._scheduling, this._beforeTeardown);
87
+ };
88
+ this.start = ()=>this._eager ? this._ensureDelivery('eager') : Promise.resolve();
89
+ this.onBeforeTeardown = (callback)=>{
90
+ if (!this._disposed) this._teardownCallbacks.add(callback);
91
+ const instance = this;
92
+ return {
93
+ get deliveryAvailable () {
94
+ return !instance._disposed && !!instance._driver;
95
+ },
96
+ dispose: ()=>{
97
+ this._teardownCallbacks.delete(callback);
98
+ }
99
+ };
100
+ };
101
+ this.flush = async (reason = 'flush')=>{
102
+ if (this._disposed) return;
103
+ if ('shutdown' === reason && this._loading) await this._loading;
104
+ else if (this._buffer.hasPending()) await this._ensureDelivery(reason);
105
+ await this._driver?.flush();
106
+ };
107
+ this.purge = ()=>this._buffer.purge();
108
+ this.dispose = ()=>{
109
+ this._disposed = true;
110
+ this._dynamicEventProperties.splice(0);
111
+ for (const release of this._disposalWaiters)release();
112
+ this._disposalWaiters.clear();
113
+ this._teardownCallbacks.clear();
114
+ if (this._driver) return this._driver.dispose();
115
+ this._buffer.dispose();
116
+ };
117
+ this._beforeTeardown = ()=>{
118
+ this._isTearingDown = true;
119
+ try {
120
+ for (const callback of this._teardownCallbacks)try {
121
+ callback();
122
+ } catch (error) {
123
+ try {
124
+ this._deliveryHost.reportFailure(error);
125
+ } catch {}
126
+ }
127
+ } finally{
128
+ this._isTearingDown = false;
129
+ }
130
+ };
131
+ this.onEvent = (listener)=>!this._disposed && this._host?.onEvent(listener) || {
132
+ dispose () {}
133
+ };
134
+ this.captureInitialPageview = ()=>this._capture('$pageview');
135
+ this.capture = (event, properties = null, options = {})=>{
136
+ try {
137
+ if ('unload' === options.delivery && !this._isTearingDown) return this._captureUnload(event, properties, options);
138
+ } catch (error) {
139
+ this._host?.logger.error('Unload capture failed', error);
140
+ return true;
141
+ }
142
+ return this._capture(event, properties, options) || !this._rateLimiter.limited;
143
+ };
144
+ this.captureImmediate = async (event, properties = null, options = {})=>{
145
+ try {
146
+ const authority = this._host?.authority();
147
+ const message = this._admitCapture(event, properties, options, false, true);
148
+ if (!message) return external_capture_summary_js_namespaceObject.EMPTY_CAPTURE_SUMMARY;
149
+ return await this._deliverImmediate(message, ()=>this._host?.authority() === authority);
150
+ } catch (error) {
151
+ return (0, external_capture_summary_js_namespaceObject.captureFailure)(error);
152
+ }
153
+ };
154
+ this.createPersonProfile = ()=>{
155
+ const host = this._host;
156
+ if (this._disposed || !host?.canMutate()) return;
157
+ host.state.prepare();
158
+ if (this._hasPersonProcessing()) return;
159
+ const properties = (0, external_person_processing_js_namespaceObject.snapshotPersonProperties)(host.state.distinctId, {}, {});
160
+ if (properties) this._setPersonProperties(properties);
161
+ };
162
+ this.setPersonProperties = (set, setOnce)=>{
163
+ const host = this._host;
164
+ if (this._disposed || !host?.canMutate()) return;
165
+ host.state.prepare();
166
+ const properties = (0, external_person_processing_js_namespaceObject.snapshotPersonProperties)(host.state.distinctId, set, setOnce);
167
+ if (!properties) return void host.logger.warn('Person properties must be JSON-serializable objects');
168
+ if (!(Object.keys(properties.set).length || Object.keys(properties.setOnce).length)) return;
169
+ this._setPersonProperties(properties);
170
+ };
171
+ this.unsetPersonProperties = (names)=>{
172
+ const host = this._host;
173
+ if (this._disposed || !host?.canMutate()) return;
174
+ const properties = (0, external_person_processing_js_namespaceObject.personPropertyNames)(names);
175
+ if (!properties.length || !this._requirePersonProcessing()) return;
176
+ host.state.prepare();
177
+ this._lastPersonProperties = void 0;
178
+ host.publishChangeProperties({
179
+ unset: properties
180
+ });
181
+ this._capture('$set', {
182
+ $unset: properties
183
+ }, {}, false, {
184
+ signature: void 0
185
+ });
186
+ };
187
+ this.setInternalOrTestUser = ()=>{
188
+ this.setPersonProperties({
189
+ $internal_or_test_user: true
190
+ });
191
+ };
192
+ this.identify = async (distinctId, set, setOnce)=>{
193
+ const host = this._host;
194
+ if (!isValidDistinctId(distinctId) || this._disposed || !host?.canMutate()) return;
195
+ host.state.prepare();
196
+ if (!this._requirePersonProcessing()) return;
197
+ const previousDistinctId = host.state.distinctId;
198
+ const wasIdentified = host.state.isIdentified;
199
+ const hasProperties = !!set || !!setOnce;
200
+ if (distinctId !== previousDistinctId || !wasIdentified) host.state.identify(distinctId);
201
+ const properties = (0, external_person_processing_js_namespaceObject.snapshotPersonProperties)(distinctId, set, setOnce);
202
+ if (!properties) {
203
+ host.logger.warn('Person properties must be JSON-serializable objects');
204
+ if (distinctId !== previousDistinctId || !wasIdentified || hasProperties) host.publishIdentify({
205
+ distinctId,
206
+ previousDistinctId,
207
+ wasIdentified,
208
+ set,
209
+ setOnce
210
+ });
211
+ return;
212
+ }
213
+ const options = this._identifyOptions(properties, !!set, !!setOnce);
214
+ if (distinctId === previousDistinctId && wasIdentified) {
215
+ if (hasProperties) this._setPersonProperties(properties, options, {
216
+ distinctId,
217
+ previousDistinctId,
218
+ wasIdentified,
219
+ set: options.set,
220
+ setOnce: options.setOnce
221
+ });
222
+ return;
223
+ }
224
+ this._lastPersonProperties = void 0;
225
+ if (hasProperties) host.publishChangeProperties({
226
+ set: options.set,
227
+ setOnce: options.setOnce
228
+ });
229
+ host.publishIdentify({
230
+ distinctId,
231
+ previousDistinctId,
232
+ wasIdentified,
233
+ set: options.set,
234
+ setOnce: options.setOnce
235
+ });
236
+ this._captureIdentification(distinctId, previousDistinctId, wasIdentified, properties, options, hasProperties);
237
+ };
238
+ this.alias = (alias)=>{
239
+ const host = this._host;
240
+ if (!isValidDistinctId(alias) || this._disposed || !host?.canCapture() || !host.state.prepare()) return;
241
+ if (!this._requirePersonProcessing()) return;
242
+ this._capture('$create_alias', {
243
+ alias
244
+ });
245
+ };
246
+ this.group = async (type, key, properties)=>{
247
+ if (!isNonEmptyString(type) || !isNonEmptyString(key) || this._disposed || !this._host?.canMutate() || 'never' === this._personProfiles) return;
248
+ this._host.state.prepare();
249
+ const changed = this._host.state.group(type, key);
250
+ if (!changed && !properties) return;
251
+ this._host.publishGroup({
252
+ type,
253
+ key,
254
+ changed,
255
+ properties
256
+ });
257
+ if (!this._hasPersonProcessing()) return;
258
+ this.capture('$groupidentify', {
259
+ $group_type: type,
260
+ $group_key: key,
261
+ ...properties ? {
262
+ $group_set: properties
263
+ } : {}
264
+ });
265
+ };
266
+ this.groupIdentify = (type, key, properties = {}, options = {})=>{
267
+ const host = this._host;
268
+ if (this._disposed || !host?.canCapture()) return;
269
+ if (!isNonEmptyString(type) || !isValidGroupKey(key)) return void host.logger.warn('Group identify requires a non-empty type and a string or finite numeric key');
270
+ if (!host.state.prepare() || !this._requirePersonProcessing()) return;
271
+ this.capture('$groupidentify', {
272
+ $group_type: type,
273
+ $group_key: key,
274
+ $group_set: properties
275
+ }, options);
276
+ };
277
+ this.resetGroups = ()=>{
278
+ const host = this._host;
279
+ if (this._disposed || !host?.canMutate()) return;
280
+ host.state.prepare();
281
+ host.state.resetGroups();
282
+ host.publishGroupsReset();
283
+ };
284
+ this.reset = ()=>{
285
+ if (this._disposed || !this._host?.canMutate()) return;
286
+ this._host.state.prepare();
287
+ this._host.state.reset();
288
+ this._lastPersonProperties = void 0;
289
+ this._host.publishReset(void 0);
290
+ };
291
+ this.registerDynamicEventProperties = (producer)=>{
292
+ if (this._disposed || !this._host || this._host.isDisposed()) return {
293
+ dispose () {}
294
+ };
295
+ this._dynamicEventProperties.push(producer);
296
+ let active = true;
297
+ return {
298
+ dispose: ()=>{
299
+ if (!active) return;
300
+ active = false;
301
+ const index = this._dynamicEventProperties.indexOf(producer);
302
+ if (-1 !== index) this._dynamicEventProperties.splice(index, 1);
303
+ }
304
+ };
305
+ };
306
+ this._load = load;
307
+ this._deliveryFactory = deliveryFactory;
308
+ let eager = false;
309
+ if (load) try {
310
+ eager = 'eager' === options.load;
311
+ } catch {}
312
+ this._eager = eager;
313
+ this._scheduling = {
314
+ flushAt: numberOption(()=>options.flushAt, 20, 1, external_analytics_internal_js_namespaceObject.MAX_ANALYTICS_BATCH_EVENTS),
315
+ flushInterval: numberOption(()=>options.flushInterval, 3000, 0, Number.MAX_SAFE_INTEGER)
316
+ };
317
+ this._buffer = new external_event_buffer_js_namespaceObject.EventBuffer(1000, (total, count = 1, reason = 'overflow')=>this._deliveryHost.reportWarning(`Analytics queue dropped ${count} ${reason} event${1 === count ? '' : 's'} (${total} total)`), external_analytics_internal_js_namespaceObject.MAX_ANALYTICS_BYTES, 3600000);
318
+ this._personProfiles = (0, external_person_processing_js_namespaceObject.snapshotPersonProfileMode)(()=>options.personProfiles);
319
+ this._rateLimiter = new external_rate_limiter_js_namespaceObject.ClientRateLimiter();
320
+ Object.defineProperty(this, 'isRateLimited', {
321
+ configurable: true,
322
+ enumerable: true,
323
+ get: ()=>this._rateLimiter.wouldLimit
324
+ });
325
+ }
326
+ async _ensureDelivery(reason, retryAfterPending = true) {
327
+ const load = this._load;
328
+ if (!load || this._disposed || this._driver) return;
329
+ if (this._loading) {
330
+ if ('capture' === reason || !retryAfterPending) return this._loading;
331
+ const previousFailures = this._failures;
332
+ await this._loading;
333
+ if (this._failures > previousFailures && this._buffer.hasPending()) await this._ensureDelivery(reason, false);
334
+ return;
335
+ }
336
+ if ('capture' === reason && this._failures > 0) return;
337
+ const loadDelivery = async ()=>{
338
+ await Promise.resolve();
339
+ try {
340
+ const createDelivery = await load();
341
+ if (this._disposed) return;
342
+ this._driver = createDelivery(this._buffer, this._client, this._deliveryHost, this._scheduling, this._beforeTeardown);
343
+ } catch (error) {
344
+ if (!this._driver) {
345
+ this._failures++;
346
+ this._client.logger.error('Automatic analytics loading failed', error);
347
+ }
348
+ } finally{
349
+ if (this._loading === pending) this._loading = void 0;
350
+ }
351
+ };
352
+ const pending = loadDelivery();
353
+ this._loading = pending;
354
+ return pending;
355
+ }
356
+ async _deliverImmediate(message, canContinue) {
357
+ let release;
358
+ const disposal = new Promise((resolve)=>{
359
+ release = resolve;
360
+ this._disposalWaiters.add(resolve);
361
+ });
362
+ try {
363
+ await Promise.race([
364
+ this._ensureDelivery('immediate'),
365
+ disposal
366
+ ]);
367
+ } finally{
368
+ this._disposalWaiters.delete(release);
369
+ }
370
+ let deliverImmediate;
371
+ try {
372
+ deliverImmediate = this._driver?.deliverImmediate;
373
+ } catch {
374
+ deliverImmediate = void 0;
375
+ }
376
+ if (!canContinue()) return (0, external_capture_summary_js_namespaceObject.captureFailure)(new Error('Immediate analytics delivery was cancelled'));
377
+ if (!this._driver || 'function' != typeof deliverImmediate || this._disposed) return (0, external_capture_summary_js_namespaceObject.captureFailure)(new Error('Immediate analytics delivery is unavailable'));
378
+ return deliverImmediate.call(this._driver, [
379
+ message
380
+ ], canContinue);
381
+ }
382
+ _deliverUnload(message, canContinue) {
383
+ const send = ()=>{
384
+ if (this._disposed || !canContinue()) return;
385
+ try {
386
+ this._driver?.deliverUnload([
387
+ message
388
+ ]);
389
+ } catch (error) {
390
+ this._deliveryHost.reportFailure(error);
391
+ }
392
+ };
393
+ if (this._driver) send();
394
+ else this._ensureDelivery('immediate').then(send, (error)=>this._deliveryHost.reportFailure(error));
395
+ }
396
+ _captureUnload(event, properties, options) {
397
+ const authority = this._host?.authority();
398
+ const message = this._admitCapture(event, properties, options, false, true);
399
+ if (message) this._deliverUnload(message, ()=>this._host?.authority() === authority);
400
+ return !!message || !this._rateLimiter.limited;
401
+ }
402
+ _capture(event, properties = null, options = {}, skipRateLimit = false, personProperties) {
403
+ return void 0 !== this._admitCapture(event, properties, options, skipRateLimit, false, personProperties);
404
+ }
405
+ _admitCapture(event, properties, options, skipRateLimit, immediate, personProperties) {
406
+ if (!isNonEmptyString(event) || this._disposed || !this._host?.canCapture() || !this._host.state.prepare()) return;
407
+ if (!this._acceptRateLimit(skipRateLimit)) return;
408
+ const snapshot = this._snapshotProperties(properties, options);
409
+ if (!snapshot) return;
410
+ const candidate = this._prepareCapture(event, snapshot, this._host);
411
+ return candidate ? this._commitCapture(candidate, immediate, this._host, personProperties) : void 0;
412
+ }
413
+ _acceptRateLimit(skipRateLimit) {
414
+ if (skipRateLimit) return true;
415
+ const rateLimit = this._rateLimiter.consume();
416
+ if (rateLimit.allowed) return true;
417
+ if (void 0 !== rateLimit.reportDropped) this._reportRateLimit(rateLimit.reportDropped);
418
+ return false;
419
+ }
420
+ _reportRateLimit(dropped) {
421
+ const warning = `${dropped} event(s) dropped since the last warning. Client limit is 10 events per second with a burst of 100.`;
422
+ this._host?.logger.warn(`PostHog client rate limited: ${warning}`);
423
+ if (this._capture(CLIENT_RATE_LIMIT_WARNING, {
424
+ $$client_ingestion_warning_message: `posthog-js client rate limited: ${warning}`
425
+ }, {}, true)) this._rateLimiter.reported();
426
+ }
427
+ _dynamicProperties() {
428
+ const properties = {};
429
+ for (const producer of this._dynamicEventProperties.slice())try {
430
+ Object.assign(properties, producer());
431
+ } catch (error) {
432
+ this._host?.logger.error('An event property producer failed', error);
433
+ }
434
+ return properties;
435
+ }
436
+ _snapshotProperties(properties, options) {
437
+ const dynamic = this._dynamicProperties();
438
+ try {
439
+ return (0, external_capture_properties_js_namespaceObject.snapshotCaptureProperties)(dynamic, properties, options);
440
+ } catch (error) {
441
+ this._host?.logger.error('Event properties are not JSON-serializable', error);
442
+ return;
443
+ }
444
+ }
445
+ _prepareCapture(event, snapshot, host) {
446
+ const processPersonProfile = this._hasPersonProcessing();
447
+ if (processPersonProfile) this._requirePersonProcessing();
448
+ const preparedSession = host.state.prepareSessionForEvent();
449
+ const session = preparedSession.context;
450
+ const distinctId = host.state.distinctId;
451
+ const deviceId = host.state.deviceId;
452
+ const groups = host.state.groups;
453
+ const metadata = {
454
+ token: host.projectToken,
455
+ distinct_id: distinctId,
456
+ $device_id: deviceId,
457
+ $groups: groups,
458
+ $session_id: session.sessionId,
459
+ $window_id: session.windowId,
460
+ $lib: 'web',
461
+ $lib_version: external_version_js_namespaceObject.version,
462
+ $process_person_profile: processPersonProfile
463
+ };
464
+ const observedProperties = {
465
+ ...snapshot.observed,
466
+ ...metadata,
467
+ $groups: {
468
+ ...groups
469
+ }
470
+ };
471
+ const message = {
472
+ uuid: snapshot.uuid ?? (0, external_id_js_namespaceObject.createId)(),
473
+ event,
474
+ distinct_id: distinctId,
475
+ properties: {
476
+ ...snapshot.message,
477
+ ...metadata
478
+ },
479
+ timestamp: eventTimestamp(snapshot.timestamp)
480
+ };
481
+ const bytes = this._measureCapture(message);
482
+ if (void 0 === bytes) return;
483
+ return {
484
+ message,
485
+ bytes,
486
+ preparedSession,
487
+ observedProperties
488
+ };
489
+ }
490
+ _measureCapture(message) {
491
+ try {
492
+ return (0, external_utf8_js_namespaceObject.utf8Bytes)(JSON.stringify(message));
493
+ } catch (error) {
494
+ this._host?.logger.error('The finalized event could not be measured', error);
495
+ return;
496
+ }
497
+ }
498
+ _warnOversized(event, bytes) {
499
+ this._host?.logger.warn(`Event "${event}" (${bytes} bytes) exceeds the local analytics limit and was dropped`);
500
+ }
501
+ _enqueueCapture(candidate, immediate) {
502
+ const { message, bytes } = candidate;
503
+ if (immediate ? bytes > external_analytics_internal_js_namespaceObject.MAX_ANALYTICS_BYTES : !this._buffer.enqueue(message, bytes)) {
504
+ if (bytes > external_analytics_internal_js_namespaceObject.MAX_ANALYTICS_BYTES) this._warnOversized(message.event, bytes);
505
+ return false;
506
+ }
507
+ return true;
508
+ }
509
+ _commitCapture(candidate, immediate, host, personProperties) {
510
+ if (!this._enqueueCapture(candidate, immediate)) return;
511
+ if (!host.state.sessionAdmitted(candidate.preparedSession)) {
512
+ if (!immediate) this._buffer.discardQueued(candidate.message);
513
+ return;
514
+ }
515
+ if (personProperties) this._lastPersonProperties = personProperties.signature;
516
+ else if (Object.hasOwn(candidate.message.properties, '$set') || Object.hasOwn(candidate.message.properties, '$set_once') || Object.hasOwn(candidate.message.properties, '$unset')) this._lastPersonProperties = void 0;
517
+ this._publishCapture(candidate, host);
518
+ if (!immediate) this._ensureDelivery('capture');
519
+ return candidate.message;
520
+ }
521
+ _publishCapture(candidate, host) {
522
+ const { preparedSession, message, observedProperties } = candidate;
523
+ const session = preparedSession.context;
524
+ if (preparedSession.reason) host.publishNewSession({
525
+ ...session,
526
+ reason: preparedSession.reason
527
+ });
528
+ host.publishEvent((0, external_immutable_js_namespaceObject.deepFreeze)({
529
+ event: message.event,
530
+ properties: observedProperties
531
+ }));
532
+ }
533
+ _hasPersonProcessing() {
534
+ const host = this._host;
535
+ if (!host) return false;
536
+ const enabled = this._kv?.get('personProcessingEnabled') === true;
537
+ return (0, external_person_processing_js_namespaceObject.shouldProcessPerson)(this._personProfiles, host.state.isIdentified, Object.keys(host.state.groups).length > 0, enabled);
538
+ }
539
+ _requirePersonProcessing() {
540
+ if ('never' === this._personProfiles) {
541
+ this._host?.logger.warn('Person processing is disabled by analytics configuration');
542
+ return false;
543
+ }
544
+ if (this._kv?.get('personProcessingEnabled') !== true) this._kv?.set('personProcessingEnabled', true);
545
+ return true;
546
+ }
547
+ _setPersonProperties(properties, options = {
548
+ set: properties.set,
549
+ setOnce: properties.setOnce
550
+ }, identification) {
551
+ const host = this._host;
552
+ if (!host || !this._requirePersonProcessing() || this._lastPersonProperties === properties.signature) return;
553
+ this._lastPersonProperties = void 0;
554
+ host.publishChangeProperties({
555
+ set: options.set,
556
+ setOnce: options.setOnce
557
+ });
558
+ if (identification) host.publishIdentify(identification);
559
+ this._capture('$set', null, options, false, {
560
+ signature: properties.signature
561
+ });
562
+ }
563
+ _identifyOptions(properties, hasSet, hasSetOnce) {
564
+ const options = {};
565
+ if (hasSet) options.set = properties.set;
566
+ if (hasSetOnce) options.setOnce = properties.setOnce;
567
+ return options;
568
+ }
569
+ _captureIdentification(distinctId, previousDistinctId, wasIdentified, properties, options, hasProperties) {
570
+ const personProperties = {
571
+ signature: properties.signature
572
+ };
573
+ if (!wasIdentified && distinctId !== previousDistinctId) return this._capture('$identify', {
574
+ $anon_distinct_id: previousDistinctId
575
+ }, options, false, personProperties);
576
+ if (distinctId === previousDistinctId) return this._capture('$set', null, {
577
+ set: properties.set,
578
+ setOnce: properties.setOnce
579
+ }, false, personProperties);
580
+ return hasProperties ? this._capture('$set', null, options, false, personProperties) : false;
581
+ }
582
+ }
583
+ exports.Analytics = __webpack_exports__.Analytics;
584
+ for(var __rspack_i in __webpack_exports__)if (-1 === [
585
+ "Analytics"
586
+ ].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
587
+ Object.defineProperty(exports, '__esModule', {
588
+ value: true
589
+ });