@metamask-previews/analytics-controller 1.0.1-preview-9fb8d00e7 → 1.1.0-preview-e43dfcb
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 +6 -1
- package/README.md +9 -0
- package/dist/AnalyticsController-method-action-types.cjs.map +1 -1
- package/dist/AnalyticsController-method-action-types.d.cts +3 -0
- package/dist/AnalyticsController-method-action-types.d.cts.map +1 -1
- package/dist/AnalyticsController-method-action-types.d.mts +3 -0
- 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 +197 -13
- package/dist/AnalyticsController.cjs.map +1 -1
- package/dist/AnalyticsController.d.cts +80 -5
- package/dist/AnalyticsController.d.cts.map +1 -1
- package/dist/AnalyticsController.d.mts +80 -5
- package/dist/AnalyticsController.d.mts.map +1 -1
- package/dist/AnalyticsController.mjs +198 -13
- package/dist/AnalyticsController.mjs.map +1 -1
- package/dist/AnalyticsPlatformAdapter.types.cjs.map +1 -1
- package/dist/AnalyticsPlatformAdapter.types.d.cts +36 -3
- package/dist/AnalyticsPlatformAdapter.types.d.cts.map +1 -1
- package/dist/AnalyticsPlatformAdapter.types.d.mts +36 -3
- package/dist/AnalyticsPlatformAdapter.types.d.mts.map +1 -1
- package/dist/AnalyticsPlatformAdapter.types.mjs.map +1 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +2 -2
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +2 -2
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs.map +1 -1
- package/package.json +5 -2
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import type { ControllerGetStateAction, ControllerStateChangeEvent } from "@metamask/base-controller";
|
|
2
2
|
import { BaseController } from "@metamask/base-controller";
|
|
3
3
|
import type { Messenger } from "@metamask/messenger";
|
|
4
|
+
import type { Json } from "@metamask/utils";
|
|
4
5
|
import type { AnalyticsControllerMethodActions } from "./AnalyticsController-method-action-types.cjs";
|
|
5
|
-
import type { AnalyticsPlatformAdapter, AnalyticsEventProperties, AnalyticsUserTraits, AnalyticsTrackingEvent } from "./AnalyticsPlatformAdapter.types.cjs";
|
|
6
|
+
import type { AnalyticsPlatformAdapter, AnalyticsContext, AnalyticsEventProperties, AnalyticsUserTraits, AnalyticsTrackingEvent } from "./AnalyticsPlatformAdapter.types.cjs";
|
|
6
7
|
/**
|
|
7
8
|
* The name of the {@link AnalyticsController}, used to namespace the
|
|
8
9
|
* controller's actions and events and to namespace the controller's state data
|
|
@@ -23,7 +24,68 @@ export type AnalyticsControllerState = {
|
|
|
23
24
|
* Must be provided by the platform - the controller does not generate it.
|
|
24
25
|
*/
|
|
25
26
|
analyticsId: string;
|
|
27
|
+
/**
|
|
28
|
+
* Persisted queue of analytics events waiting for delivery acknowledgement.
|
|
29
|
+
* This is only used when event queue persistence is enabled.
|
|
30
|
+
*/
|
|
31
|
+
eventQueue?: Record<string, Json>;
|
|
32
|
+
};
|
|
33
|
+
/**
|
|
34
|
+
* Event types supported by the persisted analytics event queue.
|
|
35
|
+
*/
|
|
36
|
+
export type AnalyticsQueuedEventType = 'track' | 'identify' | 'view';
|
|
37
|
+
/**
|
|
38
|
+
* Base persisted event queue entry.
|
|
39
|
+
*/
|
|
40
|
+
export type AnalyticsQueuedEventBase = {
|
|
41
|
+
/**
|
|
42
|
+
* Event type used to replay the payload with the platform adapter.
|
|
43
|
+
*/
|
|
44
|
+
type: AnalyticsQueuedEventType;
|
|
45
|
+
/**
|
|
46
|
+
* Stable identifier for the analytics payload.
|
|
47
|
+
*/
|
|
48
|
+
messageId: string;
|
|
49
|
+
/**
|
|
50
|
+
* Original payload timestamp serialized for persistence.
|
|
51
|
+
*/
|
|
52
|
+
timestamp: string;
|
|
53
|
+
};
|
|
54
|
+
/**
|
|
55
|
+
* Persisted track event queue entry.
|
|
56
|
+
*/
|
|
57
|
+
export type AnalyticsQueuedTrackEvent = AnalyticsQueuedEventBase & {
|
|
58
|
+
type: 'track';
|
|
59
|
+
eventName: string;
|
|
60
|
+
properties?: AnalyticsEventProperties;
|
|
61
|
+
context?: AnalyticsContext;
|
|
26
62
|
};
|
|
63
|
+
/**
|
|
64
|
+
* Persisted identify event queue entry.
|
|
65
|
+
*/
|
|
66
|
+
export type AnalyticsQueuedIdentifyEvent = AnalyticsQueuedEventBase & {
|
|
67
|
+
type: 'identify';
|
|
68
|
+
userId: string;
|
|
69
|
+
traits?: AnalyticsUserTraits;
|
|
70
|
+
context?: AnalyticsContext;
|
|
71
|
+
};
|
|
72
|
+
/**
|
|
73
|
+
* Persisted view event queue entry.
|
|
74
|
+
*/
|
|
75
|
+
export type AnalyticsQueuedViewEvent = AnalyticsQueuedEventBase & {
|
|
76
|
+
type: 'view';
|
|
77
|
+
name: string;
|
|
78
|
+
properties?: AnalyticsEventProperties;
|
|
79
|
+
context?: AnalyticsContext;
|
|
80
|
+
};
|
|
81
|
+
/**
|
|
82
|
+
* Persisted analytics event queue entry.
|
|
83
|
+
*/
|
|
84
|
+
export type AnalyticsQueuedEvent = AnalyticsQueuedTrackEvent | AnalyticsQueuedIdentifyEvent | AnalyticsQueuedViewEvent;
|
|
85
|
+
/**
|
|
86
|
+
* Persisted analytics event queue keyed by message ID.
|
|
87
|
+
*/
|
|
88
|
+
export type AnalyticsEventQueue = Record<string, AnalyticsQueuedEvent>;
|
|
27
89
|
/**
|
|
28
90
|
* Returns default values for AnalyticsController state.
|
|
29
91
|
*
|
|
@@ -86,6 +148,15 @@ export type AnalyticsControllerOptions = {
|
|
|
86
148
|
* @default false
|
|
87
149
|
*/
|
|
88
150
|
isAnonymousEventsFeatureEnabled?: boolean;
|
|
151
|
+
/**
|
|
152
|
+
* Whether analytics event queue persistence is enabled.
|
|
153
|
+
*
|
|
154
|
+
* When enabled, AnalyticsController persists each platform adapter payload
|
|
155
|
+
* until the adapter reports successful delivery.
|
|
156
|
+
*
|
|
157
|
+
* @default false
|
|
158
|
+
*/
|
|
159
|
+
isEventQueuePersistenceEnabled?: boolean;
|
|
89
160
|
};
|
|
90
161
|
/**
|
|
91
162
|
* The AnalyticsController manages analytics tracking across platforms (Mobile/Extension).
|
|
@@ -111,10 +182,11 @@ export declare class AnalyticsController extends BaseController<'AnalyticsContro
|
|
|
111
182
|
* @param options.messenger - Messenger used to communicate with BaseController
|
|
112
183
|
* @param options.platformAdapter - Platform adapter implementation for tracking
|
|
113
184
|
* @param options.isAnonymousEventsFeatureEnabled - Whether the anonymous events feature is enabled
|
|
185
|
+
* @param options.isEventQueuePersistenceEnabled - Whether analytics event queue persistence is enabled
|
|
114
186
|
* @throws Error if state.analyticsId is missing or not a valid UUIDv4
|
|
115
187
|
* @remarks After construction, call {@link AnalyticsController.init} to complete initialization.
|
|
116
188
|
*/
|
|
117
|
-
constructor({ state, messenger, platformAdapter, isAnonymousEventsFeatureEnabled, }: AnalyticsControllerOptions);
|
|
189
|
+
constructor({ state, messenger, platformAdapter, isAnonymousEventsFeatureEnabled, isEventQueuePersistenceEnabled, }: AnalyticsControllerOptions);
|
|
118
190
|
/**
|
|
119
191
|
* Initialize the controller by calling the platform adapter's onSetupCompleted lifecycle hook.
|
|
120
192
|
* This method must be called after construction to complete the setup process.
|
|
@@ -126,21 +198,24 @@ export declare class AnalyticsController extends BaseController<'AnalyticsContro
|
|
|
126
198
|
* Events are only tracked if analytics is enabled.
|
|
127
199
|
*
|
|
128
200
|
* @param event - Analytics event with properties and sensitive properties
|
|
201
|
+
* @param context - Optional platform-specific context forwarded to the platform adapter.
|
|
129
202
|
*/
|
|
130
|
-
trackEvent(event: AnalyticsTrackingEvent): void;
|
|
203
|
+
trackEvent(event: AnalyticsTrackingEvent, context?: AnalyticsContext): void;
|
|
131
204
|
/**
|
|
132
205
|
* Identify a user for analytics.
|
|
133
206
|
*
|
|
134
207
|
* @param traits - User traits/properties
|
|
208
|
+
* @param context - Optional platform-specific context forwarded to the platform adapter.
|
|
135
209
|
*/
|
|
136
|
-
identify(traits?: AnalyticsUserTraits): void;
|
|
210
|
+
identify(traits?: AnalyticsUserTraits, context?: AnalyticsContext): void;
|
|
137
211
|
/**
|
|
138
212
|
* Track a page or screen view.
|
|
139
213
|
*
|
|
140
214
|
* @param name - The identifier/name of the page or screen being viewed (e.g., "home", "settings", "wallet")
|
|
141
215
|
* @param properties - Optional properties associated with the view
|
|
216
|
+
* @param context - Optional platform-specific context forwarded to the platform adapter.
|
|
142
217
|
*/
|
|
143
|
-
trackView(name: string, properties?: AnalyticsEventProperties): void;
|
|
218
|
+
trackView(name: string, properties?: AnalyticsEventProperties, context?: AnalyticsContext): void;
|
|
144
219
|
/**
|
|
145
220
|
* Opt in to analytics.
|
|
146
221
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AnalyticsController.d.cts","sourceRoot":"","sources":["../src/AnalyticsController.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,wBAAwB,EACxB,0BAA0B,EAE3B,kCAAkC;AACnC,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAC3D,OAAO,KAAK,EAAE,SAAS,EAAE,4BAA4B;
|
|
1
|
+
{"version":3,"file":"AnalyticsController.d.cts","sourceRoot":"","sources":["../src/AnalyticsController.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,wBAAwB,EACxB,0BAA0B,EAE3B,kCAAkC;AACnC,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAC3D,OAAO,KAAK,EAAE,SAAS,EAAE,4BAA4B;AACrD,OAAO,KAAK,EAAE,IAAI,EAAE,wBAAwB;AAI5C,OAAO,KAAK,EAAE,gCAAgC,EAAE,sDAAkD;AAGlG,OAAO,KAAK,EACV,wBAAwB,EAExB,gBAAgB,EAChB,wBAAwB,EACxB,mBAAmB,EACnB,sBAAsB,EACvB,6CAAyC;AAK1C;;;;GAIG;AACH,eAAO,MAAM,cAAc,wBAAwB,CAAC;AAIpD;;GAEG;AACH,MAAM,MAAM,wBA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G;IACH,IAAI,IAAI,IAAI;IA+PZ;;;;;;;OAOG;IACH,UAAU,CAAC,KAAK,EAAE,sBAAsB,EAAE,OAAO,CAAC,EAAE,gBAAgB,GAAG,IAAI;IA0C3E;;;;;OAKG;IACH,QAAQ,CAAC,MAAM,CAAC,EAAE,mBAAmB,EAAE,OAAO,CAAC,EAAE,gBAAgB,GAAG,IAAI;IASxE;;;;;;OAMG;IACH,SAAS,CACP,IAAI,EAAE,MAAM,EACZ,UAAU,CAAC,EAAE,wBAAwB,EACrC,OAAO,CAAC,EAAE,gBAAgB,GACzB,IAAI;IASP;;OAEG;IACH,KAAK,IAAI,IAAI;IAMb;;OAEG;IACH,MAAM,IAAI,IAAI;CAOf"}
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import type { ControllerGetStateAction, ControllerStateChangeEvent } from "@metamask/base-controller";
|
|
2
2
|
import { BaseController } from "@metamask/base-controller";
|
|
3
3
|
import type { Messenger } from "@metamask/messenger";
|
|
4
|
+
import type { Json } from "@metamask/utils";
|
|
4
5
|
import type { AnalyticsControllerMethodActions } from "./AnalyticsController-method-action-types.mjs";
|
|
5
|
-
import type { AnalyticsPlatformAdapter, AnalyticsEventProperties, AnalyticsUserTraits, AnalyticsTrackingEvent } from "./AnalyticsPlatformAdapter.types.mjs";
|
|
6
|
+
import type { AnalyticsPlatformAdapter, AnalyticsContext, AnalyticsEventProperties, AnalyticsUserTraits, AnalyticsTrackingEvent } from "./AnalyticsPlatformAdapter.types.mjs";
|
|
6
7
|
/**
|
|
7
8
|
* The name of the {@link AnalyticsController}, used to namespace the
|
|
8
9
|
* controller's actions and events and to namespace the controller's state data
|
|
@@ -23,7 +24,68 @@ export type AnalyticsControllerState = {
|
|
|
23
24
|
* Must be provided by the platform - the controller does not generate it.
|
|
24
25
|
*/
|
|
25
26
|
analyticsId: string;
|
|
27
|
+
/**
|
|
28
|
+
* Persisted queue of analytics events waiting for delivery acknowledgement.
|
|
29
|
+
* This is only used when event queue persistence is enabled.
|
|
30
|
+
*/
|
|
31
|
+
eventQueue?: Record<string, Json>;
|
|
32
|
+
};
|
|
33
|
+
/**
|
|
34
|
+
* Event types supported by the persisted analytics event queue.
|
|
35
|
+
*/
|
|
36
|
+
export type AnalyticsQueuedEventType = 'track' | 'identify' | 'view';
|
|
37
|
+
/**
|
|
38
|
+
* Base persisted event queue entry.
|
|
39
|
+
*/
|
|
40
|
+
export type AnalyticsQueuedEventBase = {
|
|
41
|
+
/**
|
|
42
|
+
* Event type used to replay the payload with the platform adapter.
|
|
43
|
+
*/
|
|
44
|
+
type: AnalyticsQueuedEventType;
|
|
45
|
+
/**
|
|
46
|
+
* Stable identifier for the analytics payload.
|
|
47
|
+
*/
|
|
48
|
+
messageId: string;
|
|
49
|
+
/**
|
|
50
|
+
* Original payload timestamp serialized for persistence.
|
|
51
|
+
*/
|
|
52
|
+
timestamp: string;
|
|
53
|
+
};
|
|
54
|
+
/**
|
|
55
|
+
* Persisted track event queue entry.
|
|
56
|
+
*/
|
|
57
|
+
export type AnalyticsQueuedTrackEvent = AnalyticsQueuedEventBase & {
|
|
58
|
+
type: 'track';
|
|
59
|
+
eventName: string;
|
|
60
|
+
properties?: AnalyticsEventProperties;
|
|
61
|
+
context?: AnalyticsContext;
|
|
26
62
|
};
|
|
63
|
+
/**
|
|
64
|
+
* Persisted identify event queue entry.
|
|
65
|
+
*/
|
|
66
|
+
export type AnalyticsQueuedIdentifyEvent = AnalyticsQueuedEventBase & {
|
|
67
|
+
type: 'identify';
|
|
68
|
+
userId: string;
|
|
69
|
+
traits?: AnalyticsUserTraits;
|
|
70
|
+
context?: AnalyticsContext;
|
|
71
|
+
};
|
|
72
|
+
/**
|
|
73
|
+
* Persisted view event queue entry.
|
|
74
|
+
*/
|
|
75
|
+
export type AnalyticsQueuedViewEvent = AnalyticsQueuedEventBase & {
|
|
76
|
+
type: 'view';
|
|
77
|
+
name: string;
|
|
78
|
+
properties?: AnalyticsEventProperties;
|
|
79
|
+
context?: AnalyticsContext;
|
|
80
|
+
};
|
|
81
|
+
/**
|
|
82
|
+
* Persisted analytics event queue entry.
|
|
83
|
+
*/
|
|
84
|
+
export type AnalyticsQueuedEvent = AnalyticsQueuedTrackEvent | AnalyticsQueuedIdentifyEvent | AnalyticsQueuedViewEvent;
|
|
85
|
+
/**
|
|
86
|
+
* Persisted analytics event queue keyed by message ID.
|
|
87
|
+
*/
|
|
88
|
+
export type AnalyticsEventQueue = Record<string, AnalyticsQueuedEvent>;
|
|
27
89
|
/**
|
|
28
90
|
* Returns default values for AnalyticsController state.
|
|
29
91
|
*
|
|
@@ -86,6 +148,15 @@ export type AnalyticsControllerOptions = {
|
|
|
86
148
|
* @default false
|
|
87
149
|
*/
|
|
88
150
|
isAnonymousEventsFeatureEnabled?: boolean;
|
|
151
|
+
/**
|
|
152
|
+
* Whether analytics event queue persistence is enabled.
|
|
153
|
+
*
|
|
154
|
+
* When enabled, AnalyticsController persists each platform adapter payload
|
|
155
|
+
* until the adapter reports successful delivery.
|
|
156
|
+
*
|
|
157
|
+
* @default false
|
|
158
|
+
*/
|
|
159
|
+
isEventQueuePersistenceEnabled?: boolean;
|
|
89
160
|
};
|
|
90
161
|
/**
|
|
91
162
|
* The AnalyticsController manages analytics tracking across platforms (Mobile/Extension).
|
|
@@ -111,10 +182,11 @@ export declare class AnalyticsController extends BaseController<'AnalyticsContro
|
|
|
111
182
|
* @param options.messenger - Messenger used to communicate with BaseController
|
|
112
183
|
* @param options.platformAdapter - Platform adapter implementation for tracking
|
|
113
184
|
* @param options.isAnonymousEventsFeatureEnabled - Whether the anonymous events feature is enabled
|
|
185
|
+
* @param options.isEventQueuePersistenceEnabled - Whether analytics event queue persistence is enabled
|
|
114
186
|
* @throws Error if state.analyticsId is missing or not a valid UUIDv4
|
|
115
187
|
* @remarks After construction, call {@link AnalyticsController.init} to complete initialization.
|
|
116
188
|
*/
|
|
117
|
-
constructor({ state, messenger, platformAdapter, isAnonymousEventsFeatureEnabled, }: AnalyticsControllerOptions);
|
|
189
|
+
constructor({ state, messenger, platformAdapter, isAnonymousEventsFeatureEnabled, isEventQueuePersistenceEnabled, }: AnalyticsControllerOptions);
|
|
118
190
|
/**
|
|
119
191
|
* Initialize the controller by calling the platform adapter's onSetupCompleted lifecycle hook.
|
|
120
192
|
* This method must be called after construction to complete the setup process.
|
|
@@ -126,21 +198,24 @@ export declare class AnalyticsController extends BaseController<'AnalyticsContro
|
|
|
126
198
|
* Events are only tracked if analytics is enabled.
|
|
127
199
|
*
|
|
128
200
|
* @param event - Analytics event with properties and sensitive properties
|
|
201
|
+
* @param context - Optional platform-specific context forwarded to the platform adapter.
|
|
129
202
|
*/
|
|
130
|
-
trackEvent(event: AnalyticsTrackingEvent): void;
|
|
203
|
+
trackEvent(event: AnalyticsTrackingEvent, context?: AnalyticsContext): void;
|
|
131
204
|
/**
|
|
132
205
|
* Identify a user for analytics.
|
|
133
206
|
*
|
|
134
207
|
* @param traits - User traits/properties
|
|
208
|
+
* @param context - Optional platform-specific context forwarded to the platform adapter.
|
|
135
209
|
*/
|
|
136
|
-
identify(traits?: AnalyticsUserTraits): void;
|
|
210
|
+
identify(traits?: AnalyticsUserTraits, context?: AnalyticsContext): void;
|
|
137
211
|
/**
|
|
138
212
|
* Track a page or screen view.
|
|
139
213
|
*
|
|
140
214
|
* @param name - The identifier/name of the page or screen being viewed (e.g., "home", "settings", "wallet")
|
|
141
215
|
* @param properties - Optional properties associated with the view
|
|
216
|
+
* @param context - Optional platform-specific context forwarded to the platform adapter.
|
|
142
217
|
*/
|
|
143
|
-
trackView(name: string, properties?: AnalyticsEventProperties): void;
|
|
218
|
+
trackView(name: string, properties?: AnalyticsEventProperties, context?: AnalyticsContext): void;
|
|
144
219
|
/**
|
|
145
220
|
* Opt in to analytics.
|
|
146
221
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AnalyticsController.d.mts","sourceRoot":"","sources":["../src/AnalyticsController.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,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B,GAAG,SAAS,CAClD,OAAO,cAAc,EACrB,0BAA0B,GAAG,cAAc,EAC3C,yBAAyB,GAAG,aAAa,CAC1C,CAAC;AAIF;;GAEG;AACH,MAAM,MAAM,0BAA0B,GAAG;IACvC;;;;OAIG;IACH,KAAK,EAAE,wBAAwB,CAAC;IAChC;;OAEG;IACH,SAAS,EAAE,4BAA4B,CAAC;IACxC;;OAEG;IACH,eAAe,EAAE,wBAAwB,CAAC;IAE1C;;;;OAIG;IACH,+BAA+B,CAAC,EAAE,OAAO,CAAC;IAE1C;;;;;;;OAOG;IACH,8BAA8B,CAAC,EAAE,OAAO,CAAC;CAC1C,CAAC;AAyDF;;;;;;;;;;;;GAYG;AACH,qBAAa,mBAAoB,SAAQ,cAAc,CACrD,qBAAqB,EACrB,wBAAwB,EACxB,4BAA4B,CAC7B;;IASC;;;;;;;;;;;;OAYG;gBACS,EACV,KAAK,EACL,SAAS,EACT,eAAe,EACf,+BAAuC,EACvC,8BAAsC,GACvC,EAAE,0BAA0B;IAoC7B;;;OAGG;IACH,IAAI,IAAI,IAAI;IA+PZ;;;;;;;OAOG;IACH,UAAU,CAAC,KAAK,EAAE,sBAAsB,EAAE,OAAO,CAAC,EAAE,gBAAgB,GAAG,IAAI;IA0C3E;;;;;OAKG;IACH,QAAQ,CAAC,MAAM,CAAC,EAAE,mBAAmB,EAAE,OAAO,CAAC,EAAE,gBAAgB,GAAG,IAAI;IASxE;;;;;;OAMG;IACH,SAAS,CACP,IAAI,EAAE,MAAM,EACZ,UAAU,CAAC,EAAE,wBAAwB,EACrC,OAAO,CAAC,EAAE,gBAAgB,GACzB,IAAI;IASP;;OAEG;IACH,KAAK,IAAI,IAAI;IAMb;;OAEG;IACH,MAAM,IAAI,IAAI;CAOf"}
|
|
@@ -9,8 +9,11 @@ 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_platformAdapter, _AnalyticsController_isAnonymousEventsFeatureEnabled, _AnalyticsController_initialized;
|
|
12
|
+
var _AnalyticsController_instances, _AnalyticsController_platformAdapter, _AnalyticsController_isAnonymousEventsFeatureEnabled, _AnalyticsController_isEventQueuePersistenceEnabled, _AnalyticsController_initialized, _AnalyticsController_sendOrQueueTrackEvent, _AnalyticsController_sendOrQueueIdentifyEvent, _AnalyticsController_sendOrQueueViewEvent, _AnalyticsController_enqueueEvent, _AnalyticsController_sendQueuedEvent, _AnalyticsController_replayQueuedEvents, _AnalyticsController_removeQueuedEvent, _AnalyticsController_clearQueuedEvents;
|
|
13
13
|
import { BaseController } from "@metamask/base-controller";
|
|
14
|
+
import $lodash from "lodash";
|
|
15
|
+
const { cloneDeep } = $lodash;
|
|
16
|
+
import { v4 as uuid } from "uuid";
|
|
14
17
|
import { validateAnalyticsControllerState } from "./analyticsControllerStateValidator.mjs";
|
|
15
18
|
import { projectLogger as log } from "./AnalyticsLogger.mjs";
|
|
16
19
|
import { analyticsControllerSelectors } from "./selectors.mjs";
|
|
@@ -53,6 +56,12 @@ const analyticsControllerMetadata = {
|
|
|
53
56
|
includeInDebugSnapshot: true,
|
|
54
57
|
usedInUi: false,
|
|
55
58
|
},
|
|
59
|
+
eventQueue: {
|
|
60
|
+
includeInStateLogs: false,
|
|
61
|
+
persist: true,
|
|
62
|
+
includeInDebugSnapshot: false,
|
|
63
|
+
usedInUi: false,
|
|
64
|
+
},
|
|
56
65
|
};
|
|
57
66
|
// === MESSENGER ===
|
|
58
67
|
const MESSENGER_EXPOSED_METHODS = [
|
|
@@ -62,6 +71,46 @@ const MESSENGER_EXPOSED_METHODS = [
|
|
|
62
71
|
'optIn',
|
|
63
72
|
'optOut',
|
|
64
73
|
];
|
|
74
|
+
/**
|
|
75
|
+
* Returns whether a value is a non-array object.
|
|
76
|
+
*
|
|
77
|
+
* @param value - The value to check.
|
|
78
|
+
* @returns True if the value is a record.
|
|
79
|
+
*/
|
|
80
|
+
function isRecord(value) {
|
|
81
|
+
return value !== null && typeof value === 'object' && !Array.isArray(value);
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Returns whether a value is a valid persisted analytics event.
|
|
85
|
+
*
|
|
86
|
+
* @param value - The value to check.
|
|
87
|
+
* @returns True if the value is a queued analytics event.
|
|
88
|
+
*/
|
|
89
|
+
function isAnalyticsQueuedEvent(value) {
|
|
90
|
+
if (!isRecord(value)) {
|
|
91
|
+
return false;
|
|
92
|
+
}
|
|
93
|
+
if (typeof value.messageId !== 'string' ||
|
|
94
|
+
typeof value.timestamp !== 'string') {
|
|
95
|
+
return false;
|
|
96
|
+
}
|
|
97
|
+
if (value.type === 'track') {
|
|
98
|
+
return (typeof value.eventName === 'string' &&
|
|
99
|
+
(value.properties === undefined || isRecord(value.properties)) &&
|
|
100
|
+
(value.context === undefined || isRecord(value.context)));
|
|
101
|
+
}
|
|
102
|
+
if (value.type === 'identify') {
|
|
103
|
+
return (typeof value.userId === 'string' &&
|
|
104
|
+
(value.traits === undefined || isRecord(value.traits)) &&
|
|
105
|
+
(value.context === undefined || isRecord(value.context)));
|
|
106
|
+
}
|
|
107
|
+
if (value.type === 'view') {
|
|
108
|
+
return (typeof value.name === 'string' &&
|
|
109
|
+
(value.properties === undefined || isRecord(value.properties)) &&
|
|
110
|
+
(value.context === undefined || isRecord(value.context)));
|
|
111
|
+
}
|
|
112
|
+
return false;
|
|
113
|
+
}
|
|
65
114
|
/**
|
|
66
115
|
* The AnalyticsController manages analytics tracking across platforms (Mobile/Extension).
|
|
67
116
|
* It provides a unified interface for tracking events, identifying users, and managing
|
|
@@ -85,10 +134,11 @@ export class AnalyticsController extends BaseController {
|
|
|
85
134
|
* @param options.messenger - Messenger used to communicate with BaseController
|
|
86
135
|
* @param options.platformAdapter - Platform adapter implementation for tracking
|
|
87
136
|
* @param options.isAnonymousEventsFeatureEnabled - Whether the anonymous events feature is enabled
|
|
137
|
+
* @param options.isEventQueuePersistenceEnabled - Whether analytics event queue persistence is enabled
|
|
88
138
|
* @throws Error if state.analyticsId is missing or not a valid UUIDv4
|
|
89
139
|
* @remarks After construction, call {@link AnalyticsController.init} to complete initialization.
|
|
90
140
|
*/
|
|
91
|
-
constructor({ state, messenger, platformAdapter, isAnonymousEventsFeatureEnabled = false, }) {
|
|
141
|
+
constructor({ state, messenger, platformAdapter, isAnonymousEventsFeatureEnabled = false, isEventQueuePersistenceEnabled = false, }) {
|
|
92
142
|
const initialState = {
|
|
93
143
|
...getDefaultAnalyticsControllerState(),
|
|
94
144
|
...state,
|
|
@@ -100,10 +150,13 @@ export class AnalyticsController extends BaseController {
|
|
|
100
150
|
state: initialState,
|
|
101
151
|
messenger,
|
|
102
152
|
});
|
|
153
|
+
_AnalyticsController_instances.add(this);
|
|
103
154
|
_AnalyticsController_platformAdapter.set(this, void 0);
|
|
104
155
|
_AnalyticsController_isAnonymousEventsFeatureEnabled.set(this, void 0);
|
|
156
|
+
_AnalyticsController_isEventQueuePersistenceEnabled.set(this, void 0);
|
|
105
157
|
_AnalyticsController_initialized.set(this, void 0);
|
|
106
158
|
__classPrivateFieldSet(this, _AnalyticsController_isAnonymousEventsFeatureEnabled, isAnonymousEventsFeatureEnabled, "f");
|
|
159
|
+
__classPrivateFieldSet(this, _AnalyticsController_isEventQueuePersistenceEnabled, isEventQueuePersistenceEnabled, "f");
|
|
107
160
|
__classPrivateFieldSet(this, _AnalyticsController_platformAdapter, platformAdapter, "f");
|
|
108
161
|
__classPrivateFieldSet(this, _AnalyticsController_initialized, false, "f");
|
|
109
162
|
this.messenger.registerMethodActionHandlers(this, MESSENGER_EXPOSED_METHODS);
|
|
@@ -111,6 +164,7 @@ export class AnalyticsController extends BaseController {
|
|
|
111
164
|
enabled: analyticsControllerSelectors.selectEnabled(this.state),
|
|
112
165
|
optedIn: this.state.optedIn,
|
|
113
166
|
analyticsId: this.state.analyticsId,
|
|
167
|
+
eventQueuePersistenceEnabled: __classPrivateFieldGet(this, _AnalyticsController_isEventQueuePersistenceEnabled, "f"),
|
|
114
168
|
});
|
|
115
169
|
}
|
|
116
170
|
/**
|
|
@@ -132,6 +186,7 @@ export class AnalyticsController extends BaseController {
|
|
|
132
186
|
// Log error but don't throw - adapter setup failure shouldn't break controller
|
|
133
187
|
log('Error calling platformAdapter.onSetupCompleted', error);
|
|
134
188
|
}
|
|
189
|
+
__classPrivateFieldGet(this, _AnalyticsController_instances, "m", _AnalyticsController_replayQueuedEvents).call(this);
|
|
135
190
|
}
|
|
136
191
|
/**
|
|
137
192
|
* Track an analytics event.
|
|
@@ -139,8 +194,9 @@ export class AnalyticsController extends BaseController {
|
|
|
139
194
|
* Events are only tracked if analytics is enabled.
|
|
140
195
|
*
|
|
141
196
|
* @param event - Analytics event with properties and sensitive properties
|
|
197
|
+
* @param context - Optional platform-specific context forwarded to the platform adapter.
|
|
142
198
|
*/
|
|
143
|
-
trackEvent(event) {
|
|
199
|
+
trackEvent(event, context) {
|
|
144
200
|
// Don't track if analytics is disabled
|
|
145
201
|
if (!analyticsControllerSelectors.selectEnabled(this.state)) {
|
|
146
202
|
return;
|
|
@@ -148,50 +204,52 @@ export class AnalyticsController extends BaseController {
|
|
|
148
204
|
// if event does not have properties, send event without properties
|
|
149
205
|
// and return to prevent any additional processing
|
|
150
206
|
if (!event.hasProperties) {
|
|
151
|
-
__classPrivateFieldGet(this,
|
|
207
|
+
__classPrivateFieldGet(this, _AnalyticsController_instances, "m", _AnalyticsController_sendOrQueueTrackEvent).call(this, event.name, undefined, context);
|
|
152
208
|
return;
|
|
153
209
|
}
|
|
154
210
|
// Track regular properties first if anonymous events feature is enabled
|
|
155
211
|
if (__classPrivateFieldGet(this, _AnalyticsController_isAnonymousEventsFeatureEnabled, "f")) {
|
|
156
212
|
// Note: Even if regular properties object is empty, we still send it to ensure
|
|
157
213
|
// an event with user ID is tracked.
|
|
158
|
-
__classPrivateFieldGet(this,
|
|
214
|
+
__classPrivateFieldGet(this, _AnalyticsController_instances, "m", _AnalyticsController_sendOrQueueTrackEvent).call(this, event.name, {
|
|
159
215
|
...event.properties,
|
|
160
|
-
});
|
|
216
|
+
}, context);
|
|
161
217
|
}
|
|
162
218
|
const hasSensitiveProperties = Object.keys(event.sensitiveProperties).length > 0;
|
|
163
219
|
if (!__classPrivateFieldGet(this, _AnalyticsController_isAnonymousEventsFeatureEnabled, "f") || hasSensitiveProperties) {
|
|
164
|
-
__classPrivateFieldGet(this,
|
|
220
|
+
__classPrivateFieldGet(this, _AnalyticsController_instances, "m", _AnalyticsController_sendOrQueueTrackEvent).call(this, event.name, {
|
|
165
221
|
...event.properties,
|
|
166
222
|
...event.sensitiveProperties,
|
|
167
223
|
...(hasSensitiveProperties && { anonymous: true }),
|
|
168
|
-
});
|
|
224
|
+
}, context);
|
|
169
225
|
}
|
|
170
226
|
}
|
|
171
227
|
/**
|
|
172
228
|
* Identify a user for analytics.
|
|
173
229
|
*
|
|
174
230
|
* @param traits - User traits/properties
|
|
231
|
+
* @param context - Optional platform-specific context forwarded to the platform adapter.
|
|
175
232
|
*/
|
|
176
|
-
identify(traits) {
|
|
233
|
+
identify(traits, context) {
|
|
177
234
|
if (!analyticsControllerSelectors.selectEnabled(this.state)) {
|
|
178
235
|
return;
|
|
179
236
|
}
|
|
180
237
|
// Delegate to platform adapter using the current analytics ID
|
|
181
|
-
__classPrivateFieldGet(this,
|
|
238
|
+
__classPrivateFieldGet(this, _AnalyticsController_instances, "m", _AnalyticsController_sendOrQueueIdentifyEvent).call(this, this.state.analyticsId, traits, context);
|
|
182
239
|
}
|
|
183
240
|
/**
|
|
184
241
|
* Track a page or screen view.
|
|
185
242
|
*
|
|
186
243
|
* @param name - The identifier/name of the page or screen being viewed (e.g., "home", "settings", "wallet")
|
|
187
244
|
* @param properties - Optional properties associated with the view
|
|
245
|
+
* @param context - Optional platform-specific context forwarded to the platform adapter.
|
|
188
246
|
*/
|
|
189
|
-
trackView(name, properties) {
|
|
247
|
+
trackView(name, properties, context) {
|
|
190
248
|
if (!analyticsControllerSelectors.selectEnabled(this.state)) {
|
|
191
249
|
return;
|
|
192
250
|
}
|
|
193
251
|
// Delegate to platform adapter
|
|
194
|
-
__classPrivateFieldGet(this,
|
|
252
|
+
__classPrivateFieldGet(this, _AnalyticsController_instances, "m", _AnalyticsController_sendOrQueueViewEvent).call(this, name, properties, context);
|
|
195
253
|
}
|
|
196
254
|
/**
|
|
197
255
|
* Opt in to analytics.
|
|
@@ -208,7 +266,134 @@ export class AnalyticsController extends BaseController {
|
|
|
208
266
|
this.update((state) => {
|
|
209
267
|
state.optedIn = false;
|
|
210
268
|
});
|
|
269
|
+
__classPrivateFieldGet(this, _AnalyticsController_instances, "m", _AnalyticsController_clearQueuedEvents).call(this);
|
|
211
270
|
}
|
|
212
271
|
}
|
|
213
|
-
_AnalyticsController_platformAdapter = new WeakMap(), _AnalyticsController_isAnonymousEventsFeatureEnabled = new WeakMap(), _AnalyticsController_initialized = new WeakMap()
|
|
272
|
+
_AnalyticsController_platformAdapter = new WeakMap(), _AnalyticsController_isAnonymousEventsFeatureEnabled = new WeakMap(), _AnalyticsController_isEventQueuePersistenceEnabled = new WeakMap(), _AnalyticsController_initialized = new WeakMap(), _AnalyticsController_instances = new WeakSet(), _AnalyticsController_sendOrQueueTrackEvent = function _AnalyticsController_sendOrQueueTrackEvent(eventName, properties, context) {
|
|
273
|
+
if (!__classPrivateFieldGet(this, _AnalyticsController_isEventQueuePersistenceEnabled, "f")) {
|
|
274
|
+
__classPrivateFieldGet(this, _AnalyticsController_platformAdapter, "f").track(eventName, properties, context);
|
|
275
|
+
return;
|
|
276
|
+
}
|
|
277
|
+
const queuedEvent = {
|
|
278
|
+
type: 'track',
|
|
279
|
+
eventName,
|
|
280
|
+
messageId: uuid(),
|
|
281
|
+
timestamp: new Date().toISOString(),
|
|
282
|
+
...(properties === undefined ? {} : { properties }),
|
|
283
|
+
...(context === undefined ? {} : { context }),
|
|
284
|
+
};
|
|
285
|
+
__classPrivateFieldGet(this, _AnalyticsController_instances, "m", _AnalyticsController_enqueueEvent).call(this, queuedEvent);
|
|
286
|
+
}, _AnalyticsController_sendOrQueueIdentifyEvent = function _AnalyticsController_sendOrQueueIdentifyEvent(userId, traits, context) {
|
|
287
|
+
if (!__classPrivateFieldGet(this, _AnalyticsController_isEventQueuePersistenceEnabled, "f")) {
|
|
288
|
+
__classPrivateFieldGet(this, _AnalyticsController_platformAdapter, "f").identify(userId, traits, context);
|
|
289
|
+
return;
|
|
290
|
+
}
|
|
291
|
+
const queuedEvent = {
|
|
292
|
+
type: 'identify',
|
|
293
|
+
userId,
|
|
294
|
+
messageId: uuid(),
|
|
295
|
+
timestamp: new Date().toISOString(),
|
|
296
|
+
...(traits === undefined ? {} : { traits }),
|
|
297
|
+
...(context === undefined ? {} : { context }),
|
|
298
|
+
};
|
|
299
|
+
__classPrivateFieldGet(this, _AnalyticsController_instances, "m", _AnalyticsController_enqueueEvent).call(this, queuedEvent);
|
|
300
|
+
}, _AnalyticsController_sendOrQueueViewEvent = function _AnalyticsController_sendOrQueueViewEvent(name, properties, context) {
|
|
301
|
+
if (!__classPrivateFieldGet(this, _AnalyticsController_isEventQueuePersistenceEnabled, "f")) {
|
|
302
|
+
__classPrivateFieldGet(this, _AnalyticsController_platformAdapter, "f").view(name, properties, context);
|
|
303
|
+
return;
|
|
304
|
+
}
|
|
305
|
+
const queuedEvent = {
|
|
306
|
+
type: 'view',
|
|
307
|
+
name,
|
|
308
|
+
messageId: uuid(),
|
|
309
|
+
timestamp: new Date().toISOString(),
|
|
310
|
+
...(properties === undefined ? {} : { properties }),
|
|
311
|
+
...(context === undefined ? {} : { context }),
|
|
312
|
+
};
|
|
313
|
+
__classPrivateFieldGet(this, _AnalyticsController_instances, "m", _AnalyticsController_enqueueEvent).call(this, queuedEvent);
|
|
314
|
+
}, _AnalyticsController_enqueueEvent = function _AnalyticsController_enqueueEvent(queuedEvent) {
|
|
315
|
+
const eventQueue = {
|
|
316
|
+
...(this.state.eventQueue ?? {}),
|
|
317
|
+
[queuedEvent.messageId]: queuedEvent,
|
|
318
|
+
};
|
|
319
|
+
this.update((state) => {
|
|
320
|
+
state.eventQueue = eventQueue;
|
|
321
|
+
});
|
|
322
|
+
__classPrivateFieldGet(this, _AnalyticsController_instances, "m", _AnalyticsController_sendQueuedEvent).call(this, queuedEvent);
|
|
323
|
+
}, _AnalyticsController_sendQueuedEvent = function _AnalyticsController_sendQueuedEvent(queuedEvent) {
|
|
324
|
+
const timestamp = new Date(queuedEvent.timestamp);
|
|
325
|
+
if (Number.isNaN(timestamp.getTime())) {
|
|
326
|
+
log('Dropping queued analytics event with invalid timestamp', {
|
|
327
|
+
messageId: queuedEvent.messageId,
|
|
328
|
+
});
|
|
329
|
+
__classPrivateFieldGet(this, _AnalyticsController_instances, "m", _AnalyticsController_removeQueuedEvent).call(this, queuedEvent.messageId);
|
|
330
|
+
return;
|
|
331
|
+
}
|
|
332
|
+
const options = {
|
|
333
|
+
messageId: queuedEvent.messageId,
|
|
334
|
+
timestamp,
|
|
335
|
+
callback: (error) => {
|
|
336
|
+
if (error) {
|
|
337
|
+
log('Queued analytics event delivery failed', {
|
|
338
|
+
messageId: queuedEvent.messageId,
|
|
339
|
+
error,
|
|
340
|
+
});
|
|
341
|
+
return;
|
|
342
|
+
}
|
|
343
|
+
__classPrivateFieldGet(this, _AnalyticsController_instances, "m", _AnalyticsController_removeQueuedEvent).call(this, queuedEvent.messageId);
|
|
344
|
+
},
|
|
345
|
+
};
|
|
346
|
+
try {
|
|
347
|
+
if (queuedEvent.type === 'track') {
|
|
348
|
+
__classPrivateFieldGet(this, _AnalyticsController_platformAdapter, "f").track(queuedEvent.eventName, cloneDeep(queuedEvent.properties), cloneDeep(queuedEvent.context), options);
|
|
349
|
+
}
|
|
350
|
+
else if (queuedEvent.type === 'identify') {
|
|
351
|
+
__classPrivateFieldGet(this, _AnalyticsController_platformAdapter, "f").identify(queuedEvent.userId, cloneDeep(queuedEvent.traits), cloneDeep(queuedEvent.context), options);
|
|
352
|
+
}
|
|
353
|
+
else {
|
|
354
|
+
__classPrivateFieldGet(this, _AnalyticsController_platformAdapter, "f").view(queuedEvent.name, cloneDeep(queuedEvent.properties), cloneDeep(queuedEvent.context), options);
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
catch (error) {
|
|
358
|
+
log('Error sending queued analytics event', {
|
|
359
|
+
messageId: queuedEvent.messageId,
|
|
360
|
+
error,
|
|
361
|
+
});
|
|
362
|
+
}
|
|
363
|
+
}, _AnalyticsController_replayQueuedEvents = function _AnalyticsController_replayQueuedEvents() {
|
|
364
|
+
if (!__classPrivateFieldGet(this, _AnalyticsController_isEventQueuePersistenceEnabled, "f") || !this.state.eventQueue) {
|
|
365
|
+
return;
|
|
366
|
+
}
|
|
367
|
+
if (!analyticsControllerSelectors.selectEnabled(this.state)) {
|
|
368
|
+
__classPrivateFieldGet(this, _AnalyticsController_instances, "m", _AnalyticsController_clearQueuedEvents).call(this);
|
|
369
|
+
return;
|
|
370
|
+
}
|
|
371
|
+
for (const [messageId, queuedEvent] of Object.entries(this.state.eventQueue)) {
|
|
372
|
+
if (!isAnalyticsQueuedEvent(queuedEvent) ||
|
|
373
|
+
queuedEvent.messageId !== messageId) {
|
|
374
|
+
log('Dropping invalid queued analytics event', { messageId });
|
|
375
|
+
__classPrivateFieldGet(this, _AnalyticsController_instances, "m", _AnalyticsController_removeQueuedEvent).call(this, messageId);
|
|
376
|
+
continue;
|
|
377
|
+
}
|
|
378
|
+
__classPrivateFieldGet(this, _AnalyticsController_instances, "m", _AnalyticsController_sendQueuedEvent).call(this, queuedEvent);
|
|
379
|
+
}
|
|
380
|
+
}, _AnalyticsController_removeQueuedEvent = function _AnalyticsController_removeQueuedEvent(messageId) {
|
|
381
|
+
const currentEventQueue = this.state.eventQueue;
|
|
382
|
+
if (!currentEventQueue ||
|
|
383
|
+
!Object.prototype.hasOwnProperty.call(currentEventQueue, messageId)) {
|
|
384
|
+
return;
|
|
385
|
+
}
|
|
386
|
+
const { [messageId]: _deletedEvent, ...eventQueue } = currentEventQueue;
|
|
387
|
+
this.update((state) => {
|
|
388
|
+
state.eventQueue = eventQueue;
|
|
389
|
+
});
|
|
390
|
+
}, _AnalyticsController_clearQueuedEvents = function _AnalyticsController_clearQueuedEvents() {
|
|
391
|
+
if (!this.state.eventQueue ||
|
|
392
|
+
Object.keys(this.state.eventQueue).length === 0) {
|
|
393
|
+
return;
|
|
394
|
+
}
|
|
395
|
+
this.update((state) => {
|
|
396
|
+
state.eventQueue = {};
|
|
397
|
+
});
|
|
398
|
+
};
|
|
214
399
|
//# sourceMappingURL=AnalyticsController.mjs.map
|