@posthog/browser 0.5.0 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/ARCHITECTURE.md +17 -17
- package/README.md +42 -7
- package/dist/composition/npm/core.d.ts +1 -1
- package/dist/composition/npm/index.d.ts +2 -2
- package/dist/core/posthog.js +88 -14
- package/dist/core/posthog.mjs +88 -14
- package/dist/core/state/state.d.ts +32 -3
- package/dist/core/state/state.js +117 -25
- package/dist/core/state/state.mjs +105 -25
- package/dist/core/types.d.ts +87 -5
- package/dist/persistence/cookie.d.ts +19 -0
- package/dist/persistence/cookie.js +273 -0
- package/dist/persistence/cookie.mjs +235 -0
- package/dist/platform/logger.d.ts +5 -1
- package/dist/platform/logger.js +3 -3
- package/dist/platform/logger.mjs +3 -3
- package/dist/primitives/id.d.ts +5 -0
- package/dist/primitives/id.js +10 -2
- package/dist/primitives/id.mjs +6 -1
- package/dist/primitives/version.js +1 -1
- package/dist/primitives/version.mjs +1 -1
- package/dist/products/analytics/analytics-core.d.ts +17 -4
- package/dist/products/analytics/analytics-core.js +101 -10
- package/dist/products/analytics/analytics-core.mjs +101 -10
- package/dist/products/analytics/analytics-internal.d.ts +3 -2
- package/dist/products/analytics/delivery/analytics-delivery.js +1 -1
- package/dist/products/analytics/delivery/analytics-delivery.mjs +1 -1
- package/dist/products/analytics/delivery/lane.d.ts +8 -1
- package/dist/products/analytics/delivery/lane.js +16 -4
- package/dist/products/analytics/delivery/lane.mjs +16 -4
- package/dist/products/analytics/groups.js +1 -1
- package/dist/products/analytics/groups.mjs +1 -1
- package/dist/products/analytics/person.js +1 -1
- package/dist/products/analytics/person.mjs +1 -1
- package/dist/products/analytics/properties/analytics-properties.d.ts +1 -0
- package/dist/products/analytics/properties/analytics-properties.js +3 -0
- package/dist/products/analytics/properties/analytics-properties.mjs +1 -1
- package/dist/products/flags/flags-internal.d.ts +3 -0
- package/dist/products/flags/flags-options.d.ts +16 -2
- package/dist/products/flags/flags-token.d.ts +28 -2
- package/dist/products/flags/flags.d.ts +1 -1
- package/dist/products/flags/flags.js +110 -4
- package/dist/products/flags/flags.mjs +110 -4
- package/dist/products/surveys/surveys-extension.js +75 -26
- package/dist/products/surveys/surveys-extension.mjs +77 -28
- package/dist/products/surveys/surveys-internal.d.ts +6 -0
- package/package.json +9 -4
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import type { CaptureOptions, CapturedEventInfo, Disposable } from '@posthog/browser-common';
|
|
1
|
+
import type { CaptureOptions, CapturedEventInfo, Disposable, SessionContext } from '@posthog/browser-common';
|
|
2
2
|
import { Publisher } from '@posthog/browser-common/pubsub';
|
|
3
3
|
import { type AnalyticsCore, type AnalyticsDeliveryFactory, type AnalyticsTeardownSubscription } from './analytics-internal';
|
|
4
4
|
import type { ChangePropertiesInfo, GroupInfo, IdentifyInfo } from '../../core/context/browser-client';
|
|
5
|
-
import type { NewSessionInfo, CaptureSummary } from '../../core/types';
|
|
5
|
+
import type { BootstrapOptions, NewSessionInfo, CaptureSummary, ResetOptions } from '../../core/types';
|
|
6
6
|
import type { AnalyticsOptions, PersonProfileMode } from './analytics-options';
|
|
7
7
|
import { type PersonProperties } from './properties/person-processing';
|
|
8
8
|
export declare const isNonEmptyString: (value: unknown) => value is string;
|
|
@@ -18,14 +18,17 @@ export declare class Analytics {
|
|
|
18
18
|
readonly _core: AnalyticsCore;
|
|
19
19
|
readonly _eventPublisher: Publisher<CapturedEventInfo>;
|
|
20
20
|
readonly _newSessionPublisher: Publisher<NewSessionInfo>;
|
|
21
|
+
readonly _sessionIdPublisher: Publisher<SessionContext>;
|
|
21
22
|
readonly _identifyPublisher: Publisher<IdentifyInfo>;
|
|
22
23
|
readonly _changePropertiesPublisher: Publisher<ChangePropertiesInfo>;
|
|
23
24
|
readonly _groupPublisher: Publisher<GroupInfo>;
|
|
24
25
|
readonly _groupsResetPublisher: Publisher<void>;
|
|
25
26
|
readonly _resetPublisher: Publisher<void>;
|
|
26
|
-
|
|
27
|
+
_personProfiles: PersonProfileMode;
|
|
27
28
|
_disposed: boolean;
|
|
28
29
|
_lastPersonProperties: string | undefined;
|
|
30
|
+
/** The last admitted `$pageview` UUID and the session it belongs to. */
|
|
31
|
+
_pageview: [id: string, sessionId: string] | undefined;
|
|
29
32
|
private _driver;
|
|
30
33
|
private _isTearingDown;
|
|
31
34
|
private readonly _registeredProperties;
|
|
@@ -53,15 +56,25 @@ export declare class Analytics {
|
|
|
53
56
|
private _prepareCapture;
|
|
54
57
|
private _warnOversized;
|
|
55
58
|
private _commitCapture;
|
|
59
|
+
/** A pageview belongs to its session, so an expired or rotated session has none. */
|
|
60
|
+
_pageviewIdFor(sessionId: string | undefined): string | undefined;
|
|
56
61
|
_hasPersonProcessing(): boolean;
|
|
57
62
|
_requirePersonProcessing(): boolean;
|
|
58
63
|
/** Whether a local identity operation may run now. Prepares state without requiring it. */
|
|
59
64
|
_canMutate(): boolean;
|
|
65
|
+
/** Applies an identity another subdomain stored as this context applies its own reset or identify. */
|
|
66
|
+
private _adoptStoredIdentity;
|
|
67
|
+
/** Clears state that belonged to the previous identity and notifies reset listeners. */
|
|
68
|
+
private _resetIdentityState;
|
|
60
69
|
/** Whether registered properties are available now. */
|
|
61
70
|
private _canUseProperties;
|
|
62
71
|
_setPersonProperties(properties: PersonProperties, options?: CaptureOptions, identification?: IdentifyInfo): void;
|
|
63
72
|
identify(distinctId: string, set?: Record<string, unknown>, setOnce?: Record<string, unknown>): void;
|
|
64
|
-
|
|
73
|
+
/** Resets identity state and returns the bootstrap values for the new identity, if any. */
|
|
74
|
+
reset(options?: boolean | ResetOptions): ResetOptions['bootstrap'];
|
|
75
|
+
/** Applies setup bootstrap without replacing a persisted identity, as in posthog-js. */
|
|
76
|
+
bootstrap(options: BootstrapOptions | undefined): void;
|
|
77
|
+
private _bootstrapSession;
|
|
65
78
|
private _registration;
|
|
66
79
|
register(properties: Record<string, unknown>): void;
|
|
67
80
|
registerOnce(properties: Record<string, unknown>, defaultValue?: unknown): void;
|
|
@@ -35,6 +35,7 @@ __webpack_require__.d(__webpack_exports__, {
|
|
|
35
35
|
});
|
|
36
36
|
const pubsub_namespaceObject = require("@posthog/browser-common/pubsub");
|
|
37
37
|
const external_analytics_internal_js_namespaceObject = require("./analytics-internal.js");
|
|
38
|
+
const state_js_namespaceObject = require("../../core/state/state.js");
|
|
38
39
|
const event_buffer_js_namespaceObject = require("./admission/event-buffer.js");
|
|
39
40
|
const capture_summary_js_namespaceObject = require("./admission/capture-summary.js");
|
|
40
41
|
const id_js_namespaceObject = require("../../primitives/id.js");
|
|
@@ -70,6 +71,14 @@ const eventTimestamp = (timestamp)=>{
|
|
|
70
71
|
return new Date().toISOString();
|
|
71
72
|
}
|
|
72
73
|
};
|
|
74
|
+
const snapshotOptions = (options)=>{
|
|
75
|
+
try {
|
|
76
|
+
const snapshot = JSON.parse(JSON.stringify(options ?? {}));
|
|
77
|
+
return 'object' == typeof snapshot && null !== snapshot ? snapshot : {};
|
|
78
|
+
} catch {
|
|
79
|
+
return {};
|
|
80
|
+
}
|
|
81
|
+
};
|
|
73
82
|
class Analytics {
|
|
74
83
|
constructor(_core, options, createDelivery){
|
|
75
84
|
this._core = _core;
|
|
@@ -94,6 +103,7 @@ class Analytics {
|
|
|
94
103
|
const report = (label)=>(error)=>_core.logger.error(`${label} listener failed`, error);
|
|
95
104
|
this._eventPublisher = new pubsub_namespaceObject.Publisher(report('An event'));
|
|
96
105
|
this._newSessionPublisher = new pubsub_namespaceObject.Publisher(report('A session'));
|
|
106
|
+
this._sessionIdPublisher = new pubsub_namespaceObject.Publisher(report('A session ID'));
|
|
97
107
|
this._identifyPublisher = new pubsub_namespaceObject.Publisher(report('An identify'));
|
|
98
108
|
this._changePropertiesPublisher = new pubsub_namespaceObject.Publisher(report('A person-property'));
|
|
99
109
|
this._groupPublisher = new pubsub_namespaceObject.Publisher(report('A group'));
|
|
@@ -148,6 +158,7 @@ class Analytics {
|
|
|
148
158
|
for (const publisher of [
|
|
149
159
|
this._eventPublisher,
|
|
150
160
|
this._newSessionPublisher,
|
|
161
|
+
this._sessionIdPublisher,
|
|
151
162
|
this._identifyPublisher,
|
|
152
163
|
this._changePropertiesPublisher,
|
|
153
164
|
this._groupPublisher,
|
|
@@ -161,8 +172,10 @@ class Analytics {
|
|
|
161
172
|
return this._capture('$pageview');
|
|
162
173
|
}
|
|
163
174
|
capture(event, properties = null, options = {}) {
|
|
175
|
+
let delivery;
|
|
164
176
|
try {
|
|
165
|
-
|
|
177
|
+
delivery = options.delivery;
|
|
178
|
+
if ('unload' === delivery && !this._isTearingDown) {
|
|
166
179
|
const authority = this._core._immediateAuthority;
|
|
167
180
|
const message = this._admitCapture(event, properties, options, false, true);
|
|
168
181
|
if (message && !this._disposed && this._core._immediateAuthority === authority) this._driver?.deliverUnload([
|
|
@@ -174,7 +187,9 @@ class Analytics {
|
|
|
174
187
|
this._core.logger.error('Unload capture failed', error);
|
|
175
188
|
return true;
|
|
176
189
|
}
|
|
177
|
-
|
|
190
|
+
if (!this._capture(event, properties, options)) return !this._rateLimiter.limited;
|
|
191
|
+
if ('immediate' === delivery && !this._isTearingDown) this._driver?.flush(true).catch(this._deliveryHost.reportFailure);
|
|
192
|
+
return true;
|
|
178
193
|
}
|
|
179
194
|
async captureImmediate(event, properties = null, options = {}) {
|
|
180
195
|
try {
|
|
@@ -197,6 +212,7 @@ class Analytics {
|
|
|
197
212
|
_admitCapture(event, properties, options, skipRateLimit, immediate, personProperties) {
|
|
198
213
|
if (!isNonEmptyString(event) || this._disposed || !this._core.canCapture) return;
|
|
199
214
|
if (!this._acceptRateLimit(skipRateLimit)) return;
|
|
215
|
+
this._adoptStoredIdentity();
|
|
200
216
|
const snapshot = this._snapshotProperties(properties, options);
|
|
201
217
|
if (!snapshot) return;
|
|
202
218
|
const candidate = this._prepareCapture(event, snapshot);
|
|
@@ -238,6 +254,13 @@ class Analytics {
|
|
|
238
254
|
const session = preparedSession.context;
|
|
239
255
|
const distinctId = state.distinctId;
|
|
240
256
|
const groups = state.groups;
|
|
257
|
+
const uuid = snapshot.uuid ?? (0, id_js_namespaceObject.createId)();
|
|
258
|
+
const activePageviewId = this._pageviewIdFor(session.sessionId);
|
|
259
|
+
const isPageview = '$pageview' === event;
|
|
260
|
+
const pageviewId = isPageview ? uuid : activePageviewId;
|
|
261
|
+
const defaults = {};
|
|
262
|
+
if (pageviewId && !snapshot.overrides.has('$pageview_id')) defaults.$pageview_id = pageviewId;
|
|
263
|
+
if (activePageviewId && (isPageview || '$pageleave' === event) && !snapshot.overrides.has('$prev_pageview_id')) defaults.$prev_pageview_id = activePageviewId;
|
|
241
264
|
const metadata = {
|
|
242
265
|
token: this._core.projectToken,
|
|
243
266
|
distinct_id: distinctId,
|
|
@@ -250,10 +273,11 @@ class Analytics {
|
|
|
250
273
|
$process_person_profile: processPersonProfile
|
|
251
274
|
};
|
|
252
275
|
const message = {
|
|
253
|
-
uuid
|
|
276
|
+
uuid,
|
|
254
277
|
event,
|
|
255
278
|
distinct_id: distinctId,
|
|
256
279
|
properties: {
|
|
280
|
+
...defaults,
|
|
257
281
|
...snapshot.message,
|
|
258
282
|
...metadata
|
|
259
283
|
},
|
|
@@ -264,12 +288,16 @@ class Analytics {
|
|
|
264
288
|
if (bytes > external_analytics_internal_js_namespaceObject.MAX_ANALYTICS_BYTES) {
|
|
265
289
|
const minimum = (0, utf8_js_namespaceObject.utf8Bytes)(JSON.stringify({
|
|
266
290
|
...message,
|
|
267
|
-
properties:
|
|
291
|
+
properties: {
|
|
292
|
+
...defaults,
|
|
293
|
+
...(0, capture_properties_js_namespaceObject.unavoidableProperties)(snapshot, metadata)
|
|
294
|
+
}
|
|
268
295
|
}));
|
|
269
296
|
if (minimum > external_analytics_internal_js_namespaceObject.MAX_ANALYTICS_BYTES) return void this._warnOversized(event, bytes);
|
|
270
297
|
}
|
|
271
298
|
const changed = (0, capture_properties_js_namespaceObject.applySessionProperties)(snapshot, this._sessionProperties.values(session.sessionId));
|
|
272
299
|
message.properties = {
|
|
300
|
+
...defaults,
|
|
273
301
|
...snapshot.message,
|
|
274
302
|
...metadata
|
|
275
303
|
};
|
|
@@ -279,6 +307,7 @@ class Analytics {
|
|
|
279
307
|
bytes,
|
|
280
308
|
preparedSession,
|
|
281
309
|
observedProperties: {
|
|
310
|
+
...defaults,
|
|
282
311
|
...snapshot.observed,
|
|
283
312
|
...metadata,
|
|
284
313
|
$groups: {
|
|
@@ -300,6 +329,7 @@ class Analytics {
|
|
|
300
329
|
if (bytes > external_analytics_internal_js_namespaceObject.MAX_ANALYTICS_BYTES) this._warnOversized(message.event, bytes);
|
|
301
330
|
return;
|
|
302
331
|
}
|
|
332
|
+
const previous = this._core._state.session;
|
|
303
333
|
if (!this._core._state.sessionAdmitted(preparedSession)) {
|
|
304
334
|
if (!immediate) this._buffer.discardQueued(message);
|
|
305
335
|
return;
|
|
@@ -307,7 +337,15 @@ class Analytics {
|
|
|
307
337
|
if (personProperties) this._lastPersonProperties = personProperties.signature;
|
|
308
338
|
else if (Object.hasOwn(message.properties, '$set') || Object.hasOwn(message.properties, '$set_once') || Object.hasOwn(message.properties, '$unset')) this._lastPersonProperties = void 0;
|
|
309
339
|
const session = preparedSession.context;
|
|
340
|
+
if ('$pageview' === message.event) this._pageview = [
|
|
341
|
+
message.uuid,
|
|
342
|
+
session.sessionId
|
|
343
|
+
];
|
|
344
|
+
else if (this._pageview?.[1] !== session.sessionId) this._pageview = void 0;
|
|
310
345
|
this._sessionProperties.admitted(session.sessionId);
|
|
346
|
+
if (previous?.sessionId !== session.sessionId || previous.windowId !== session.windowId) this._sessionIdPublisher.publish({
|
|
347
|
+
...session
|
|
348
|
+
});
|
|
311
349
|
if (preparedSession.reason) this._newSessionPublisher.publish({
|
|
312
350
|
...session,
|
|
313
351
|
reason: preparedSession.reason
|
|
@@ -318,6 +356,10 @@ class Analytics {
|
|
|
318
356
|
}));
|
|
319
357
|
return message;
|
|
320
358
|
}
|
|
359
|
+
_pageviewIdFor(sessionId) {
|
|
360
|
+
const pageview = this._pageview;
|
|
361
|
+
return pageview && pageview[1] === sessionId ? pageview[0] : void 0;
|
|
362
|
+
}
|
|
321
363
|
_hasPersonProcessing() {
|
|
322
364
|
const state = this._core._state;
|
|
323
365
|
return (0, person_processing_js_namespaceObject.shouldProcessPerson)(this._personProfiles, state.isIdentified, Object.keys(state.groups).length > 0, true === this._core.kv.get('personProcessingEnabled'));
|
|
@@ -333,8 +375,30 @@ class Analytics {
|
|
|
333
375
|
_canMutate() {
|
|
334
376
|
if (this._disposed || !this._core._canMutate()) return false;
|
|
335
377
|
this._core._state.prepare();
|
|
378
|
+
this._adoptStoredIdentity();
|
|
336
379
|
return true;
|
|
337
380
|
}
|
|
381
|
+
_adoptStoredIdentity() {
|
|
382
|
+
const state = this._core._state;
|
|
383
|
+
const previousDistinctId = state.distinctId;
|
|
384
|
+
const wasIdentified = state.isIdentified;
|
|
385
|
+
if (!state.adoptStoredIdentity()) return;
|
|
386
|
+
if (!state.isIdentified) return this._resetIdentityState();
|
|
387
|
+
this._lastPersonProperties = void 0;
|
|
388
|
+
this._identifyPublisher.publish({
|
|
389
|
+
distinctId: state.distinctId,
|
|
390
|
+
previousDistinctId,
|
|
391
|
+
wasIdentified,
|
|
392
|
+
set: void 0,
|
|
393
|
+
setOnce: void 0
|
|
394
|
+
});
|
|
395
|
+
}
|
|
396
|
+
_resetIdentityState() {
|
|
397
|
+
this._lastPersonProperties = void 0;
|
|
398
|
+
this._pageview = void 0;
|
|
399
|
+
this._sessionProperties.clear();
|
|
400
|
+
this._resetPublisher.publish(void 0);
|
|
401
|
+
}
|
|
338
402
|
_canUseProperties() {
|
|
339
403
|
return !this._disposed && this._core._canMutate() && this._core._state.prepare();
|
|
340
404
|
}
|
|
@@ -405,12 +469,39 @@ class Analytics {
|
|
|
405
469
|
$anon_distinct_id: previousDistinctId
|
|
406
470
|
}, options, false, signature);
|
|
407
471
|
}
|
|
408
|
-
reset() {
|
|
409
|
-
if (!this._canMutate()) return;
|
|
410
|
-
this._core._state.
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
472
|
+
reset(options) {
|
|
473
|
+
if (this._disposed || !this._core._canMutate()) return;
|
|
474
|
+
this._core._state.prepare();
|
|
475
|
+
const { resetDeviceId, bootstrap } = true === options || false === options ? {
|
|
476
|
+
resetDeviceId: options
|
|
477
|
+
} : snapshotOptions(options);
|
|
478
|
+
const { distinctId, isIdentifiedId } = bootstrap ?? {};
|
|
479
|
+
this._core._state.reset(true === resetDeviceId, isValidDistinctId(distinctId) ? [
|
|
480
|
+
distinctId,
|
|
481
|
+
true === isIdentifiedId
|
|
482
|
+
] : void 0, this._bootstrapSession(bootstrap));
|
|
483
|
+
this._resetIdentityState();
|
|
484
|
+
return bootstrap;
|
|
485
|
+
}
|
|
486
|
+
bootstrap(options) {
|
|
487
|
+
const bootstrap = snapshotOptions(options);
|
|
488
|
+
const { distinctId, isIdentifiedId } = bootstrap;
|
|
489
|
+
const session = this._bootstrapSession(bootstrap);
|
|
490
|
+
if (session) this._core._state.bootstrapSession(session);
|
|
491
|
+
if (!isValidDistinctId(distinctId) || !this._canMutate()) return;
|
|
492
|
+
const state = this._core._state;
|
|
493
|
+
if (state.generated) return state.bootstrapIdentity(distinctId, true === isIdentifiedId);
|
|
494
|
+
if (true !== isIdentifiedId) return;
|
|
495
|
+
if (distinctId === state.distinctId) state.identify(distinctId);
|
|
496
|
+
else if (state.isIdentified) this._core.logger.warn('Ignored the bootstrap distinct ID of a different identified user');
|
|
497
|
+
else this.identify(distinctId);
|
|
498
|
+
}
|
|
499
|
+
_bootstrapSession(bootstrap) {
|
|
500
|
+
const sessionId = bootstrap?.sessionId;
|
|
501
|
+
if (void 0 === sessionId) return;
|
|
502
|
+
const session = (0, state_js_namespaceObject.readBootstrapSession)(sessionId);
|
|
503
|
+
if (!session) this._core.logger.error('Ignored the bootstrap session ID: not a past UUIDv7');
|
|
504
|
+
return session;
|
|
414
505
|
}
|
|
415
506
|
_registration(properties) {
|
|
416
507
|
if (!this._canUseProperties()) return;
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { Publisher } from "@posthog/browser-common/pubsub";
|
|
2
2
|
import { MAX_ANALYTICS_BATCH_EVENTS, MAX_ANALYTICS_BYTES } from "./analytics-internal.mjs";
|
|
3
|
+
import { readBootstrapSession } from "../../core/state/state.mjs";
|
|
3
4
|
import { EventBuffer } from "./admission/event-buffer.mjs";
|
|
4
5
|
import { EMPTY_CAPTURE_SUMMARY, captureFailure } from "./admission/capture-summary.mjs";
|
|
5
6
|
import { createId } from "../../primitives/id.mjs";
|
|
@@ -35,6 +36,14 @@ const eventTimestamp = (timestamp)=>{
|
|
|
35
36
|
return new Date().toISOString();
|
|
36
37
|
}
|
|
37
38
|
};
|
|
39
|
+
const snapshotOptions = (options)=>{
|
|
40
|
+
try {
|
|
41
|
+
const snapshot = JSON.parse(JSON.stringify(options ?? {}));
|
|
42
|
+
return 'object' == typeof snapshot && null !== snapshot ? snapshot : {};
|
|
43
|
+
} catch {
|
|
44
|
+
return {};
|
|
45
|
+
}
|
|
46
|
+
};
|
|
38
47
|
class Analytics {
|
|
39
48
|
constructor(_core, options, createDelivery){
|
|
40
49
|
this._core = _core;
|
|
@@ -59,6 +68,7 @@ class Analytics {
|
|
|
59
68
|
const report = (label)=>(error)=>_core.logger.error(`${label} listener failed`, error);
|
|
60
69
|
this._eventPublisher = new Publisher(report('An event'));
|
|
61
70
|
this._newSessionPublisher = new Publisher(report('A session'));
|
|
71
|
+
this._sessionIdPublisher = new Publisher(report('A session ID'));
|
|
62
72
|
this._identifyPublisher = new Publisher(report('An identify'));
|
|
63
73
|
this._changePropertiesPublisher = new Publisher(report('A person-property'));
|
|
64
74
|
this._groupPublisher = new Publisher(report('A group'));
|
|
@@ -113,6 +123,7 @@ class Analytics {
|
|
|
113
123
|
for (const publisher of [
|
|
114
124
|
this._eventPublisher,
|
|
115
125
|
this._newSessionPublisher,
|
|
126
|
+
this._sessionIdPublisher,
|
|
116
127
|
this._identifyPublisher,
|
|
117
128
|
this._changePropertiesPublisher,
|
|
118
129
|
this._groupPublisher,
|
|
@@ -126,8 +137,10 @@ class Analytics {
|
|
|
126
137
|
return this._capture('$pageview');
|
|
127
138
|
}
|
|
128
139
|
capture(event, properties = null, options = {}) {
|
|
140
|
+
let delivery;
|
|
129
141
|
try {
|
|
130
|
-
|
|
142
|
+
delivery = options.delivery;
|
|
143
|
+
if ('unload' === delivery && !this._isTearingDown) {
|
|
131
144
|
const authority = this._core._immediateAuthority;
|
|
132
145
|
const message = this._admitCapture(event, properties, options, false, true);
|
|
133
146
|
if (message && !this._disposed && this._core._immediateAuthority === authority) this._driver?.deliverUnload([
|
|
@@ -139,7 +152,9 @@ class Analytics {
|
|
|
139
152
|
this._core.logger.error('Unload capture failed', error);
|
|
140
153
|
return true;
|
|
141
154
|
}
|
|
142
|
-
|
|
155
|
+
if (!this._capture(event, properties, options)) return !this._rateLimiter.limited;
|
|
156
|
+
if ('immediate' === delivery && !this._isTearingDown) this._driver?.flush(true).catch(this._deliveryHost.reportFailure);
|
|
157
|
+
return true;
|
|
143
158
|
}
|
|
144
159
|
async captureImmediate(event, properties = null, options = {}) {
|
|
145
160
|
try {
|
|
@@ -162,6 +177,7 @@ class Analytics {
|
|
|
162
177
|
_admitCapture(event, properties, options, skipRateLimit, immediate, personProperties) {
|
|
163
178
|
if (!isNonEmptyString(event) || this._disposed || !this._core.canCapture) return;
|
|
164
179
|
if (!this._acceptRateLimit(skipRateLimit)) return;
|
|
180
|
+
this._adoptStoredIdentity();
|
|
165
181
|
const snapshot = this._snapshotProperties(properties, options);
|
|
166
182
|
if (!snapshot) return;
|
|
167
183
|
const candidate = this._prepareCapture(event, snapshot);
|
|
@@ -203,6 +219,13 @@ class Analytics {
|
|
|
203
219
|
const session = preparedSession.context;
|
|
204
220
|
const distinctId = state.distinctId;
|
|
205
221
|
const groups = state.groups;
|
|
222
|
+
const uuid = snapshot.uuid ?? createId();
|
|
223
|
+
const activePageviewId = this._pageviewIdFor(session.sessionId);
|
|
224
|
+
const isPageview = '$pageview' === event;
|
|
225
|
+
const pageviewId = isPageview ? uuid : activePageviewId;
|
|
226
|
+
const defaults = {};
|
|
227
|
+
if (pageviewId && !snapshot.overrides.has('$pageview_id')) defaults.$pageview_id = pageviewId;
|
|
228
|
+
if (activePageviewId && (isPageview || '$pageleave' === event) && !snapshot.overrides.has('$prev_pageview_id')) defaults.$prev_pageview_id = activePageviewId;
|
|
206
229
|
const metadata = {
|
|
207
230
|
token: this._core.projectToken,
|
|
208
231
|
distinct_id: distinctId,
|
|
@@ -215,10 +238,11 @@ class Analytics {
|
|
|
215
238
|
$process_person_profile: processPersonProfile
|
|
216
239
|
};
|
|
217
240
|
const message = {
|
|
218
|
-
uuid
|
|
241
|
+
uuid,
|
|
219
242
|
event,
|
|
220
243
|
distinct_id: distinctId,
|
|
221
244
|
properties: {
|
|
245
|
+
...defaults,
|
|
222
246
|
...snapshot.message,
|
|
223
247
|
...metadata
|
|
224
248
|
},
|
|
@@ -229,12 +253,16 @@ class Analytics {
|
|
|
229
253
|
if (bytes > MAX_ANALYTICS_BYTES) {
|
|
230
254
|
const minimum = utf8Bytes(JSON.stringify({
|
|
231
255
|
...message,
|
|
232
|
-
properties:
|
|
256
|
+
properties: {
|
|
257
|
+
...defaults,
|
|
258
|
+
...unavoidableProperties(snapshot, metadata)
|
|
259
|
+
}
|
|
233
260
|
}));
|
|
234
261
|
if (minimum > MAX_ANALYTICS_BYTES) return void this._warnOversized(event, bytes);
|
|
235
262
|
}
|
|
236
263
|
const changed = applySessionProperties(snapshot, this._sessionProperties.values(session.sessionId));
|
|
237
264
|
message.properties = {
|
|
265
|
+
...defaults,
|
|
238
266
|
...snapshot.message,
|
|
239
267
|
...metadata
|
|
240
268
|
};
|
|
@@ -244,6 +272,7 @@ class Analytics {
|
|
|
244
272
|
bytes,
|
|
245
273
|
preparedSession,
|
|
246
274
|
observedProperties: {
|
|
275
|
+
...defaults,
|
|
247
276
|
...snapshot.observed,
|
|
248
277
|
...metadata,
|
|
249
278
|
$groups: {
|
|
@@ -265,6 +294,7 @@ class Analytics {
|
|
|
265
294
|
if (bytes > MAX_ANALYTICS_BYTES) this._warnOversized(message.event, bytes);
|
|
266
295
|
return;
|
|
267
296
|
}
|
|
297
|
+
const previous = this._core._state.session;
|
|
268
298
|
if (!this._core._state.sessionAdmitted(preparedSession)) {
|
|
269
299
|
if (!immediate) this._buffer.discardQueued(message);
|
|
270
300
|
return;
|
|
@@ -272,7 +302,15 @@ class Analytics {
|
|
|
272
302
|
if (personProperties) this._lastPersonProperties = personProperties.signature;
|
|
273
303
|
else if (Object.hasOwn(message.properties, '$set') || Object.hasOwn(message.properties, '$set_once') || Object.hasOwn(message.properties, '$unset')) this._lastPersonProperties = void 0;
|
|
274
304
|
const session = preparedSession.context;
|
|
305
|
+
if ('$pageview' === message.event) this._pageview = [
|
|
306
|
+
message.uuid,
|
|
307
|
+
session.sessionId
|
|
308
|
+
];
|
|
309
|
+
else if (this._pageview?.[1] !== session.sessionId) this._pageview = void 0;
|
|
275
310
|
this._sessionProperties.admitted(session.sessionId);
|
|
311
|
+
if (previous?.sessionId !== session.sessionId || previous.windowId !== session.windowId) this._sessionIdPublisher.publish({
|
|
312
|
+
...session
|
|
313
|
+
});
|
|
276
314
|
if (preparedSession.reason) this._newSessionPublisher.publish({
|
|
277
315
|
...session,
|
|
278
316
|
reason: preparedSession.reason
|
|
@@ -283,6 +321,10 @@ class Analytics {
|
|
|
283
321
|
}));
|
|
284
322
|
return message;
|
|
285
323
|
}
|
|
324
|
+
_pageviewIdFor(sessionId) {
|
|
325
|
+
const pageview = this._pageview;
|
|
326
|
+
return pageview && pageview[1] === sessionId ? pageview[0] : void 0;
|
|
327
|
+
}
|
|
286
328
|
_hasPersonProcessing() {
|
|
287
329
|
const state = this._core._state;
|
|
288
330
|
return shouldProcessPerson(this._personProfiles, state.isIdentified, Object.keys(state.groups).length > 0, true === this._core.kv.get('personProcessingEnabled'));
|
|
@@ -298,8 +340,30 @@ class Analytics {
|
|
|
298
340
|
_canMutate() {
|
|
299
341
|
if (this._disposed || !this._core._canMutate()) return false;
|
|
300
342
|
this._core._state.prepare();
|
|
343
|
+
this._adoptStoredIdentity();
|
|
301
344
|
return true;
|
|
302
345
|
}
|
|
346
|
+
_adoptStoredIdentity() {
|
|
347
|
+
const state = this._core._state;
|
|
348
|
+
const previousDistinctId = state.distinctId;
|
|
349
|
+
const wasIdentified = state.isIdentified;
|
|
350
|
+
if (!state.adoptStoredIdentity()) return;
|
|
351
|
+
if (!state.isIdentified) return this._resetIdentityState();
|
|
352
|
+
this._lastPersonProperties = void 0;
|
|
353
|
+
this._identifyPublisher.publish({
|
|
354
|
+
distinctId: state.distinctId,
|
|
355
|
+
previousDistinctId,
|
|
356
|
+
wasIdentified,
|
|
357
|
+
set: void 0,
|
|
358
|
+
setOnce: void 0
|
|
359
|
+
});
|
|
360
|
+
}
|
|
361
|
+
_resetIdentityState() {
|
|
362
|
+
this._lastPersonProperties = void 0;
|
|
363
|
+
this._pageview = void 0;
|
|
364
|
+
this._sessionProperties.clear();
|
|
365
|
+
this._resetPublisher.publish(void 0);
|
|
366
|
+
}
|
|
303
367
|
_canUseProperties() {
|
|
304
368
|
return !this._disposed && this._core._canMutate() && this._core._state.prepare();
|
|
305
369
|
}
|
|
@@ -370,12 +434,39 @@ class Analytics {
|
|
|
370
434
|
$anon_distinct_id: previousDistinctId
|
|
371
435
|
}, options, false, signature);
|
|
372
436
|
}
|
|
373
|
-
reset() {
|
|
374
|
-
if (!this._canMutate()) return;
|
|
375
|
-
this._core._state.
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
437
|
+
reset(options) {
|
|
438
|
+
if (this._disposed || !this._core._canMutate()) return;
|
|
439
|
+
this._core._state.prepare();
|
|
440
|
+
const { resetDeviceId, bootstrap } = true === options || false === options ? {
|
|
441
|
+
resetDeviceId: options
|
|
442
|
+
} : snapshotOptions(options);
|
|
443
|
+
const { distinctId, isIdentifiedId } = bootstrap ?? {};
|
|
444
|
+
this._core._state.reset(true === resetDeviceId, isValidDistinctId(distinctId) ? [
|
|
445
|
+
distinctId,
|
|
446
|
+
true === isIdentifiedId
|
|
447
|
+
] : void 0, this._bootstrapSession(bootstrap));
|
|
448
|
+
this._resetIdentityState();
|
|
449
|
+
return bootstrap;
|
|
450
|
+
}
|
|
451
|
+
bootstrap(options) {
|
|
452
|
+
const bootstrap = snapshotOptions(options);
|
|
453
|
+
const { distinctId, isIdentifiedId } = bootstrap;
|
|
454
|
+
const session = this._bootstrapSession(bootstrap);
|
|
455
|
+
if (session) this._core._state.bootstrapSession(session);
|
|
456
|
+
if (!isValidDistinctId(distinctId) || !this._canMutate()) return;
|
|
457
|
+
const state = this._core._state;
|
|
458
|
+
if (state.generated) return state.bootstrapIdentity(distinctId, true === isIdentifiedId);
|
|
459
|
+
if (true !== isIdentifiedId) return;
|
|
460
|
+
if (distinctId === state.distinctId) state.identify(distinctId);
|
|
461
|
+
else if (state.isIdentified) this._core.logger.warn('Ignored the bootstrap distinct ID of a different identified user');
|
|
462
|
+
else this.identify(distinctId);
|
|
463
|
+
}
|
|
464
|
+
_bootstrapSession(bootstrap) {
|
|
465
|
+
const sessionId = bootstrap?.sessionId;
|
|
466
|
+
if (void 0 === sessionId) return;
|
|
467
|
+
const session = readBootstrapSession(sessionId);
|
|
468
|
+
if (!session) this._core.logger.error('Ignored the bootstrap session ID: not a past UUIDv7');
|
|
469
|
+
return session;
|
|
379
470
|
}
|
|
380
471
|
_registration(properties) {
|
|
381
472
|
if (!this._canUseProperties()) return;
|
|
@@ -33,7 +33,8 @@ export interface AnalyticsDelivery extends LaneDelivery<AnalyticsMessage> {
|
|
|
33
33
|
}
|
|
34
34
|
/** Delivery driver attached to the client's event buffer. A driver without an immediate path reports immediate capture as unavailable. */
|
|
35
35
|
export interface AnalyticsDriver {
|
|
36
|
-
|
|
36
|
+
/** With `immediate`, one more drain is forced if an earlier batch fails before the events queued now are attempted. */
|
|
37
|
+
flush(immediate?: boolean): Promise<void>;
|
|
37
38
|
deliverImmediate?: AnalyticsDelivery['deliverImmediate'];
|
|
38
39
|
deliverUnload(messages: readonly AnalyticsMessage[]): void;
|
|
39
40
|
dispose(): Promise<void>;
|
|
@@ -46,7 +47,7 @@ export interface AnalyticsTeardownSubscription {
|
|
|
46
47
|
/** The client internals the capture pipeline reads. The client itself satisfies this. */
|
|
47
48
|
export interface AnalyticsCore extends Pick<Client, 'projectToken' | 'logger' | 'kv' | 'library' | 'onRemoteConfig'> {
|
|
48
49
|
readonly canCapture: boolean;
|
|
49
|
-
readonly _state: Pick<BrowserState, 'prepare' | 'distinctId' | 'deviceId' | 'groups' | 'activeSessionId' | 'isIdentified' | 'prepareSessionForEvent' | 'sessionAdmitted' | 'identify' | 'group' | 'reset' | 'resetGroups'>;
|
|
50
|
+
readonly _state: Pick<BrowserState, 'prepare' | 'distinctId' | 'deviceId' | 'groups' | 'activeSessionId' | 'isIdentified' | 'prepareSessionForEvent' | 'sessionAdmitted' | 'identify' | 'group' | 'reset' | 'resetGroups' | 'session' | 'generated' | 'bootstrapIdentity' | 'bootstrapSession' | 'adoptStoredIdentity'>;
|
|
50
51
|
readonly _sessionKv: KeyValueStore;
|
|
51
52
|
readonly _requestRuntime: RequestRuntime;
|
|
52
53
|
readonly _disposed: boolean;
|
|
@@ -216,7 +216,7 @@ const createAnalyticsDelivery = (buffer, client, host, options, beforeTeardown)=
|
|
|
216
216
|
};
|
|
217
217
|
lane.attach(delivery);
|
|
218
218
|
return {
|
|
219
|
-
flush: ()=>lane.flush(),
|
|
219
|
+
flush: (immediate)=>lane.flush(immediate),
|
|
220
220
|
deliverImmediate: delivery.deliverImmediate,
|
|
221
221
|
deliverUnload: (messages)=>delivery.teardown?.(messages, external_capture_v1_js_namespaceObject.CAPTURE_V1_TEARDOWN_BUDGET_BYTES),
|
|
222
222
|
dispose
|
|
@@ -184,7 +184,7 @@ const createAnalyticsDelivery = (buffer, client, host, options, beforeTeardown)=
|
|
|
184
184
|
};
|
|
185
185
|
lane.attach(delivery);
|
|
186
186
|
return {
|
|
187
|
-
flush: ()=>lane.flush(),
|
|
187
|
+
flush: (immediate)=>lane.flush(immediate),
|
|
188
188
|
deliverImmediate: delivery.deliverImmediate,
|
|
189
189
|
deliverUnload: (messages)=>delivery.teardown?.(messages, CAPTURE_V1_TEARDOWN_BUDGET_BYTES),
|
|
190
190
|
dispose
|
|
@@ -29,9 +29,15 @@ export declare class Lane<E> {
|
|
|
29
29
|
private _driveEpoch;
|
|
30
30
|
private _activeEpoch;
|
|
31
31
|
private _forceTarget;
|
|
32
|
+
/** The last event an immediate flush covered; it survives one failed drain until those events are attempted. */
|
|
33
|
+
private _immediateTarget;
|
|
32
34
|
constructor(_buffer: EventBuffer<E>, _onError?: (error: unknown) => void, _onAvailable?: () => void);
|
|
33
35
|
attach(delivery: LaneDelivery<E>): void;
|
|
34
|
-
|
|
36
|
+
/**
|
|
37
|
+
* Drives delivery through the events queued now. With `immediate`, one more drain is forced if an earlier batch
|
|
38
|
+
* fails before those events are attempted; behind a full batch of older events, they can still wait for the next drain.
|
|
39
|
+
*/
|
|
40
|
+
flush(immediate?: boolean): Promise<void>;
|
|
35
41
|
/** Pauses scheduled work while preserving active and queued accounting. */
|
|
36
42
|
pause(): void;
|
|
37
43
|
/** Redrives retained work after a connectivity transition. */
|
|
@@ -43,6 +49,7 @@ export declare class Lane<E> {
|
|
|
43
49
|
private _clearTimer;
|
|
44
50
|
private _trimQueue;
|
|
45
51
|
private _updateSettled;
|
|
52
|
+
private _takeUnattemptedImmediateTarget;
|
|
46
53
|
private _reportError;
|
|
47
54
|
private _reportAvailable;
|
|
48
55
|
private _resolveFlushWaiters;
|
|
@@ -44,6 +44,7 @@ class Lane {
|
|
|
44
44
|
} else if ('purge' === change) {
|
|
45
45
|
this._retryBlocked = false;
|
|
46
46
|
this._forceTarget = void 0;
|
|
47
|
+
this._immediateTarget = void 0;
|
|
47
48
|
this._clearTimer();
|
|
48
49
|
this._cancelActive();
|
|
49
50
|
} else {
|
|
@@ -61,11 +62,15 @@ class Lane {
|
|
|
61
62
|
this._buffer._expireQueue(this._buffer._now());
|
|
62
63
|
this._schedule();
|
|
63
64
|
}
|
|
64
|
-
flush() {
|
|
65
|
+
flush(immediate = false) {
|
|
65
66
|
this._buffer._expireQueue(this._buffer._now());
|
|
66
67
|
const attached = this._delivery;
|
|
67
|
-
if (!attached || !this._buffer._activeEntries && 0 === this._buffer._queue.length || !this._canDeliver(attached)) return Promise.resolve();
|
|
68
68
|
const target = this._buffer._nextId;
|
|
69
|
+
if (immediate) {
|
|
70
|
+
this._immediateTarget = target;
|
|
71
|
+
this._forceTarget = Math.max(this._forceTarget ?? 0, target);
|
|
72
|
+
}
|
|
73
|
+
if (!attached || !this._buffer._activeEntries && 0 === this._buffer._queue.length || !this._canDeliver(attached)) return Promise.resolve();
|
|
69
74
|
this._retryBlocked = false;
|
|
70
75
|
this._clearTimer();
|
|
71
76
|
this._forceTarget = Math.max(this._forceTarget ?? 0, target);
|
|
@@ -149,8 +154,14 @@ class Lane {
|
|
|
149
154
|
const firstPending = void 0 === activeId ? queuedId : void 0 === queuedId ? activeId : Math.min(activeId, queuedId);
|
|
150
155
|
this._settledId = void 0 === firstPending ? this._buffer._nextId : firstPending - 1;
|
|
151
156
|
if (void 0 !== this._forceTarget && this._settledId >= this._forceTarget) this._forceTarget = void 0;
|
|
157
|
+
if (void 0 !== this._immediateTarget && this._settledId >= this._immediateTarget) this._immediateTarget = void 0;
|
|
152
158
|
if (!this._drain) this._resolveFlushWaiters(false);
|
|
153
159
|
}
|
|
160
|
+
_takeUnattemptedImmediateTarget() {
|
|
161
|
+
const target = this._immediateTarget;
|
|
162
|
+
this._immediateTarget = void 0;
|
|
163
|
+
return void 0 !== target && this._buffer._queue.some((entry)=>entry[0] <= target && 0 === entry[4]) ? target : void 0;
|
|
164
|
+
}
|
|
154
165
|
_reportError(error) {
|
|
155
166
|
try {
|
|
156
167
|
this._onError(error);
|
|
@@ -241,7 +252,8 @@ class Lane {
|
|
|
241
252
|
return;
|
|
242
253
|
}
|
|
243
254
|
if (!this._canDeliver(attached)) return void this._clearTimer();
|
|
244
|
-
const
|
|
255
|
+
const head = this._buffer._queue[0];
|
|
256
|
+
const forced = void 0 !== this._forceTarget && head[0] <= this._forceTarget && !(this._retryBlocked && head[5] > this._buffer._now());
|
|
245
257
|
if (forced || !this._retryBlocked && this._buffer._queue.length >= this._readFlushAt(attached)) {
|
|
246
258
|
this._clearTimer();
|
|
247
259
|
this._beginDrive(false);
|
|
@@ -329,7 +341,7 @@ class Lane {
|
|
|
329
341
|
if (completionGeneration === this._buffer._generation) {
|
|
330
342
|
this._trimQueue();
|
|
331
343
|
this._retryBlocked = true;
|
|
332
|
-
this._forceTarget =
|
|
344
|
+
this._forceTarget = this._takeUnattemptedImmediateTarget();
|
|
333
345
|
this._updateSettled();
|
|
334
346
|
}
|
|
335
347
|
} else this._updateSettled();
|