@metamask-previews/analytics-controller 2.0.0-preview-fe4b3be11 → 2.1.0-preview-2f57247
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/CHANGELOG.md +12 -1
- package/README.md +46 -5
- package/dist/AnalyticsController-method-action-types.cjs.map +1 -1
- package/dist/AnalyticsController-method-action-types.d.cts +106 -2
- package/dist/AnalyticsController-method-action-types.d.cts.map +1 -1
- package/dist/AnalyticsController-method-action-types.d.mts +106 -2
- package/dist/AnalyticsController-method-action-types.d.mts.map +1 -1
- package/dist/AnalyticsController-method-action-types.mjs.map +1 -1
- package/dist/AnalyticsController.cjs +364 -14
- package/dist/AnalyticsController.cjs.map +1 -1
- package/dist/AnalyticsController.d.cts +121 -2
- package/dist/AnalyticsController.d.cts.map +1 -1
- package/dist/AnalyticsController.d.mts +121 -2
- package/dist/AnalyticsController.d.mts.map +1 -1
- package/dist/AnalyticsController.mjs +363 -13
- package/dist/AnalyticsController.mjs.map +1 -1
- package/dist/EventFragment.types.cjs +3 -0
- package/dist/EventFragment.types.cjs.map +1 -0
- package/dist/EventFragment.types.d.cts +111 -0
- package/dist/EventFragment.types.d.cts.map +1 -0
- package/dist/EventFragment.types.d.mts +111 -0
- package/dist/EventFragment.types.d.mts.map +1 -0
- package/dist/EventFragment.types.mjs +2 -0
- package/dist/EventFragment.types.mjs.map +1 -0
- package/dist/index.cjs +2 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +3 -2
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +3 -2
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +1 -1
- package/dist/index.mjs.map +1 -1
- package/dist/selectors.cjs +19 -0
- package/dist/selectors.cjs.map +1 -1
- package/dist/selectors.d.cts +3 -0
- package/dist/selectors.d.cts.map +1 -1
- package/dist/selectors.d.mts +3 -0
- package/dist/selectors.d.mts.map +1 -1
- package/dist/selectors.mjs +19 -0
- package/dist/selectors.mjs.map +1 -1
- package/package.json +2 -2
|
@@ -9,7 +9,7 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
|
|
|
9
9
|
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
10
10
|
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
11
11
|
};
|
|
12
|
-
var _AnalyticsController_instances, _AnalyticsController_platformAdapter, _AnalyticsController_isAnonymousEventsFeatureEnabled, _AnalyticsController_isEventQueuePersistenceEnabled, _AnalyticsController_isPreConsentQueueEnabled, _AnalyticsController_isGeolocationEnabled, _AnalyticsController_initPromise, _AnalyticsController_locationResolvePromise, _AnalyticsController_locationContext, _AnalyticsController_performInit, _AnalyticsController_maybeResolveLocation, _AnalyticsController_resolveLocationContext, _AnalyticsController_withLocationContext, _AnalyticsController_sendOrQueueTrackEvent, _AnalyticsController_sendOrQueueIdentifyEvent, _AnalyticsController_sendOrQueueViewEvent, _AnalyticsController_enqueueEvent, _AnalyticsController_sendQueuedEvent, _AnalyticsController_replayQueuedEvents, _AnalyticsController_removeQueuedEvent, _AnalyticsController_clearQueuedEvents, _AnalyticsController_enqueuePreConsentEvent, _AnalyticsController_replayPreConsentEvents, _AnalyticsController_enrichPreConsentEvent, _AnalyticsController_clearPreConsentEvents, _AnalyticsController_reconcilePreConsentEvents;
|
|
12
|
+
var _AnalyticsController_instances, _AnalyticsController_platformAdapter, _AnalyticsController_isAnonymousEventsFeatureEnabled, _AnalyticsController_isEventQueuePersistenceEnabled, _AnalyticsController_isPreConsentQueueEnabled, _AnalyticsController_isGeolocationEnabled, _AnalyticsController_isEventFragmentsEnabled, _AnalyticsController_initPromise, _AnalyticsController_locationResolvePromise, _AnalyticsController_locationContext, _AnalyticsController_performInit, _AnalyticsController_maybeResolveLocation, _AnalyticsController_resolveLocationContext, _AnalyticsController_withLocationContext, _AnalyticsController_sendOrQueueTrackEvent, _AnalyticsController_sendOrQueueIdentifyEvent, _AnalyticsController_sendOrQueueViewEvent, _AnalyticsController_enqueueEvent, _AnalyticsController_sendQueuedEvent, _AnalyticsController_replayQueuedEvents, _AnalyticsController_removeQueuedEvent, _AnalyticsController_clearQueuedEvents, _AnalyticsController_enqueuePreConsentEvent, _AnalyticsController_replayPreConsentEvents, _AnalyticsController_enrichPreConsentEvent, _AnalyticsController_clearPreConsentEvents, _AnalyticsController_reconcilePreConsentEvents, _AnalyticsController_reconcileEventFragments, _AnalyticsController_purgeStaleEventFragments, _AnalyticsController_getEventFragment, _AnalyticsController_setEventFragment, _AnalyticsController_removeEventFragment, _AnalyticsController_clearEventFragments, _AnalyticsController_shouldIgnoreEventFragmentCall, _AnalyticsController_emitEventFragment, _AnalyticsController_isAnalyticsCaptureAllowed;
|
|
13
13
|
import { BaseController } from "@metamask/base-controller";
|
|
14
14
|
import $lodash from "lodash";
|
|
15
15
|
const { cloneDeep } = $lodash;
|
|
@@ -24,6 +24,16 @@ import { analyticsControllerSelectors } from "./selectors.mjs";
|
|
|
24
24
|
* when composed with other controllers.
|
|
25
25
|
*/
|
|
26
26
|
export const controllerName = 'AnalyticsController';
|
|
27
|
+
/**
|
|
28
|
+
* Maximum age of a persisted event fragment, measured from
|
|
29
|
+
* {@link AnalyticsEventFragment.lastUpdated}.
|
|
30
|
+
*
|
|
31
|
+
* Fragments older than this are discarded during {@link AnalyticsController.init}
|
|
32
|
+
* without emitting a success or failure event. Confirmation journeys that span a
|
|
33
|
+
* restart are expected to resume within this window; abandoned ones must not keep
|
|
34
|
+
* `properties` or `sensitiveProperties` in storage indefinitely.
|
|
35
|
+
*/
|
|
36
|
+
export const EVENT_FRAGMENT_MAX_AGE = 24 * 60 * 60 * 1000;
|
|
27
37
|
/**
|
|
28
38
|
* Returns default values for AnalyticsController state.
|
|
29
39
|
*
|
|
@@ -75,6 +85,12 @@ const analyticsControllerMetadata = {
|
|
|
75
85
|
includeInDebugSnapshot: false,
|
|
76
86
|
usedInUi: false,
|
|
77
87
|
},
|
|
88
|
+
eventFragments: {
|
|
89
|
+
includeInStateLogs: false,
|
|
90
|
+
persist: true,
|
|
91
|
+
includeInDebugSnapshot: false,
|
|
92
|
+
usedInUi: false,
|
|
93
|
+
},
|
|
78
94
|
};
|
|
79
95
|
// === MESSENGER ===
|
|
80
96
|
const MESSENGER_EXPOSED_METHODS = [
|
|
@@ -84,6 +100,12 @@ const MESSENGER_EXPOSED_METHODS = [
|
|
|
84
100
|
'optIn',
|
|
85
101
|
'optOut',
|
|
86
102
|
'resetConsentDecision',
|
|
103
|
+
'createEventFragment',
|
|
104
|
+
'upsertEventFragment',
|
|
105
|
+
'updateEventFragment',
|
|
106
|
+
'getEventFragmentById',
|
|
107
|
+
'deleteEventFragment',
|
|
108
|
+
'finalizeEventFragment',
|
|
87
109
|
];
|
|
88
110
|
/**
|
|
89
111
|
* Returns whether a value is a non-array object.
|
|
@@ -155,6 +177,69 @@ function isAnalyticsQueuedEvent(value) {
|
|
|
155
177
|
}
|
|
156
178
|
return false;
|
|
157
179
|
}
|
|
180
|
+
/**
|
|
181
|
+
* Returns whether a value is a valid persisted event fragment.
|
|
182
|
+
*
|
|
183
|
+
* @param value - The value to check.
|
|
184
|
+
* @returns True if the value is an event fragment.
|
|
185
|
+
*/
|
|
186
|
+
function isAnalyticsEventFragment(value) {
|
|
187
|
+
if (!isRecord(value)) {
|
|
188
|
+
return false;
|
|
189
|
+
}
|
|
190
|
+
return (typeof value.id === 'string' &&
|
|
191
|
+
typeof value.createdAt === 'number' &&
|
|
192
|
+
typeof value.lastUpdated === 'number' &&
|
|
193
|
+
isRecord(value.properties) &&
|
|
194
|
+
isRecord(value.sensitiveProperties) &&
|
|
195
|
+
(value.initialEvent === undefined ||
|
|
196
|
+
typeof value.initialEvent === 'string') &&
|
|
197
|
+
(value.successEvent === undefined ||
|
|
198
|
+
typeof value.successEvent === 'string') &&
|
|
199
|
+
(value.failureEvent === undefined ||
|
|
200
|
+
typeof value.failureEvent === 'string') &&
|
|
201
|
+
(value.context === undefined || isRecord(value.context)) &&
|
|
202
|
+
(value.persist === undefined || typeof value.persist === 'boolean'));
|
|
203
|
+
}
|
|
204
|
+
/**
|
|
205
|
+
* Merges a payload into an event fragment.
|
|
206
|
+
*
|
|
207
|
+
* `properties`, `sensitiveProperties` and `context` are merged one level deep,
|
|
208
|
+
* so a key written twice is replaced rather than combined. This keeps array
|
|
209
|
+
* values predictable: writing a shorter array replaces the longer one instead
|
|
210
|
+
* of leaving stale trailing entries behind.
|
|
211
|
+
*
|
|
212
|
+
* @param fragment - The fragment to merge into.
|
|
213
|
+
* @param payload - The payload to merge.
|
|
214
|
+
* @returns A new fragment with the payload applied.
|
|
215
|
+
*/
|
|
216
|
+
function mergeEventFragment(fragment, payload) {
|
|
217
|
+
const context = mergeEventFragmentContext(fragment.context, payload.context);
|
|
218
|
+
return {
|
|
219
|
+
...fragment,
|
|
220
|
+
properties: { ...fragment.properties, ...(payload.properties ?? {}) },
|
|
221
|
+
sensitiveProperties: {
|
|
222
|
+
...fragment.sensitiveProperties,
|
|
223
|
+
...(payload.sensitiveProperties ?? {}),
|
|
224
|
+
},
|
|
225
|
+
...(context === undefined ? {} : { context }),
|
|
226
|
+
lastUpdated: Date.now(),
|
|
227
|
+
};
|
|
228
|
+
}
|
|
229
|
+
/**
|
|
230
|
+
* Merges two optional analytics contexts, preserving `undefined` when neither
|
|
231
|
+
* side has one so an empty context is never sent.
|
|
232
|
+
*
|
|
233
|
+
* @param base - The context to merge into.
|
|
234
|
+
* @param override - The context whose fields win.
|
|
235
|
+
* @returns The merged context, or `undefined` when both sides are unset.
|
|
236
|
+
*/
|
|
237
|
+
function mergeEventFragmentContext(base, override) {
|
|
238
|
+
if (base === undefined && override === undefined) {
|
|
239
|
+
return undefined;
|
|
240
|
+
}
|
|
241
|
+
return { ...(base ?? {}), ...(override ?? {}) };
|
|
242
|
+
}
|
|
158
243
|
/**
|
|
159
244
|
* The AnalyticsController manages analytics tracking across platforms (Mobile/Extension).
|
|
160
245
|
* It provides a unified interface for tracking events, identifying users, and managing
|
|
@@ -181,10 +266,11 @@ export class AnalyticsController extends BaseController {
|
|
|
181
266
|
* @param options.isEventQueuePersistenceEnabled - Whether analytics event queue persistence is enabled
|
|
182
267
|
* @param options.isPreConsentQueueEnabled - Whether the pre-consent event queue is enabled
|
|
183
268
|
* @param options.isGeolocationEnabled - Whether geolocation enrichment is enabled
|
|
269
|
+
* @param options.isEventFragmentsEnabled - Whether the event fragments feature is enabled
|
|
184
270
|
* @throws Error if state.analyticsId is missing or not a valid UUIDv4
|
|
185
271
|
* @remarks After construction, call {@link AnalyticsController.init} to complete initialization.
|
|
186
272
|
*/
|
|
187
|
-
constructor({ state, messenger, platformAdapter, isAnonymousEventsFeatureEnabled = false, isEventQueuePersistenceEnabled = false, isPreConsentQueueEnabled = false, isGeolocationEnabled = false, }) {
|
|
273
|
+
constructor({ state, messenger, platformAdapter, isAnonymousEventsFeatureEnabled = false, isEventQueuePersistenceEnabled = false, isPreConsentQueueEnabled = false, isGeolocationEnabled = false, isEventFragmentsEnabled = false, }) {
|
|
188
274
|
const initialState = {
|
|
189
275
|
...getDefaultAnalyticsControllerState(),
|
|
190
276
|
...state,
|
|
@@ -202,6 +288,7 @@ export class AnalyticsController extends BaseController {
|
|
|
202
288
|
_AnalyticsController_isEventQueuePersistenceEnabled.set(this, void 0);
|
|
203
289
|
_AnalyticsController_isPreConsentQueueEnabled.set(this, void 0);
|
|
204
290
|
_AnalyticsController_isGeolocationEnabled.set(this, void 0);
|
|
291
|
+
_AnalyticsController_isEventFragmentsEnabled.set(this, void 0);
|
|
205
292
|
/**
|
|
206
293
|
* The in-flight (or settled) initialization promise. Set on the first
|
|
207
294
|
* {@link init} call and returned by subsequent calls so overlapping callers
|
|
@@ -218,6 +305,7 @@ export class AnalyticsController extends BaseController {
|
|
|
218
305
|
__classPrivateFieldSet(this, _AnalyticsController_isEventQueuePersistenceEnabled, isEventQueuePersistenceEnabled, "f");
|
|
219
306
|
__classPrivateFieldSet(this, _AnalyticsController_isPreConsentQueueEnabled, isPreConsentQueueEnabled, "f");
|
|
220
307
|
__classPrivateFieldSet(this, _AnalyticsController_isGeolocationEnabled, isGeolocationEnabled, "f");
|
|
308
|
+
__classPrivateFieldSet(this, _AnalyticsController_isEventFragmentsEnabled, isEventFragmentsEnabled, "f");
|
|
221
309
|
__classPrivateFieldSet(this, _AnalyticsController_platformAdapter, platformAdapter, "f");
|
|
222
310
|
__classPrivateFieldSet(this, _AnalyticsController_initPromise, undefined, "f");
|
|
223
311
|
__classPrivateFieldSet(this, _AnalyticsController_locationResolvePromise, undefined, "f");
|
|
@@ -230,6 +318,7 @@ export class AnalyticsController extends BaseController {
|
|
|
230
318
|
eventQueuePersistenceEnabled: __classPrivateFieldGet(this, _AnalyticsController_isEventQueuePersistenceEnabled, "f"),
|
|
231
319
|
preConsentQueueEnabled: __classPrivateFieldGet(this, _AnalyticsController_isPreConsentQueueEnabled, "f"),
|
|
232
320
|
geolocationEnabled: __classPrivateFieldGet(this, _AnalyticsController_isGeolocationEnabled, "f"),
|
|
321
|
+
eventFragmentsEnabled: __classPrivateFieldGet(this, _AnalyticsController_isEventFragmentsEnabled, "f"),
|
|
233
322
|
});
|
|
234
323
|
}
|
|
235
324
|
/**
|
|
@@ -267,15 +356,11 @@ export class AnalyticsController extends BaseController {
|
|
|
267
356
|
* @param context - Optional platform-specific context forwarded to the platform adapter.
|
|
268
357
|
*/
|
|
269
358
|
trackEvent(event, context) {
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
const shouldQueuePreConsent = __classPrivateFieldGet(this, _AnalyticsController_isPreConsentQueueEnabled, "f") && !this.state.consentDecisionMade;
|
|
276
|
-
if (!shouldQueuePreConsent) {
|
|
277
|
-
return;
|
|
278
|
-
}
|
|
359
|
+
// An event captured while the user is still undecided is held in the
|
|
360
|
+
// pre-consent queue (see #sendOrQueueTrackEvent) instead of being
|
|
361
|
+
// delivered, and replayed if they later opt in.
|
|
362
|
+
if (!__classPrivateFieldGet(this, _AnalyticsController_instances, "m", _AnalyticsController_isAnalyticsCaptureAllowed).call(this)) {
|
|
363
|
+
return;
|
|
279
364
|
}
|
|
280
365
|
// if event does not have properties, send event without properties
|
|
281
366
|
// and return to prevent any additional processing
|
|
@@ -333,6 +418,160 @@ export class AnalyticsController extends BaseController {
|
|
|
333
418
|
// Delegate to platform adapter
|
|
334
419
|
__classPrivateFieldGet(this, _AnalyticsController_instances, "m", _AnalyticsController_sendOrQueueViewEvent).call(this, name, properties, __classPrivateFieldGet(this, _AnalyticsController_instances, "m", _AnalyticsController_withLocationContext).call(this, context));
|
|
335
420
|
}
|
|
421
|
+
/**
|
|
422
|
+
* Create an event fragment.
|
|
423
|
+
*
|
|
424
|
+
* A fragment accumulates properties across a user journey so that several
|
|
425
|
+
* parts of a client can contribute to the same set of events without
|
|
426
|
+
* re-deriving them. Declaring `successEvent` and `failureEvent` turns the
|
|
427
|
+
* fragment into a funnel that {@link finalizeEventFragment} closes. Declaring
|
|
428
|
+
* none of the event names makes it a pure property bag that the client reads
|
|
429
|
+
* back with {@link getEventFragmentById} when it emits its own events.
|
|
430
|
+
*
|
|
431
|
+
* Any existing fragment with the same ID is replaced, so a new journey never
|
|
432
|
+
* inherits properties from a stale one.
|
|
433
|
+
*
|
|
434
|
+
* Nothing is created unless the user is opted in, or undecided with the
|
|
435
|
+
* pre-consent queue enabled, so an opted-out user accumulates no fragment
|
|
436
|
+
* data.
|
|
437
|
+
*
|
|
438
|
+
* @param options - The fragment definition. An ID is generated when one is
|
|
439
|
+
* not supplied.
|
|
440
|
+
* @returns A read-only copy of the created fragment, or `undefined` when the
|
|
441
|
+
* event fragments feature is disabled or the consent state does not allow
|
|
442
|
+
* capture. Mutating the returned object does not change controller state.
|
|
443
|
+
* Use {@link updateEventFragment} or {@link upsertEventFragment} to write.
|
|
444
|
+
*/
|
|
445
|
+
createEventFragment(options = {}) {
|
|
446
|
+
if (__classPrivateFieldGet(this, _AnalyticsController_instances, "m", _AnalyticsController_shouldIgnoreEventFragmentCall).call(this, 'createEventFragment')) {
|
|
447
|
+
return undefined;
|
|
448
|
+
}
|
|
449
|
+
const now = Date.now();
|
|
450
|
+
const fragment = {
|
|
451
|
+
id: options.id ?? uuid(),
|
|
452
|
+
properties: { ...(options.properties ?? {}) },
|
|
453
|
+
sensitiveProperties: { ...(options.sensitiveProperties ?? {}) },
|
|
454
|
+
createdAt: now,
|
|
455
|
+
lastUpdated: now,
|
|
456
|
+
...(options.initialEvent === undefined
|
|
457
|
+
? {}
|
|
458
|
+
: { initialEvent: options.initialEvent }),
|
|
459
|
+
...(options.successEvent === undefined
|
|
460
|
+
? {}
|
|
461
|
+
: { successEvent: options.successEvent }),
|
|
462
|
+
...(options.failureEvent === undefined
|
|
463
|
+
? {}
|
|
464
|
+
: { failureEvent: options.failureEvent }),
|
|
465
|
+
...(options.context === undefined
|
|
466
|
+
? {}
|
|
467
|
+
: { context: { ...options.context } }),
|
|
468
|
+
...(options.persist === undefined ? {} : { persist: options.persist }),
|
|
469
|
+
};
|
|
470
|
+
__classPrivateFieldGet(this, _AnalyticsController_instances, "m", _AnalyticsController_setEventFragment).call(this, fragment);
|
|
471
|
+
if (fragment.initialEvent) {
|
|
472
|
+
__classPrivateFieldGet(this, _AnalyticsController_instances, "m", _AnalyticsController_emitEventFragment).call(this, fragment, fragment.initialEvent, fragment.context);
|
|
473
|
+
}
|
|
474
|
+
return cloneDeep(fragment);
|
|
475
|
+
}
|
|
476
|
+
/**
|
|
477
|
+
* Write to an event fragment, creating a property bag if none exists.
|
|
478
|
+
*
|
|
479
|
+
* This is the ergonomic entry point for contributors that do not know
|
|
480
|
+
* whether the journey has been started yet, and it avoids the read then
|
|
481
|
+
* write race a caller would otherwise have to implement itself.
|
|
482
|
+
*
|
|
483
|
+
* @param id - The fragment ID.
|
|
484
|
+
* @param payload - The properties and context to merge in.
|
|
485
|
+
*/
|
|
486
|
+
upsertEventFragment(id, payload = {}) {
|
|
487
|
+
if (__classPrivateFieldGet(this, _AnalyticsController_instances, "m", _AnalyticsController_shouldIgnoreEventFragmentCall).call(this, 'upsertEventFragment')) {
|
|
488
|
+
return;
|
|
489
|
+
}
|
|
490
|
+
const fragment = __classPrivateFieldGet(this, _AnalyticsController_instances, "m", _AnalyticsController_getEventFragment).call(this, id);
|
|
491
|
+
if (!fragment) {
|
|
492
|
+
this.createEventFragment({ id, ...payload });
|
|
493
|
+
return;
|
|
494
|
+
}
|
|
495
|
+
__classPrivateFieldGet(this, _AnalyticsController_instances, "m", _AnalyticsController_setEventFragment).call(this, mergeEventFragment(fragment, payload));
|
|
496
|
+
}
|
|
497
|
+
/**
|
|
498
|
+
* Write to an existing event fragment.
|
|
499
|
+
*
|
|
500
|
+
* @param id - The fragment ID.
|
|
501
|
+
* @param payload - The properties and context to merge in.
|
|
502
|
+
* @throws Error if no fragment has that ID when the call is not ignored.
|
|
503
|
+
* Use {@link upsertEventFragment} when the fragment may not exist yet.
|
|
504
|
+
* When the event fragments feature is disabled or the consent state does not
|
|
505
|
+
* allow capture, the call is a logged no-op and does not throw.
|
|
506
|
+
*/
|
|
507
|
+
updateEventFragment(id, payload = {}) {
|
|
508
|
+
if (__classPrivateFieldGet(this, _AnalyticsController_instances, "m", _AnalyticsController_shouldIgnoreEventFragmentCall).call(this, 'updateEventFragment')) {
|
|
509
|
+
return;
|
|
510
|
+
}
|
|
511
|
+
const fragment = __classPrivateFieldGet(this, _AnalyticsController_instances, "m", _AnalyticsController_getEventFragment).call(this, id);
|
|
512
|
+
if (!fragment) {
|
|
513
|
+
throw new Error(`Event fragment with id ${id} does not exist.`);
|
|
514
|
+
}
|
|
515
|
+
__classPrivateFieldGet(this, _AnalyticsController_instances, "m", _AnalyticsController_setEventFragment).call(this, mergeEventFragment(fragment, payload));
|
|
516
|
+
}
|
|
517
|
+
/**
|
|
518
|
+
* Read an event fragment.
|
|
519
|
+
*
|
|
520
|
+
* @param id - The fragment ID.
|
|
521
|
+
* @returns A read-only copy of the fragment, or `undefined` when no fragment
|
|
522
|
+
* has that ID, the event fragments feature is disabled, or the consent state
|
|
523
|
+
* does not allow capture. Mutating the returned object does not change
|
|
524
|
+
* controller state. Use {@link updateEventFragment} or
|
|
525
|
+
* {@link upsertEventFragment} to write.
|
|
526
|
+
*/
|
|
527
|
+
getEventFragmentById(id) {
|
|
528
|
+
if (__classPrivateFieldGet(this, _AnalyticsController_instances, "m", _AnalyticsController_shouldIgnoreEventFragmentCall).call(this, 'getEventFragmentById')) {
|
|
529
|
+
return undefined;
|
|
530
|
+
}
|
|
531
|
+
const fragment = __classPrivateFieldGet(this, _AnalyticsController_instances, "m", _AnalyticsController_getEventFragment).call(this, id);
|
|
532
|
+
return fragment === undefined ? undefined : cloneDeep(fragment);
|
|
533
|
+
}
|
|
534
|
+
/**
|
|
535
|
+
* Discard an event fragment without emitting anything.
|
|
536
|
+
*
|
|
537
|
+
* @param id - The fragment ID.
|
|
538
|
+
*/
|
|
539
|
+
deleteEventFragment(id) {
|
|
540
|
+
if (__classPrivateFieldGet(this, _AnalyticsController_instances, "m", _AnalyticsController_shouldIgnoreEventFragmentCall).call(this, 'deleteEventFragment')) {
|
|
541
|
+
return;
|
|
542
|
+
}
|
|
543
|
+
__classPrivateFieldGet(this, _AnalyticsController_instances, "m", _AnalyticsController_removeEventFragment).call(this, id);
|
|
544
|
+
}
|
|
545
|
+
/**
|
|
546
|
+
* Close an event fragment, emitting its closing event and discarding it.
|
|
547
|
+
*
|
|
548
|
+
* The event emitted is `failureEvent` when the journey was abandoned and
|
|
549
|
+
* `successEvent` otherwise. A fragment that does not declare the relevant
|
|
550
|
+
* event name is discarded silently, which is what makes a pure property bag
|
|
551
|
+
* possible.
|
|
552
|
+
*
|
|
553
|
+
* @param id - The fragment ID.
|
|
554
|
+
* @param options - Finalization options.
|
|
555
|
+
* @param options.abandoned - Whether the journey was abandoned.
|
|
556
|
+
* @param options.context - Context merged over the fragment's own context.
|
|
557
|
+
* @throws Error if no fragment has that ID when the call is not ignored.
|
|
558
|
+
* When the event fragments feature is disabled or the consent state does not
|
|
559
|
+
* allow capture, the call is a logged no-op and does not throw.
|
|
560
|
+
*/
|
|
561
|
+
finalizeEventFragment(id, { abandoned = false, context } = {}) {
|
|
562
|
+
if (__classPrivateFieldGet(this, _AnalyticsController_instances, "m", _AnalyticsController_shouldIgnoreEventFragmentCall).call(this, 'finalizeEventFragment')) {
|
|
563
|
+
return;
|
|
564
|
+
}
|
|
565
|
+
const fragment = __classPrivateFieldGet(this, _AnalyticsController_instances, "m", _AnalyticsController_getEventFragment).call(this, id);
|
|
566
|
+
if (!fragment) {
|
|
567
|
+
throw new Error(`Event fragment with id ${id} does not exist.`);
|
|
568
|
+
}
|
|
569
|
+
const eventName = abandoned ? fragment.failureEvent : fragment.successEvent;
|
|
570
|
+
if (eventName) {
|
|
571
|
+
__classPrivateFieldGet(this, _AnalyticsController_instances, "m", _AnalyticsController_emitEventFragment).call(this, fragment, eventName, mergeEventFragmentContext(fragment.context, context));
|
|
572
|
+
}
|
|
573
|
+
__classPrivateFieldGet(this, _AnalyticsController_instances, "m", _AnalyticsController_removeEventFragment).call(this, id);
|
|
574
|
+
}
|
|
336
575
|
/**
|
|
337
576
|
* Opt in to analytics.
|
|
338
577
|
*
|
|
@@ -363,7 +602,8 @@ export class AnalyticsController extends BaseController {
|
|
|
363
602
|
* Opt out of analytics.
|
|
364
603
|
*
|
|
365
604
|
* Records that a consent decision has been made and discards any persisted
|
|
366
|
-
* events so nothing captured before the
|
|
605
|
+
* events and in-progress event fragments so nothing captured before the
|
|
606
|
+
* decision is ever delivered.
|
|
367
607
|
*/
|
|
368
608
|
optOut() {
|
|
369
609
|
this.update((state) => {
|
|
@@ -372,6 +612,7 @@ export class AnalyticsController extends BaseController {
|
|
|
372
612
|
});
|
|
373
613
|
__classPrivateFieldGet(this, _AnalyticsController_instances, "m", _AnalyticsController_clearQueuedEvents).call(this);
|
|
374
614
|
__classPrivateFieldGet(this, _AnalyticsController_instances, "m", _AnalyticsController_clearPreConsentEvents).call(this);
|
|
615
|
+
__classPrivateFieldGet(this, _AnalyticsController_instances, "m", _AnalyticsController_clearEventFragments).call(this);
|
|
375
616
|
}
|
|
376
617
|
/**
|
|
377
618
|
* Reset the consent decision back to undecided.
|
|
@@ -380,6 +621,10 @@ export class AnalyticsController extends BaseController {
|
|
|
380
621
|
* preference and discards the delivery queue, but preserves any pre-consent
|
|
381
622
|
* events so they can still be replayed if the user opts in again. The user is
|
|
382
623
|
* treated as undecided again.
|
|
624
|
+
*
|
|
625
|
+
* In-progress event fragments are kept only while the undecided user can
|
|
626
|
+
* still accumulate them, and discarded otherwise, so no fragment outlives the
|
|
627
|
+
* consent state that allowed it.
|
|
383
628
|
*/
|
|
384
629
|
resetConsentDecision() {
|
|
385
630
|
this.update((state) => {
|
|
@@ -387,15 +632,29 @@ export class AnalyticsController extends BaseController {
|
|
|
387
632
|
state.consentDecisionMade = false;
|
|
388
633
|
});
|
|
389
634
|
__classPrivateFieldGet(this, _AnalyticsController_instances, "m", _AnalyticsController_clearQueuedEvents).call(this);
|
|
635
|
+
if (!__classPrivateFieldGet(this, _AnalyticsController_instances, "m", _AnalyticsController_isAnalyticsCaptureAllowed).call(this)) {
|
|
636
|
+
__classPrivateFieldGet(this, _AnalyticsController_instances, "m", _AnalyticsController_clearEventFragments).call(this);
|
|
637
|
+
}
|
|
390
638
|
}
|
|
391
639
|
}
|
|
392
|
-
_AnalyticsController_platformAdapter = new WeakMap(), _AnalyticsController_isAnonymousEventsFeatureEnabled = new WeakMap(), _AnalyticsController_isEventQueuePersistenceEnabled = new WeakMap(), _AnalyticsController_isPreConsentQueueEnabled = new WeakMap(), _AnalyticsController_isGeolocationEnabled = new WeakMap(), _AnalyticsController_initPromise = new WeakMap(), _AnalyticsController_locationResolvePromise = new WeakMap(), _AnalyticsController_locationContext = new WeakMap(), _AnalyticsController_instances = new WeakSet(), _AnalyticsController_performInit =
|
|
640
|
+
_AnalyticsController_platformAdapter = new WeakMap(), _AnalyticsController_isAnonymousEventsFeatureEnabled = new WeakMap(), _AnalyticsController_isEventQueuePersistenceEnabled = new WeakMap(), _AnalyticsController_isPreConsentQueueEnabled = new WeakMap(), _AnalyticsController_isGeolocationEnabled = new WeakMap(), _AnalyticsController_isEventFragmentsEnabled = new WeakMap(), _AnalyticsController_initPromise = new WeakMap(), _AnalyticsController_locationResolvePromise = new WeakMap(), _AnalyticsController_locationContext = new WeakMap(), _AnalyticsController_instances = new WeakSet(), _AnalyticsController_performInit =
|
|
393
641
|
/**
|
|
394
642
|
* Performs the one-time initialization work: resolve geolocation, run the
|
|
395
643
|
* platform adapter's onSetupCompleted lifecycle hook, then replay any queued
|
|
396
644
|
* and pre-consent events.
|
|
397
645
|
*/
|
|
398
646
|
async function _AnalyticsController_performInit() {
|
|
647
|
+
// Snapshot fragment IDs and createdAt before any awaited init work so
|
|
648
|
+
// reconciliation can tell previous-session leftovers from fragments
|
|
649
|
+
// created or replaced while init runs.
|
|
650
|
+
const initEventFragmentSnapshot = new Map();
|
|
651
|
+
for (const [id, fragment] of Object.entries(this.state.eventFragments ?? {})) {
|
|
652
|
+
if (isAnalyticsEventFragment(fragment) &&
|
|
653
|
+
fragment.id === id &&
|
|
654
|
+
typeof fragment.createdAt === 'number') {
|
|
655
|
+
initEventFragmentSnapshot.set(id, fragment.createdAt);
|
|
656
|
+
}
|
|
657
|
+
}
|
|
399
658
|
// Resolve geolocation only when the user is already opted in; for undecided
|
|
400
659
|
// or opted-out users it is deferred to {@link optIn}. Awaited so that an
|
|
401
660
|
// already-opted-in session has location available before events replay.
|
|
@@ -411,6 +670,7 @@ async function _AnalyticsController_performInit() {
|
|
|
411
670
|
}
|
|
412
671
|
__classPrivateFieldGet(this, _AnalyticsController_instances, "m", _AnalyticsController_replayQueuedEvents).call(this);
|
|
413
672
|
__classPrivateFieldGet(this, _AnalyticsController_instances, "m", _AnalyticsController_reconcilePreConsentEvents).call(this);
|
|
673
|
+
__classPrivateFieldGet(this, _AnalyticsController_instances, "m", _AnalyticsController_reconcileEventFragments).call(this, initEventFragmentSnapshot);
|
|
414
674
|
}, _AnalyticsController_maybeResolveLocation = function _AnalyticsController_maybeResolveLocation() {
|
|
415
675
|
if (__classPrivateFieldGet(this, _AnalyticsController_isGeolocationEnabled, "f") &&
|
|
416
676
|
__classPrivateFieldGet(this, _AnalyticsController_locationResolvePromise, "f") === undefined &&
|
|
@@ -633,5 +893,95 @@ async function _AnalyticsController_performInit() {
|
|
|
633
893
|
else if (this.state.consentDecisionMade) {
|
|
634
894
|
__classPrivateFieldGet(this, _AnalyticsController_instances, "m", _AnalyticsController_clearPreConsentEvents).call(this);
|
|
635
895
|
}
|
|
896
|
+
}, _AnalyticsController_reconcileEventFragments = function _AnalyticsController_reconcileEventFragments(initEventFragmentSnapshot) {
|
|
897
|
+
const fragments = this.state.eventFragments;
|
|
898
|
+
if (!fragments) {
|
|
899
|
+
return;
|
|
900
|
+
}
|
|
901
|
+
if (!__classPrivateFieldGet(this, _AnalyticsController_isEventFragmentsEnabled, "f") || !__classPrivateFieldGet(this, _AnalyticsController_instances, "m", _AnalyticsController_isAnalyticsCaptureAllowed).call(this)) {
|
|
902
|
+
__classPrivateFieldGet(this, _AnalyticsController_instances, "m", _AnalyticsController_clearEventFragments).call(this);
|
|
903
|
+
return;
|
|
904
|
+
}
|
|
905
|
+
__classPrivateFieldGet(this, _AnalyticsController_instances, "m", _AnalyticsController_purgeStaleEventFragments).call(this, fragments, initEventFragmentSnapshot);
|
|
906
|
+
}, _AnalyticsController_purgeStaleEventFragments = function _AnalyticsController_purgeStaleEventFragments(currentEventFragments, initEventFragmentSnapshot) {
|
|
907
|
+
const eventFragments = {};
|
|
908
|
+
const now = Date.now();
|
|
909
|
+
for (const [id, fragment] of Object.entries(currentEventFragments)) {
|
|
910
|
+
if (!isAnalyticsEventFragment(fragment) || fragment.id !== id) {
|
|
911
|
+
log('Dropping invalid persisted event fragment', { id });
|
|
912
|
+
continue;
|
|
913
|
+
}
|
|
914
|
+
if (now - fragment.lastUpdated > EVENT_FRAGMENT_MAX_AGE) {
|
|
915
|
+
log('Dropping expired persisted event fragment', { id });
|
|
916
|
+
continue;
|
|
917
|
+
}
|
|
918
|
+
const snapshotCreatedAt = initEventFragmentSnapshot.get(id);
|
|
919
|
+
if (fragment.persist === true ||
|
|
920
|
+
snapshotCreatedAt === undefined ||
|
|
921
|
+
fragment.createdAt !== snapshotCreatedAt) {
|
|
922
|
+
eventFragments[id] = fragment;
|
|
923
|
+
}
|
|
924
|
+
}
|
|
925
|
+
if (Object.keys(eventFragments).length ===
|
|
926
|
+
Object.keys(currentEventFragments).length) {
|
|
927
|
+
return;
|
|
928
|
+
}
|
|
929
|
+
this.update((state) => {
|
|
930
|
+
state.eventFragments = eventFragments;
|
|
931
|
+
});
|
|
932
|
+
}, _AnalyticsController_getEventFragment = function _AnalyticsController_getEventFragment(id) {
|
|
933
|
+
return this.state.eventFragments?.[id];
|
|
934
|
+
}, _AnalyticsController_setEventFragment = function _AnalyticsController_setEventFragment(fragment) {
|
|
935
|
+
const eventFragments = {
|
|
936
|
+
...this.state.eventFragments,
|
|
937
|
+
[fragment.id]: fragment,
|
|
938
|
+
};
|
|
939
|
+
this.update((state) => {
|
|
940
|
+
state.eventFragments = eventFragments;
|
|
941
|
+
});
|
|
942
|
+
}, _AnalyticsController_removeEventFragment = function _AnalyticsController_removeEventFragment(id) {
|
|
943
|
+
const currentEventFragments = this.state.eventFragments;
|
|
944
|
+
if (!currentEventFragments ||
|
|
945
|
+
!Object.prototype.hasOwnProperty.call(currentEventFragments, id)) {
|
|
946
|
+
return;
|
|
947
|
+
}
|
|
948
|
+
const { [id]: _deletedFragment, ...eventFragments } = currentEventFragments;
|
|
949
|
+
this.update((state) => {
|
|
950
|
+
state.eventFragments = eventFragments;
|
|
951
|
+
});
|
|
952
|
+
}, _AnalyticsController_clearEventFragments = function _AnalyticsController_clearEventFragments() {
|
|
953
|
+
if (!this.state.eventFragments ||
|
|
954
|
+
Object.keys(this.state.eventFragments).length === 0) {
|
|
955
|
+
return;
|
|
956
|
+
}
|
|
957
|
+
this.update((state) => {
|
|
958
|
+
state.eventFragments = {};
|
|
959
|
+
});
|
|
960
|
+
}, _AnalyticsController_shouldIgnoreEventFragmentCall = function _AnalyticsController_shouldIgnoreEventFragmentCall(method) {
|
|
961
|
+
if (!__classPrivateFieldGet(this, _AnalyticsController_isEventFragmentsEnabled, "f")) {
|
|
962
|
+
log('Ignoring event fragment call because the event fragments feature is disabled', { method });
|
|
963
|
+
return true;
|
|
964
|
+
}
|
|
965
|
+
if (!__classPrivateFieldGet(this, _AnalyticsController_instances, "m", _AnalyticsController_isAnalyticsCaptureAllowed).call(this)) {
|
|
966
|
+
log('Ignoring event fragment call because the consent state does not allow capturing analytics', { method });
|
|
967
|
+
return true;
|
|
968
|
+
}
|
|
969
|
+
return false;
|
|
970
|
+
}, _AnalyticsController_emitEventFragment = function _AnalyticsController_emitEventFragment(fragment, name, context) {
|
|
971
|
+
const properties = { ...fragment.properties };
|
|
972
|
+
const sensitiveProperties = { ...fragment.sensitiveProperties };
|
|
973
|
+
this.trackEvent({
|
|
974
|
+
name,
|
|
975
|
+
properties,
|
|
976
|
+
sensitiveProperties,
|
|
977
|
+
saveDataRecording: false,
|
|
978
|
+
hasProperties: Object.keys(properties).length > 0 ||
|
|
979
|
+
Object.keys(sensitiveProperties).length > 0,
|
|
980
|
+
}, context);
|
|
981
|
+
}, _AnalyticsController_isAnalyticsCaptureAllowed = function _AnalyticsController_isAnalyticsCaptureAllowed() {
|
|
982
|
+
if (analyticsControllerSelectors.selectEnabled(this.state)) {
|
|
983
|
+
return true;
|
|
984
|
+
}
|
|
985
|
+
return __classPrivateFieldGet(this, _AnalyticsController_isPreConsentQueueEnabled, "f") && !this.state.consentDecisionMade;
|
|
636
986
|
};
|
|
637
987
|
//# sourceMappingURL=AnalyticsController.mjs.map
|