@metamask-previews/analytics-controller 0.0.0-preview-8647bc1c → 0.0.0-preview-704ae19a
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/README.md +61 -4
- package/dist/AnalyticsController-method-action-types.cjs.map +1 -1
- package/dist/AnalyticsController-method-action-types.d.cts +62 -23
- package/dist/AnalyticsController-method-action-types.d.cts.map +1 -1
- package/dist/AnalyticsController-method-action-types.d.mts +62 -23
- 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 +125 -52
- package/dist/AnalyticsController.cjs.map +1 -1
- package/dist/AnalyticsController.d.cts +52 -24
- package/dist/AnalyticsController.d.cts.map +1 -1
- package/dist/AnalyticsController.d.mts +52 -24
- package/dist/AnalyticsController.d.mts.map +1 -1
- package/dist/AnalyticsController.mjs +126 -53
- package/dist/AnalyticsController.mjs.map +1 -1
- package/dist/AnalyticsPlatformAdapter.types.cjs.map +1 -1
- package/dist/AnalyticsPlatformAdapter.types.d.cts +69 -13
- package/dist/AnalyticsPlatformAdapter.types.d.cts.map +1 -1
- package/dist/AnalyticsPlatformAdapter.types.d.mts +69 -13
- package/dist/AnalyticsPlatformAdapter.types.d.mts.map +1 -1
- package/dist/AnalyticsPlatformAdapter.types.mjs.map +1 -1
- package/dist/AnalyticsPlatformAdapterSetupError.cjs +17 -0
- package/dist/AnalyticsPlatformAdapterSetupError.cjs.map +1 -0
- package/dist/AnalyticsPlatformAdapterSetupError.d.cts +8 -0
- package/dist/AnalyticsPlatformAdapterSetupError.d.cts.map +1 -0
- package/dist/AnalyticsPlatformAdapterSetupError.d.mts +8 -0
- package/dist/AnalyticsPlatformAdapterSetupError.d.mts.map +1 -0
- package/dist/AnalyticsPlatformAdapterSetupError.mjs +13 -0
- package/dist/AnalyticsPlatformAdapterSetupError.mjs.map +1 -0
- package/dist/analyticsStateComputer.cjs +46 -0
- package/dist/analyticsStateComputer.cjs.map +1 -0
- package/dist/analyticsStateComputer.d.cts +35 -0
- package/dist/analyticsStateComputer.d.cts.map +1 -0
- package/dist/analyticsStateComputer.d.mts +35 -0
- package/dist/analyticsStateComputer.d.mts.map +1 -0
- package/dist/analyticsStateComputer.mjs +42 -0
- package/dist/analyticsStateComputer.mjs.map +1 -0
- package/dist/index.cjs +4 -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 +2 -0
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
|
@@ -11,8 +11,9 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
|
|
|
11
11
|
};
|
|
12
12
|
var _AnalyticsController_platformAdapter;
|
|
13
13
|
import { BaseController } from "@metamask/base-controller";
|
|
14
|
-
import { v4 as uuidv4 } from "uuid";
|
|
14
|
+
import { v4 as uuidv4, validate as uuidValidate, version as uuidVersion } from "uuid";
|
|
15
15
|
import { projectLogger } from "./AnalyticsLogger.mjs";
|
|
16
|
+
import { computeEnabledState } from "./analyticsStateComputer.mjs";
|
|
16
17
|
// === GENERAL ===
|
|
17
18
|
/**
|
|
18
19
|
* The name of the {@link AnalyticsController}, used to namespace the
|
|
@@ -24,19 +25,19 @@ export const controllerName = 'AnalyticsController';
|
|
|
24
25
|
* The metadata for each property in {@link AnalyticsControllerState}.
|
|
25
26
|
*/
|
|
26
27
|
const analyticsControllerMetadata = {
|
|
27
|
-
|
|
28
|
+
user_optedIn: {
|
|
28
29
|
includeInStateLogs: true,
|
|
29
30
|
persist: true,
|
|
30
31
|
includeInDebugSnapshot: true,
|
|
31
32
|
usedInUi: true,
|
|
32
33
|
},
|
|
33
|
-
|
|
34
|
+
user_socialOptedIn: {
|
|
34
35
|
includeInStateLogs: true,
|
|
35
36
|
persist: true,
|
|
36
37
|
includeInDebugSnapshot: true,
|
|
37
38
|
usedInUi: true,
|
|
38
39
|
},
|
|
39
|
-
|
|
40
|
+
user_analyticsId: {
|
|
40
41
|
includeInStateLogs: true,
|
|
41
42
|
persist: true,
|
|
42
43
|
includeInDebugSnapshot: true,
|
|
@@ -53,20 +54,24 @@ const analyticsControllerMetadata = {
|
|
|
53
54
|
*/
|
|
54
55
|
export function getDefaultAnalyticsControllerState() {
|
|
55
56
|
return {
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
57
|
+
user_optedIn: false,
|
|
58
|
+
user_socialOptedIn: false,
|
|
59
|
+
user_analyticsId: uuidv4(),
|
|
59
60
|
};
|
|
60
61
|
}
|
|
61
62
|
// === MESSENGER ===
|
|
62
63
|
const MESSENGER_EXPOSED_METHODS = [
|
|
63
64
|
'trackEvent',
|
|
64
65
|
'identify',
|
|
65
|
-
'
|
|
66
|
-
'enable',
|
|
67
|
-
'disable',
|
|
66
|
+
'trackView',
|
|
68
67
|
'optIn',
|
|
69
68
|
'optOut',
|
|
69
|
+
'socialOptIn',
|
|
70
|
+
'socialOptOut',
|
|
71
|
+
'getAnalyticsId',
|
|
72
|
+
'isEnabled',
|
|
73
|
+
'isOptedIn',
|
|
74
|
+
'isSocialOptedIn',
|
|
70
75
|
];
|
|
71
76
|
/**
|
|
72
77
|
* The AnalyticsController manages analytics tracking across platforms (Mobile/Extension).
|
|
@@ -83,111 +88,179 @@ export class AnalyticsController extends BaseController {
|
|
|
83
88
|
* Constructs an AnalyticsController instance.
|
|
84
89
|
*
|
|
85
90
|
* @param options - Controller options
|
|
86
|
-
* @param options.state - Initial controller state (defaults from getDefaultAnalyticsControllerState)
|
|
91
|
+
* @param options.state - Initial controller state (defaults from getDefaultAnalyticsControllerState).
|
|
92
|
+
* For migration from a previous system, pass the existing analytics ID via state.user_analyticsId.
|
|
87
93
|
* @param options.messenger - Messenger used to communicate with BaseController
|
|
88
94
|
* @param options.platformAdapter - Platform adapter implementation for tracking
|
|
89
95
|
*/
|
|
90
96
|
constructor({ state = {}, messenger, platformAdapter, }) {
|
|
97
|
+
const initialState = {
|
|
98
|
+
...getDefaultAnalyticsControllerState(),
|
|
99
|
+
...state,
|
|
100
|
+
};
|
|
91
101
|
super({
|
|
92
102
|
name: controllerName,
|
|
93
103
|
metadata: analyticsControllerMetadata,
|
|
94
|
-
state:
|
|
95
|
-
...getDefaultAnalyticsControllerState(),
|
|
96
|
-
...state,
|
|
97
|
-
},
|
|
104
|
+
state: initialState,
|
|
98
105
|
messenger,
|
|
99
106
|
});
|
|
100
107
|
_AnalyticsController_platformAdapter.set(this, void 0);
|
|
101
108
|
__classPrivateFieldSet(this, _AnalyticsController_platformAdapter, platformAdapter, "f");
|
|
102
109
|
this.messenger.registerMethodActionHandlers(this, MESSENGER_EXPOSED_METHODS);
|
|
103
110
|
projectLogger('AnalyticsController initialized and ready', {
|
|
104
|
-
enabled: this.state
|
|
105
|
-
optedIn: this.state.
|
|
106
|
-
|
|
111
|
+
enabled: computeEnabledState(this.state),
|
|
112
|
+
optedIn: this.state.user_optedIn,
|
|
113
|
+
socialOptedIn: this.state.user_socialOptedIn,
|
|
114
|
+
analyticsId: this.state.user_analyticsId,
|
|
107
115
|
});
|
|
116
|
+
// Call onSetupCompleted lifecycle hook after initialization
|
|
117
|
+
// Only call if analyticsId is set and is a valid UUIDv4 (this is the definition of "completed" setup)
|
|
118
|
+
if (this.state.user_analyticsId &&
|
|
119
|
+
uuidValidate(this.state.user_analyticsId) &&
|
|
120
|
+
uuidVersion(this.state.user_analyticsId) === 4) {
|
|
121
|
+
try {
|
|
122
|
+
__classPrivateFieldGet(this, _AnalyticsController_platformAdapter, "f").onSetupCompleted(this.state.user_analyticsId);
|
|
123
|
+
}
|
|
124
|
+
catch (error) {
|
|
125
|
+
// Log error but don't throw - adapter setup failure shouldn't break controller
|
|
126
|
+
projectLogger('Error calling platformAdapter.onSetupCompleted', error);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
else {
|
|
130
|
+
// analyticsId is undefined, null, empty string, or not a valid UUIDv4
|
|
131
|
+
throw new Error(`Invalid analyticsId: expected a valid UUIDv4, but got ${JSON.stringify(this.state.user_analyticsId)}`);
|
|
132
|
+
}
|
|
108
133
|
}
|
|
109
134
|
/**
|
|
110
135
|
* Track an analytics event.
|
|
111
136
|
*
|
|
112
137
|
* Events are only tracked if analytics is enabled.
|
|
113
138
|
*
|
|
114
|
-
* @param
|
|
115
|
-
* @param properties - Event properties
|
|
139
|
+
* @param event - Analytics event with properties and sensitive properties
|
|
116
140
|
*/
|
|
117
|
-
trackEvent(
|
|
141
|
+
trackEvent(event) {
|
|
118
142
|
// Don't track if analytics is disabled
|
|
119
|
-
if (!this.state
|
|
143
|
+
if (!computeEnabledState(this.state)) {
|
|
120
144
|
return;
|
|
121
145
|
}
|
|
122
|
-
//
|
|
123
|
-
|
|
146
|
+
// if event does not have properties, only send the non-anonymous empty event
|
|
147
|
+
// and return to prevent any additional processing
|
|
148
|
+
if (!event.hasProperties) {
|
|
149
|
+
__classPrivateFieldGet(this, _AnalyticsController_platformAdapter, "f").track(event.name, {
|
|
150
|
+
anonymous: false,
|
|
151
|
+
});
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
// Log all non-anonymous properties, or an empty event if there's no non-anon props.
|
|
155
|
+
__classPrivateFieldGet(this, _AnalyticsController_platformAdapter, "f").track(event.name, {
|
|
156
|
+
anonymous: false,
|
|
157
|
+
...event.properties,
|
|
158
|
+
});
|
|
159
|
+
// Track all sensitive properties in an anonymous event
|
|
160
|
+
if (event.isAnonymous) {
|
|
161
|
+
__classPrivateFieldGet(this, _AnalyticsController_platformAdapter, "f").track(event.name, {
|
|
162
|
+
anonymous: true,
|
|
163
|
+
...event.properties,
|
|
164
|
+
...event.sensitiveProperties,
|
|
165
|
+
});
|
|
166
|
+
}
|
|
124
167
|
}
|
|
125
168
|
/**
|
|
126
169
|
* Identify a user for analytics.
|
|
127
170
|
*
|
|
128
|
-
* @param userId - The user identifier (e.g., metametrics ID)
|
|
129
171
|
* @param traits - User traits/properties
|
|
130
172
|
*/
|
|
131
|
-
identify(
|
|
132
|
-
if (!this.state
|
|
173
|
+
identify(traits) {
|
|
174
|
+
if (!computeEnabledState(this.state)) {
|
|
133
175
|
return;
|
|
134
176
|
}
|
|
135
|
-
//
|
|
136
|
-
this.update((state) => {
|
|
137
|
-
state.analyticsId = userId;
|
|
138
|
-
});
|
|
139
|
-
// Delegate to platform adapter if supported
|
|
177
|
+
// Delegate to platform adapter if supported, using the current analytics ID
|
|
140
178
|
if (__classPrivateFieldGet(this, _AnalyticsController_platformAdapter, "f").identify) {
|
|
141
|
-
__classPrivateFieldGet(this, _AnalyticsController_platformAdapter, "f").identify(
|
|
179
|
+
__classPrivateFieldGet(this, _AnalyticsController_platformAdapter, "f").identify(this.state.user_analyticsId, traits);
|
|
142
180
|
}
|
|
143
181
|
}
|
|
144
182
|
/**
|
|
145
183
|
* Track a page view.
|
|
146
184
|
*
|
|
147
|
-
* @param
|
|
148
|
-
* @param properties -
|
|
185
|
+
* @param name - The name of the UI item being viewed (pages for web, screen for mobile)
|
|
186
|
+
* @param properties - UI item properties
|
|
149
187
|
*/
|
|
150
|
-
|
|
151
|
-
if (!this.state
|
|
188
|
+
trackView(name, properties) {
|
|
189
|
+
if (!computeEnabledState(this.state)) {
|
|
152
190
|
return;
|
|
153
191
|
}
|
|
154
|
-
// Delegate to platform adapter
|
|
155
|
-
|
|
156
|
-
__classPrivateFieldGet(this, _AnalyticsController_platformAdapter, "f").trackPage(pageName, properties);
|
|
157
|
-
}
|
|
192
|
+
// Delegate to platform adapter
|
|
193
|
+
__classPrivateFieldGet(this, _AnalyticsController_platformAdapter, "f").view(name, properties);
|
|
158
194
|
}
|
|
159
195
|
/**
|
|
160
|
-
*
|
|
196
|
+
* Opt in to analytics.
|
|
197
|
+
* This updates the user's opt-in status.
|
|
161
198
|
*/
|
|
162
|
-
|
|
199
|
+
optIn() {
|
|
163
200
|
this.update((state) => {
|
|
164
|
-
state.
|
|
201
|
+
state.user_optedIn = true;
|
|
165
202
|
});
|
|
166
203
|
}
|
|
167
204
|
/**
|
|
168
|
-
*
|
|
205
|
+
* Opt out of analytics.
|
|
206
|
+
* This updates the user's opt-in status.
|
|
169
207
|
*/
|
|
170
|
-
|
|
208
|
+
optOut() {
|
|
171
209
|
this.update((state) => {
|
|
172
|
-
state.
|
|
210
|
+
state.user_optedIn = false;
|
|
173
211
|
});
|
|
174
212
|
}
|
|
175
213
|
/**
|
|
176
|
-
* Opt in to analytics.
|
|
214
|
+
* Opt in to analytics through social account.
|
|
215
|
+
* This updates the user's social opt-in status.
|
|
177
216
|
*/
|
|
178
|
-
|
|
217
|
+
socialOptIn() {
|
|
179
218
|
this.update((state) => {
|
|
180
|
-
state.
|
|
219
|
+
state.user_socialOptedIn = true;
|
|
181
220
|
});
|
|
182
221
|
}
|
|
183
222
|
/**
|
|
184
|
-
* Opt out of analytics.
|
|
223
|
+
* Opt out of analytics through social account.
|
|
224
|
+
* This updates the user's social opt-in status.
|
|
185
225
|
*/
|
|
186
|
-
|
|
226
|
+
socialOptOut() {
|
|
187
227
|
this.update((state) => {
|
|
188
|
-
state.
|
|
228
|
+
state.user_socialOptedIn = false;
|
|
189
229
|
});
|
|
190
230
|
}
|
|
231
|
+
/**
|
|
232
|
+
* Get the analytics ID from the controller state.
|
|
233
|
+
*
|
|
234
|
+
* @returns The current analytics ID.
|
|
235
|
+
*/
|
|
236
|
+
getAnalyticsId() {
|
|
237
|
+
return this.state.user_analyticsId;
|
|
238
|
+
}
|
|
239
|
+
/**
|
|
240
|
+
* Get the enabled status from the controller state.
|
|
241
|
+
* This is computed from user state via the state machine.
|
|
242
|
+
*
|
|
243
|
+
* @returns The current enabled status.
|
|
244
|
+
*/
|
|
245
|
+
isEnabled() {
|
|
246
|
+
return computeEnabledState(this.state);
|
|
247
|
+
}
|
|
248
|
+
/**
|
|
249
|
+
* Get the opted in status from the controller state.
|
|
250
|
+
*
|
|
251
|
+
* @returns The current opted in status.
|
|
252
|
+
*/
|
|
253
|
+
isOptedIn() {
|
|
254
|
+
return this.state.user_optedIn;
|
|
255
|
+
}
|
|
256
|
+
/**
|
|
257
|
+
* Get the social opted in status from the controller state.
|
|
258
|
+
*
|
|
259
|
+
* @returns The current social opted in status.
|
|
260
|
+
*/
|
|
261
|
+
isSocialOptedIn() {
|
|
262
|
+
return this.state.user_socialOptedIn;
|
|
263
|
+
}
|
|
191
264
|
}
|
|
192
265
|
_AnalyticsController_platformAdapter = new WeakMap();
|
|
193
266
|
//# sourceMappingURL=AnalyticsController.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AnalyticsController.mjs","sourceRoot":"","sources":["../src/AnalyticsController.ts"],"names":[],"mappings":";;;;;;;;;;;;AAKA,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAE3D,OAAO,EAAE,EAAE,IAAI,MAAM,EAAE,aAAa;AAGpC,OAAO,EAAE,aAAa,EAAE,8BAA0B;AAMlD,kBAAkB;AAElB;;;;GAIG;AACH,MAAM,CAAC,MAAM,cAAc,GAAG,qBAAqB,CAAC;AAwBpD;;GAEG;AACH,MAAM,2BAA2B,GAAG;IAClC,OAAO,EAAE;QACP,kBAAkB,EAAE,IAAI;QACxB,OAAO,EAAE,IAAI;QACb,sBAAsB,EAAE,IAAI;QAC5B,QAAQ,EAAE,IAAI;KACf;IACD,OAAO,EAAE;QACP,kBAAkB,EAAE,IAAI;QACxB,OAAO,EAAE,IAAI;QACb,sBAAsB,EAAE,IAAI;QAC5B,QAAQ,EAAE,IAAI;KACf;IACD,WAAW,EAAE;QACX,kBAAkB,EAAE,IAAI;QACxB,OAAO,EAAE,IAAI;QACb,sBAAsB,EAAE,IAAI;QAC5B,QAAQ,EAAE,KAAK;KAChB;CACgD,CAAC;AAEpD;;;;;;;GAOG;AACH,MAAM,UAAU,kCAAkC;IAChD,OAAO;QACL,OAAO,EAAE,IAAI;QACb,OAAO,EAAE,KAAK;QACd,WAAW,EAAE,MAAM,EAA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{\n ControllerGetStateAction,\n ControllerStateChangeEvent,\n StateMetadata,\n} from '@metamask/base-controller';\nimport { BaseController } from '@metamask/base-controller';\nimport type { Messenger } from '@metamask/messenger';\nimport { v4 as uuidv4 } from 'uuid';\n\nimport type { AnalyticsControllerMethodActions } from './AnalyticsController-method-action-types';\nimport { projectLogger } from './AnalyticsLogger';\nimport type {\n AnalyticsPlatformAdapter,\n AnalyticsEventProperties,\n} from './AnalyticsPlatformAdapter.types';\n\n// === GENERAL ===\n\n/**\n * The name of the {@link AnalyticsController}, used to namespace the\n * controller's actions and events and to namespace the controller's state data\n * when composed with other controllers.\n */\nexport const controllerName = 'AnalyticsController';\n\n// === STATE ===\n\n/**\n * Describes the shape of the state object for {@link AnalyticsController}.\n */\nexport type AnalyticsControllerState = {\n /**\n * Whether analytics tracking is enabled\n */\n enabled: boolean;\n\n /**\n * Whether the user has opted in to analytics\n */\n optedIn: boolean;\n\n /**\n * User's UUIDv4 analytics identifier\n */\n analyticsId: string;\n};\n\n/**\n * The metadata for each property in {@link AnalyticsControllerState}.\n */\nconst analyticsControllerMetadata = {\n enabled: {\n includeInStateLogs: true,\n persist: true,\n includeInDebugSnapshot: true,\n usedInUi: true,\n },\n optedIn: {\n includeInStateLogs: true,\n persist: true,\n includeInDebugSnapshot: true,\n usedInUi: true,\n },\n analyticsId: {\n includeInStateLogs: true,\n persist: true,\n includeInDebugSnapshot: true,\n usedInUi: false,\n },\n} satisfies StateMetadata<AnalyticsControllerState>;\n\n/**\n * Constructs the default {@link AnalyticsController} state. This allows\n * consumers to provide a partial state object when initializing the controller\n * and also helps in constructing complete state objects for this controller in\n * tests.\n *\n * @returns The default {@link AnalyticsController} state.\n */\nexport function getDefaultAnalyticsControllerState(): AnalyticsControllerState {\n return {\n enabled: true,\n optedIn: false,\n analyticsId: uuidv4(),\n };\n}\n\n// === MESSENGER ===\n\nconst MESSENGER_EXPOSED_METHODS = [\n 'trackEvent',\n 'identify',\n 'trackPage',\n 'enable',\n 'disable',\n 'optIn',\n 'optOut',\n] as const;\n\n/**\n * Returns the state of the {@link AnalyticsController}.\n */\nexport type AnalyticsControllerGetStateAction = ControllerGetStateAction<\n typeof controllerName,\n AnalyticsControllerState\n>;\n\n/**\n * Actions that {@link AnalyticsControllerMessenger} exposes to other consumers.\n */\nexport type AnalyticsControllerActions =\n | AnalyticsControllerGetStateAction\n | AnalyticsControllerMethodActions;\n\n/**\n * Actions from other messengers that {@link AnalyticsControllerMessenger} calls.\n */\ntype AllowedActions = never;\n\n/**\n * Event emitted when the state of the {@link AnalyticsController} changes.\n */\nexport type AnalyticsControllerStateChangeEvent = ControllerStateChangeEvent<\n typeof controllerName,\n AnalyticsControllerState\n>;\n\n/**\n * Events that {@link AnalyticsControllerMessenger} exposes to other consumers.\n */\nexport type AnalyticsControllerEvents = AnalyticsControllerStateChangeEvent;\n\n/**\n * Events from other messengers that {@link AnalyticsControllerMessenger} subscribes to.\n */\ntype AllowedEvents = never;\n\n/**\n * The messenger restricted to actions and events accessed by\n * {@link AnalyticsController}.\n */\nexport type AnalyticsControllerMessenger = Messenger<\n typeof controllerName,\n AnalyticsControllerActions | AllowedActions,\n AnalyticsControllerEvents | AllowedEvents\n>;\n\n// === CONTROLLER DEFINITION ===\n\n/**\n * The options that AnalyticsController takes.\n */\nexport type AnalyticsControllerOptions = {\n state?: Partial<AnalyticsControllerState>;\n messenger: AnalyticsControllerMessenger;\n /**\n * Platform adapter implementation for tracking events\n */\n platformAdapter: AnalyticsPlatformAdapter;\n};\n\n/**\n * The AnalyticsController manages analytics tracking across platforms (Mobile/Extension).\n * It provides a unified interface for tracking events, identifying users, and managing\n * analytics preferences while delegating platform-specific implementation to an\n * {@link AnalyticsPlatformAdapter}.\n *\n * This controller follows the MetaMask controller pattern and integrates with the\n * messenger system to allow other controllers and components to track analytics events.\n * It delegates platform-specific implementation to an {@link AnalyticsPlatformAdapter}.\n */\nexport class AnalyticsController extends BaseController<\n 'AnalyticsController',\n AnalyticsControllerState,\n AnalyticsControllerMessenger\n> {\n readonly #platformAdapter: AnalyticsPlatformAdapter;\n\n /**\n * Constructs an AnalyticsController instance.\n *\n * @param options - Controller options\n * @param options.state - Initial controller state (defaults from getDefaultAnalyticsControllerState)\n * @param options.messenger - Messenger used to communicate with BaseController\n * @param options.platformAdapter - Platform adapter implementation for tracking\n */\n constructor({\n state = {},\n messenger,\n platformAdapter,\n }: AnalyticsControllerOptions) {\n super({\n name: controllerName,\n metadata: analyticsControllerMetadata,\n state: {\n ...getDefaultAnalyticsControllerState(),\n ...state,\n },\n messenger,\n });\n\n this.#platformAdapter = platformAdapter;\n\n this.messenger.registerMethodActionHandlers(\n this,\n MESSENGER_EXPOSED_METHODS,\n );\n\n projectLogger('AnalyticsController initialized and ready', {\n enabled: this.state.enabled,\n optedIn: this.state.optedIn,\n analyticsId: this.state.analyticsId,\n });\n }\n\n /**\n * Track an analytics event.\n *\n * Events are only tracked if analytics is enabled.\n *\n * @param eventName - The name of the event\n * @param properties - Event properties\n */\n trackEvent(\n eventName: string,\n properties: AnalyticsEventProperties = {},\n ): void {\n // Don't track if analytics is disabled\n if (!this.state.enabled) {\n return;\n }\n\n // Delegate to platform adapter\n this.#platformAdapter.trackEvent(eventName, properties);\n }\n\n /**\n * Identify a user for analytics.\n *\n * @param userId - The user identifier (e.g., metametrics ID)\n * @param traits - User traits/properties\n */\n identify(userId: string, traits?: AnalyticsEventProperties): void {\n if (!this.state.enabled) {\n return;\n }\n\n // Update state with analytics ID\n this.update((state) => {\n state.analyticsId = userId;\n });\n\n // Delegate to platform adapter if supported\n if (this.#platformAdapter.identify) {\n this.#platformAdapter.identify(userId, traits);\n }\n }\n\n /**\n * Track a page view.\n *\n * @param pageName - The name of the page\n * @param properties - Page properties\n */\n trackPage(pageName: string, properties?: AnalyticsEventProperties): void {\n if (!this.state.enabled) {\n return;\n }\n\n // Delegate to platform adapter if supported\n if (this.#platformAdapter.trackPage) {\n this.#platformAdapter.trackPage(pageName, properties);\n }\n }\n\n /**\n * Enable analytics tracking.\n */\n enable(): void {\n this.update((state) => {\n state.enabled = true;\n });\n }\n\n /**\n * Disable analytics tracking.\n */\n disable(): void {\n this.update((state) => {\n state.enabled = false;\n });\n }\n\n /**\n * Opt in to analytics.\n */\n optIn(): void {\n this.update((state) => {\n state.optedIn = true;\n });\n }\n\n /**\n * Opt out of analytics.\n */\n optOut(): void {\n this.update((state) => {\n state.optedIn = false;\n });\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"AnalyticsController.mjs","sourceRoot":"","sources":["../src/AnalyticsController.ts"],"names":[],"mappings":";;;;;;;;;;;;AAKA,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAE3D,OAAO,EACL,EAAE,IAAI,MAAM,EACZ,QAAQ,IAAI,YAAY,EACxB,OAAO,IAAI,WAAW,EACvB,aAAa;AAGd,OAAO,EAAE,aAAa,EAAE,8BAA0B;AAOlD,OAAO,EAAE,mBAAmB,EAAE,qCAAiC;AAE/D,kBAAkB;AAElB;;;;GAIG;AACH,MAAM,CAAC,MAAM,cAAc,GAAG,qBAAqB,CAAC;AAwBpD;;GAEG;AACH,MAAM,2BAA2B,GAAG;IAClC,YAAY,EAAE;QACZ,kBAAkB,EAAE,IAAI;QACxB,OAAO,EAAE,IAAI;QACb,sBAAsB,EAAE,IAAI;QAC5B,QAAQ,EAAE,IAAI;KACf;IACD,kBAAkB,EAAE;QAClB,kBAAkB,EAAE,IAAI;QACxB,OAAO,EAAE,IAAI;QACb,sBAAsB,EAAE,IAAI;QAC5B,QAAQ,EAAE,IAAI;KACf;IACD,gBAAgB,EAAE;QAChB,kBAAkB,EAAE,IAAI;QACxB,OAAO,EAAE,IAAI;QACb,sBAAsB,EAAE,IAAI;QAC5B,QAAQ,EAAE,KAAK;KAChB;CACgD,CAAC;AAEpD;;;;;;;GAOG;AACH,MAAM,UAAU,kCAAkC;IAChD,OAAO;QACL,YAAY,EAAE,KAAK;QACnB,kBAAkB,EAAE,KAAK;QACzB,gBAAgB,EAAE,MAAM,EAA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AC,KAAK,CAAC,CAAC;IACzC,CAAC;IAED;;;;OAIG;IACH,SAAS;QACP,OAAO,IAAI,CAAC,KAAK,CAAC,YAAY,CAAC;IACjC,CAAC;IAED;;;;OAIG;IACH,eAAe;QACb,OAAO,IAAI,CAAC,KAAK,CAAC,kBAAkB,CAAC;IACvC,CAAC;CACF","sourcesContent":["import type {\n ControllerGetStateAction,\n ControllerStateChangeEvent,\n StateMetadata,\n} from '@metamask/base-controller';\nimport { BaseController } from '@metamask/base-controller';\nimport type { Messenger } from '@metamask/messenger';\nimport {\n v4 as uuidv4,\n validate as uuidValidate,\n version as uuidVersion,\n} from 'uuid';\n\nimport type { AnalyticsControllerMethodActions } from './AnalyticsController-method-action-types';\nimport { projectLogger } from './AnalyticsLogger';\nimport type {\n AnalyticsPlatformAdapter,\n AnalyticsEventProperties,\n AnalyticsUserTraits,\n AnalyticsTrackingEvent,\n} from './AnalyticsPlatformAdapter.types';\nimport { computeEnabledState } from './analyticsStateComputer';\n\n// === GENERAL ===\n\n/**\n * The name of the {@link AnalyticsController}, used to namespace the\n * controller's actions and events and to namespace the controller's state data\n * when composed with other controllers.\n */\nexport const controllerName = 'AnalyticsController';\n\n// === STATE ===\n\n/**\n * Describes the shape of the state object for {@link AnalyticsController}.\n */\nexport type AnalyticsControllerState = {\n /**\n * User domain: Whether the user has opted in to analytics.\n */\n user_optedIn: boolean;\n\n /**\n * User domain: Whether the user has opted in to analytics through social account.\n */\n user_socialOptedIn: boolean;\n\n /**\n * User domain: User's UUIDv4 analytics identifier.\n */\n user_analyticsId: string;\n};\n\n/**\n * The metadata for each property in {@link AnalyticsControllerState}.\n */\nconst analyticsControllerMetadata = {\n user_optedIn: {\n includeInStateLogs: true,\n persist: true,\n includeInDebugSnapshot: true,\n usedInUi: true,\n },\n user_socialOptedIn: {\n includeInStateLogs: true,\n persist: true,\n includeInDebugSnapshot: true,\n usedInUi: true,\n },\n user_analyticsId: {\n includeInStateLogs: true,\n persist: true,\n includeInDebugSnapshot: true,\n usedInUi: false,\n },\n} satisfies StateMetadata<AnalyticsControllerState>;\n\n/**\n * Constructs the default {@link AnalyticsController} state. This allows\n * consumers to provide a partial state object when initializing the controller\n * and also helps in constructing complete state objects for this controller in\n * tests.\n *\n * @returns The default {@link AnalyticsController} state.\n */\nexport function getDefaultAnalyticsControllerState(): AnalyticsControllerState {\n return {\n user_optedIn: false,\n user_socialOptedIn: false,\n user_analyticsId: uuidv4(),\n };\n}\n\n// === MESSENGER ===\n\nconst MESSENGER_EXPOSED_METHODS = [\n 'trackEvent',\n 'identify',\n 'trackView',\n 'optIn',\n 'optOut',\n 'socialOptIn',\n 'socialOptOut',\n 'getAnalyticsId',\n 'isEnabled',\n 'isOptedIn',\n 'isSocialOptedIn',\n] as const;\n\n/**\n * Returns the state of the {@link AnalyticsController}.\n */\nexport type AnalyticsControllerGetStateAction = ControllerGetStateAction<\n typeof controllerName,\n AnalyticsControllerState\n>;\n\n/**\n * Actions that {@link AnalyticsControllerMessenger} exposes to other consumers.\n */\nexport type AnalyticsControllerActions =\n | AnalyticsControllerGetStateAction\n | AnalyticsControllerMethodActions;\n\n/**\n * Actions from other messengers that {@link AnalyticsControllerMessenger} calls.\n */\ntype AllowedActions = never;\n\n/**\n * Event emitted when the state of the {@link AnalyticsController} changes.\n */\nexport type AnalyticsControllerStateChangeEvent = ControllerStateChangeEvent<\n typeof controllerName,\n AnalyticsControllerState\n>;\n\n/**\n * Events that {@link AnalyticsControllerMessenger} exposes to other consumers.\n */\nexport type AnalyticsControllerEvents = AnalyticsControllerStateChangeEvent;\n\n/**\n * Events from other messengers that {@link AnalyticsControllerMessenger} subscribes to.\n */\ntype AllowedEvents = never;\n\n/**\n * The messenger restricted to actions and events accessed by\n * {@link AnalyticsController}.\n */\nexport type AnalyticsControllerMessenger = Messenger<\n typeof controllerName,\n AnalyticsControllerActions | AllowedActions,\n AnalyticsControllerEvents | AllowedEvents\n>;\n\n// === CONTROLLER DEFINITION ===\n\n/**\n * The options that AnalyticsController takes.\n */\nexport type AnalyticsControllerOptions = {\n state?: Partial<AnalyticsControllerState>;\n messenger: AnalyticsControllerMessenger;\n /**\n * Platform adapter implementation for tracking events\n */\n platformAdapter: AnalyticsPlatformAdapter;\n};\n\n/**\n * The AnalyticsController manages analytics tracking across platforms (Mobile/Extension).\n * It provides a unified interface for tracking events, identifying users, and managing\n * analytics preferences while delegating platform-specific implementation to an\n * {@link AnalyticsPlatformAdapter}.\n *\n * This controller follows the MetaMask controller pattern and integrates with the\n * messenger system to allow other controllers and components to track analytics events.\n * It delegates platform-specific implementation to an {@link AnalyticsPlatformAdapter}.\n */\nexport class AnalyticsController extends BaseController<\n 'AnalyticsController',\n AnalyticsControllerState,\n AnalyticsControllerMessenger\n> {\n readonly #platformAdapter: AnalyticsPlatformAdapter;\n\n /**\n * Constructs an AnalyticsController instance.\n *\n * @param options - Controller options\n * @param options.state - Initial controller state (defaults from getDefaultAnalyticsControllerState).\n * For migration from a previous system, pass the existing analytics ID via state.user_analyticsId.\n * @param options.messenger - Messenger used to communicate with BaseController\n * @param options.platformAdapter - Platform adapter implementation for tracking\n */\n constructor({\n state = {},\n messenger,\n platformAdapter,\n }: AnalyticsControllerOptions) {\n const initialState = {\n ...getDefaultAnalyticsControllerState(),\n ...state,\n };\n\n super({\n name: controllerName,\n metadata: analyticsControllerMetadata,\n state: initialState,\n messenger,\n });\n\n this.#platformAdapter = platformAdapter;\n\n this.messenger.registerMethodActionHandlers(\n this,\n MESSENGER_EXPOSED_METHODS,\n );\n\n projectLogger('AnalyticsController initialized and ready', {\n enabled: computeEnabledState(this.state),\n optedIn: this.state.user_optedIn,\n socialOptedIn: this.state.user_socialOptedIn,\n analyticsId: this.state.user_analyticsId,\n });\n\n // Call onSetupCompleted lifecycle hook after initialization\n // Only call if analyticsId is set and is a valid UUIDv4 (this is the definition of \"completed\" setup)\n if (\n this.state.user_analyticsId &&\n uuidValidate(this.state.user_analyticsId) &&\n uuidVersion(this.state.user_analyticsId) === 4\n ) {\n try {\n this.#platformAdapter.onSetupCompleted(this.state.user_analyticsId);\n } catch (error) {\n // Log error but don't throw - adapter setup failure shouldn't break controller\n projectLogger('Error calling platformAdapter.onSetupCompleted', error);\n }\n } else {\n // analyticsId is undefined, null, empty string, or not a valid UUIDv4\n throw new Error(\n `Invalid analyticsId: expected a valid UUIDv4, but got ${JSON.stringify(this.state.user_analyticsId)}`,\n );\n }\n }\n\n /**\n * Track an analytics event.\n *\n * Events are only tracked if analytics is enabled.\n *\n * @param event - Analytics event with properties and sensitive properties\n */\n trackEvent(event: AnalyticsTrackingEvent): void {\n // Don't track if analytics is disabled\n if (!computeEnabledState(this.state)) {\n return;\n }\n\n // if event does not have properties, only send the non-anonymous empty event\n // and return to prevent any additional processing\n if (!event.hasProperties) {\n this.#platformAdapter.track(event.name, {\n anonymous: false,\n } as AnalyticsEventProperties);\n\n return;\n }\n\n // Log all non-anonymous properties, or an empty event if there's no non-anon props.\n this.#platformAdapter.track(event.name, {\n anonymous: false,\n ...event.properties,\n } as AnalyticsEventProperties);\n\n // Track all sensitive properties in an anonymous event\n if (event.isAnonymous) {\n this.#platformAdapter.track(event.name, {\n anonymous: true,\n ...event.properties,\n ...event.sensitiveProperties,\n } as AnalyticsEventProperties);\n }\n }\n\n /**\n * Identify a user for analytics.\n *\n * @param traits - User traits/properties\n */\n identify(traits?: AnalyticsUserTraits): void {\n if (!computeEnabledState(this.state)) {\n return;\n }\n\n // Delegate to platform adapter if supported, using the current analytics ID\n if (this.#platformAdapter.identify) {\n this.#platformAdapter.identify(this.state.user_analyticsId, traits);\n }\n }\n\n /**\n * Track a page view.\n *\n * @param name - The name of the UI item being viewed (pages for web, screen for mobile)\n * @param properties - UI item properties\n */\n trackView(name: string, properties?: AnalyticsEventProperties): void {\n if (!computeEnabledState(this.state)) {\n return;\n }\n\n // Delegate to platform adapter\n this.#platformAdapter.view(name, properties);\n }\n\n /**\n * Opt in to analytics.\n * This updates the user's opt-in status.\n */\n optIn(): void {\n this.update((state) => {\n state.user_optedIn = true;\n });\n }\n\n /**\n * Opt out of analytics.\n * This updates the user's opt-in status.\n */\n optOut(): void {\n this.update((state) => {\n state.user_optedIn = false;\n });\n }\n\n /**\n * Opt in to analytics through social account.\n * This updates the user's social opt-in status.\n */\n socialOptIn(): void {\n this.update((state) => {\n state.user_socialOptedIn = true;\n });\n }\n\n /**\n * Opt out of analytics through social account.\n * This updates the user's social opt-in status.\n */\n socialOptOut(): void {\n this.update((state) => {\n state.user_socialOptedIn = false;\n });\n }\n\n /**\n * Get the analytics ID from the controller state.\n *\n * @returns The current analytics ID.\n */\n getAnalyticsId(): string {\n return this.state.user_analyticsId;\n }\n\n /**\n * Get the enabled status from the controller state.\n * This is computed from user state via the state machine.\n *\n * @returns The current enabled status.\n */\n isEnabled(): boolean {\n return computeEnabledState(this.state);\n }\n\n /**\n * Get the opted in status from the controller state.\n *\n * @returns The current opted in status.\n */\n isOptedIn(): boolean {\n return this.state.user_optedIn;\n }\n\n /**\n * Get the social opted in status from the controller state.\n *\n * @returns The current social opted in status.\n */\n isSocialOptedIn(): boolean {\n return this.state.user_socialOptedIn;\n }\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AnalyticsPlatformAdapter.types.cjs","sourceRoot":"","sources":["../src/AnalyticsPlatformAdapter.types.ts"],"names":[],"mappings":"","sourcesContent":["import type { Json } from '@metamask/utils';\n\n/**\n *
|
|
1
|
+
{"version":3,"file":"AnalyticsPlatformAdapter.types.cjs","sourceRoot":"","sources":["../src/AnalyticsPlatformAdapter.types.ts"],"names":[],"mappings":"","sourcesContent":["import type { Json } from '@metamask/utils';\n\n/**\n * Represents values that can be passed as properties to the event tracking function.\n * Similar to JsonValue from Segment SDK but decoupled for platform agnosticism.\n */\ntype AnalyticsJsonValue =\n | boolean\n | number\n | string\n | null\n | AnalyticsJsonValue[]\n | AnalyticsJsonMap\n | undefined;\n\n/**\n * Represents the map object used to pass properties to the event tracking function.\n * Similar to JsonMap from Segment SDK but decoupled for platform agnosticism.\n */\ntype AnalyticsJsonMap = {\n [key: string]: AnalyticsJsonValue;\n [index: number]: AnalyticsJsonValue;\n};\n\n/**\n * Analytics event properties\n */\nexport type AnalyticsEventProperties = Record<string, Json>;\n\n/**\n * User traits/properties for analytics identification\n */\nexport type AnalyticsUserTraits = Record<string, Json>;\n\n/**\n * Event properties structure with two distinct properties lists for anonymous and non-anonymous data.\n * Similar to ITrackingEvent from legacy analytics but decoupled for platform agnosticism.\n */\nexport type AnalyticsTrackingEvent = {\n readonly name: string;\n properties: AnalyticsJsonMap;\n sensitiveProperties: AnalyticsJsonMap;\n saveDataRecording: boolean;\n readonly isAnonymous: boolean;\n readonly hasProperties: boolean;\n};\n\n/**\n * Platform adapter interface for analytics tracking\n * Implementations should handle platform-specific details (Segment SDK, etc.)\n */\nexport type AnalyticsPlatformAdapter = {\n /**\n * Track an analytics event.\n *\n * This is the same as trackEvent in the old analytics system\n *\n * @param eventName - The name of the event\n * @param properties - Event properties\n */\n track(eventName: string, properties: AnalyticsEventProperties): void;\n\n /**\n * Identify a user with traits.\n *\n * @param userId - The user identifier (e.g., metametrics ID)\n * @param traits - User traits/properties\n */\n identify(userId: string, traits?: AnalyticsUserTraits): void;\n\n /**\n * Track a UI unit (page or screen) view depending on the platform\n *\n * This is the same as page in Segment web SDK and screen in Segment mobile SDK.\n * Each platform adapter should implement this method to track UI views\n * using the appropriate method for the platform: \"pages\" for web, \"screen\" for mobile.\n *\n * @param name - The name of the UI item being viewed (pages for web, screen for mobile)\n * @param properties - Page properties\n */\n view(name: string, properties?: AnalyticsEventProperties): void;\n\n /**\n * Lifecycle hook called after the AnalyticsController is fully initialized.\n *\n * This hook allows platform-specific adapters to perform setup that requires\n * access to the controller's state (e.g., analyticsId).\n *\n * The controller calls this method once after initialization, passing the\n * analyticsId from controller state. The analyticsId is guaranteed to be set\n * when this method is called - this is the definition of \"completed\" setup.\n *\n * @param analyticsId - The analytics ID from controller state. Always set (never empty).\n * @throws {AnalyticsPlatformAdapterSetupError} May throw errors during setup (e.g., configuration errors, network failures).\n * Errors thrown by this method are caught and logged by the controller, but do not prevent\n * controller initialization from completing successfully.\n *\n * @example\n * ```typescript\n * onSetupCompleted(analyticsId: string): void {\n * // Add platform-specific plugins that require analyticsId\n * client.add({\n * plugin: new PrivacyPlugin(analyticsId),\n * });\n * }\n * ```\n */\n onSetupCompleted(analyticsId: string): void;\n};\n"]}
|
|
@@ -1,39 +1,95 @@
|
|
|
1
1
|
import type { Json } from "@metamask/utils";
|
|
2
|
+
/**
|
|
3
|
+
* Represents values that can be passed as properties to the event tracking function.
|
|
4
|
+
* Similar to JsonValue from Segment SDK but decoupled for platform agnosticism.
|
|
5
|
+
*/
|
|
6
|
+
type AnalyticsJsonValue = boolean | number | string | null | AnalyticsJsonValue[] | AnalyticsJsonMap | undefined;
|
|
7
|
+
/**
|
|
8
|
+
* Represents the map object used to pass properties to the event tracking function.
|
|
9
|
+
* Similar to JsonMap from Segment SDK but decoupled for platform agnosticism.
|
|
10
|
+
*/
|
|
11
|
+
type AnalyticsJsonMap = {
|
|
12
|
+
[key: string]: AnalyticsJsonValue;
|
|
13
|
+
[index: number]: AnalyticsJsonValue;
|
|
14
|
+
};
|
|
2
15
|
/**
|
|
3
16
|
* Analytics event properties
|
|
4
17
|
*/
|
|
5
18
|
export type AnalyticsEventProperties = Record<string, Json>;
|
|
19
|
+
/**
|
|
20
|
+
* User traits/properties for analytics identification
|
|
21
|
+
*/
|
|
22
|
+
export type AnalyticsUserTraits = Record<string, Json>;
|
|
23
|
+
/**
|
|
24
|
+
* Event properties structure with two distinct properties lists for anonymous and non-anonymous data.
|
|
25
|
+
* Similar to ITrackingEvent from legacy analytics but decoupled for platform agnosticism.
|
|
26
|
+
*/
|
|
27
|
+
export type AnalyticsTrackingEvent = {
|
|
28
|
+
readonly name: string;
|
|
29
|
+
properties: AnalyticsJsonMap;
|
|
30
|
+
sensitiveProperties: AnalyticsJsonMap;
|
|
31
|
+
saveDataRecording: boolean;
|
|
32
|
+
readonly isAnonymous: boolean;
|
|
33
|
+
readonly hasProperties: boolean;
|
|
34
|
+
};
|
|
6
35
|
/**
|
|
7
36
|
* Platform adapter interface for analytics tracking
|
|
8
37
|
* Implementations should handle platform-specific details (Segment SDK, etc.)
|
|
9
|
-
*
|
|
10
|
-
* @todo This type is work in progress and will be updated as we
|
|
11
|
-
* integrate with the new analytics system on mobile.
|
|
12
|
-
* We have this draft type to help us iterate on the implementation.
|
|
13
|
-
* It will be updated with proper types as we create the mobile adapter
|
|
14
|
-
* And the controller package will be released only when this is completed.
|
|
15
38
|
*/
|
|
16
39
|
export type AnalyticsPlatformAdapter = {
|
|
17
40
|
/**
|
|
18
|
-
* Track an analytics event
|
|
41
|
+
* Track an analytics event.
|
|
42
|
+
*
|
|
43
|
+
* This is the same as trackEvent in the old analytics system
|
|
19
44
|
*
|
|
20
45
|
* @param eventName - The name of the event
|
|
21
46
|
* @param properties - Event properties
|
|
22
47
|
*/
|
|
23
|
-
|
|
48
|
+
track(eventName: string, properties: AnalyticsEventProperties): void;
|
|
24
49
|
/**
|
|
25
|
-
* Identify a user
|
|
50
|
+
* Identify a user with traits.
|
|
26
51
|
*
|
|
27
52
|
* @param userId - The user identifier (e.g., metametrics ID)
|
|
28
53
|
* @param traits - User traits/properties
|
|
29
54
|
*/
|
|
30
|
-
identify
|
|
55
|
+
identify(userId: string, traits?: AnalyticsUserTraits): void;
|
|
31
56
|
/**
|
|
32
|
-
* Track a page view
|
|
57
|
+
* Track a UI unit (page or screen) view depending on the platform
|
|
58
|
+
*
|
|
59
|
+
* This is the same as page in Segment web SDK and screen in Segment mobile SDK.
|
|
60
|
+
* Each platform adapter should implement this method to track UI views
|
|
61
|
+
* using the appropriate method for the platform: "pages" for web, "screen" for mobile.
|
|
33
62
|
*
|
|
34
|
-
* @param
|
|
63
|
+
* @param name - The name of the UI item being viewed (pages for web, screen for mobile)
|
|
35
64
|
* @param properties - Page properties
|
|
36
65
|
*/
|
|
37
|
-
|
|
66
|
+
view(name: string, properties?: AnalyticsEventProperties): void;
|
|
67
|
+
/**
|
|
68
|
+
* Lifecycle hook called after the AnalyticsController is fully initialized.
|
|
69
|
+
*
|
|
70
|
+
* This hook allows platform-specific adapters to perform setup that requires
|
|
71
|
+
* access to the controller's state (e.g., analyticsId).
|
|
72
|
+
*
|
|
73
|
+
* The controller calls this method once after initialization, passing the
|
|
74
|
+
* analyticsId from controller state. The analyticsId is guaranteed to be set
|
|
75
|
+
* when this method is called - this is the definition of "completed" setup.
|
|
76
|
+
*
|
|
77
|
+
* @param analyticsId - The analytics ID from controller state. Always set (never empty).
|
|
78
|
+
* @throws {AnalyticsPlatformAdapterSetupError} May throw errors during setup (e.g., configuration errors, network failures).
|
|
79
|
+
* Errors thrown by this method are caught and logged by the controller, but do not prevent
|
|
80
|
+
* controller initialization from completing successfully.
|
|
81
|
+
*
|
|
82
|
+
* @example
|
|
83
|
+
* ```typescript
|
|
84
|
+
* onSetupCompleted(analyticsId: string): void {
|
|
85
|
+
* // Add platform-specific plugins that require analyticsId
|
|
86
|
+
* client.add({
|
|
87
|
+
* plugin: new PrivacyPlugin(analyticsId),
|
|
88
|
+
* });
|
|
89
|
+
* }
|
|
90
|
+
* ```
|
|
91
|
+
*/
|
|
92
|
+
onSetupCompleted(analyticsId: string): void;
|
|
38
93
|
};
|
|
94
|
+
export {};
|
|
39
95
|
//# sourceMappingURL=AnalyticsPlatformAdapter.types.d.cts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AnalyticsPlatformAdapter.types.d.cts","sourceRoot":"","sources":["../src/AnalyticsPlatformAdapter.types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAE,wBAAwB;AAE5C;;GAEG;AACH,MAAM,MAAM,wBAAwB,GAAG,MAAM,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC;AAE5D
|
|
1
|
+
{"version":3,"file":"AnalyticsPlatformAdapter.types.d.cts","sourceRoot":"","sources":["../src/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IACH,gBAAgB,CAAC,WAAW,EAAE,MAAM,GAAG,IAAI,CAAC;CAC7C,CAAC"}
|
|
@@ -1,39 +1,95 @@
|
|
|
1
1
|
import type { Json } from "@metamask/utils";
|
|
2
|
+
/**
|
|
3
|
+
* Represents values that can be passed as properties to the event tracking function.
|
|
4
|
+
* Similar to JsonValue from Segment SDK but decoupled for platform agnosticism.
|
|
5
|
+
*/
|
|
6
|
+
type AnalyticsJsonValue = boolean | number | string | null | AnalyticsJsonValue[] | AnalyticsJsonMap | undefined;
|
|
7
|
+
/**
|
|
8
|
+
* Represents the map object used to pass properties to the event tracking function.
|
|
9
|
+
* Similar to JsonMap from Segment SDK but decoupled for platform agnosticism.
|
|
10
|
+
*/
|
|
11
|
+
type AnalyticsJsonMap = {
|
|
12
|
+
[key: string]: AnalyticsJsonValue;
|
|
13
|
+
[index: number]: AnalyticsJsonValue;
|
|
14
|
+
};
|
|
2
15
|
/**
|
|
3
16
|
* Analytics event properties
|
|
4
17
|
*/
|
|
5
18
|
export type AnalyticsEventProperties = Record<string, Json>;
|
|
19
|
+
/**
|
|
20
|
+
* User traits/properties for analytics identification
|
|
21
|
+
*/
|
|
22
|
+
export type AnalyticsUserTraits = Record<string, Json>;
|
|
23
|
+
/**
|
|
24
|
+
* Event properties structure with two distinct properties lists for anonymous and non-anonymous data.
|
|
25
|
+
* Similar to ITrackingEvent from legacy analytics but decoupled for platform agnosticism.
|
|
26
|
+
*/
|
|
27
|
+
export type AnalyticsTrackingEvent = {
|
|
28
|
+
readonly name: string;
|
|
29
|
+
properties: AnalyticsJsonMap;
|
|
30
|
+
sensitiveProperties: AnalyticsJsonMap;
|
|
31
|
+
saveDataRecording: boolean;
|
|
32
|
+
readonly isAnonymous: boolean;
|
|
33
|
+
readonly hasProperties: boolean;
|
|
34
|
+
};
|
|
6
35
|
/**
|
|
7
36
|
* Platform adapter interface for analytics tracking
|
|
8
37
|
* Implementations should handle platform-specific details (Segment SDK, etc.)
|
|
9
|
-
*
|
|
10
|
-
* @todo This type is work in progress and will be updated as we
|
|
11
|
-
* integrate with the new analytics system on mobile.
|
|
12
|
-
* We have this draft type to help us iterate on the implementation.
|
|
13
|
-
* It will be updated with proper types as we create the mobile adapter
|
|
14
|
-
* And the controller package will be released only when this is completed.
|
|
15
38
|
*/
|
|
16
39
|
export type AnalyticsPlatformAdapter = {
|
|
17
40
|
/**
|
|
18
|
-
* Track an analytics event
|
|
41
|
+
* Track an analytics event.
|
|
42
|
+
*
|
|
43
|
+
* This is the same as trackEvent in the old analytics system
|
|
19
44
|
*
|
|
20
45
|
* @param eventName - The name of the event
|
|
21
46
|
* @param properties - Event properties
|
|
22
47
|
*/
|
|
23
|
-
|
|
48
|
+
track(eventName: string, properties: AnalyticsEventProperties): void;
|
|
24
49
|
/**
|
|
25
|
-
* Identify a user
|
|
50
|
+
* Identify a user with traits.
|
|
26
51
|
*
|
|
27
52
|
* @param userId - The user identifier (e.g., metametrics ID)
|
|
28
53
|
* @param traits - User traits/properties
|
|
29
54
|
*/
|
|
30
|
-
identify
|
|
55
|
+
identify(userId: string, traits?: AnalyticsUserTraits): void;
|
|
31
56
|
/**
|
|
32
|
-
* Track a page view
|
|
57
|
+
* Track a UI unit (page or screen) view depending on the platform
|
|
58
|
+
*
|
|
59
|
+
* This is the same as page in Segment web SDK and screen in Segment mobile SDK.
|
|
60
|
+
* Each platform adapter should implement this method to track UI views
|
|
61
|
+
* using the appropriate method for the platform: "pages" for web, "screen" for mobile.
|
|
33
62
|
*
|
|
34
|
-
* @param
|
|
63
|
+
* @param name - The name of the UI item being viewed (pages for web, screen for mobile)
|
|
35
64
|
* @param properties - Page properties
|
|
36
65
|
*/
|
|
37
|
-
|
|
66
|
+
view(name: string, properties?: AnalyticsEventProperties): void;
|
|
67
|
+
/**
|
|
68
|
+
* Lifecycle hook called after the AnalyticsController is fully initialized.
|
|
69
|
+
*
|
|
70
|
+
* This hook allows platform-specific adapters to perform setup that requires
|
|
71
|
+
* access to the controller's state (e.g., analyticsId).
|
|
72
|
+
*
|
|
73
|
+
* The controller calls this method once after initialization, passing the
|
|
74
|
+
* analyticsId from controller state. The analyticsId is guaranteed to be set
|
|
75
|
+
* when this method is called - this is the definition of "completed" setup.
|
|
76
|
+
*
|
|
77
|
+
* @param analyticsId - The analytics ID from controller state. Always set (never empty).
|
|
78
|
+
* @throws {AnalyticsPlatformAdapterSetupError} May throw errors during setup (e.g., configuration errors, network failures).
|
|
79
|
+
* Errors thrown by this method are caught and logged by the controller, but do not prevent
|
|
80
|
+
* controller initialization from completing successfully.
|
|
81
|
+
*
|
|
82
|
+
* @example
|
|
83
|
+
* ```typescript
|
|
84
|
+
* onSetupCompleted(analyticsId: string): void {
|
|
85
|
+
* // Add platform-specific plugins that require analyticsId
|
|
86
|
+
* client.add({
|
|
87
|
+
* plugin: new PrivacyPlugin(analyticsId),
|
|
88
|
+
* });
|
|
89
|
+
* }
|
|
90
|
+
* ```
|
|
91
|
+
*/
|
|
92
|
+
onSetupCompleted(analyticsId: string): void;
|
|
38
93
|
};
|
|
94
|
+
export {};
|
|
39
95
|
//# sourceMappingURL=AnalyticsPlatformAdapter.types.d.mts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AnalyticsPlatformAdapter.types.d.mts","sourceRoot":"","sources":["../src/AnalyticsPlatformAdapter.types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAE,wBAAwB;AAE5C;;GAEG;AACH,MAAM,MAAM,wBAAwB,GAAG,MAAM,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC;AAE5D
|
|
1
|
+
{"version":3,"file":"AnalyticsPlatformAdapter.types.d.mts","sourceRoot":"","sources":["../src/AnalyticsPlatformAdapter.types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAE,wBAAwB;AAE5C;;;GAGG;AACH,KAAK,kBAAkB,GACnB,OAAO,GACP,MAAM,GACN,MAAM,GACN,IAAI,GACJ,kBAAkB,EAAE,GACp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"names":[],"mappings":"","sourcesContent":["import type { Json } from '@metamask/utils';\n\n/**\n *
|
|
1
|
+
{"version":3,"file":"AnalyticsPlatformAdapter.types.mjs","sourceRoot":"","sources":["../src/AnalyticsPlatformAdapter.types.ts"],"names":[],"mappings":"","sourcesContent":["import type { Json } from '@metamask/utils';\n\n/**\n * Represents values that can be passed as properties to the event tracking function.\n * Similar to JsonValue from Segment SDK but decoupled for platform agnosticism.\n */\ntype AnalyticsJsonValue =\n | boolean\n | number\n | string\n | null\n | AnalyticsJsonValue[]\n | AnalyticsJsonMap\n | undefined;\n\n/**\n * Represents the map object used to pass properties to the event tracking function.\n * Similar to JsonMap from Segment SDK but decoupled for platform agnosticism.\n */\ntype AnalyticsJsonMap = {\n [key: string]: AnalyticsJsonValue;\n [index: number]: AnalyticsJsonValue;\n};\n\n/**\n * Analytics event properties\n */\nexport type AnalyticsEventProperties = Record<string, Json>;\n\n/**\n * User traits/properties for analytics identification\n */\nexport type AnalyticsUserTraits = Record<string, Json>;\n\n/**\n * Event properties structure with two distinct properties lists for anonymous and non-anonymous data.\n * Similar to ITrackingEvent from legacy analytics but decoupled for platform agnosticism.\n */\nexport type AnalyticsTrackingEvent = {\n readonly name: string;\n properties: AnalyticsJsonMap;\n sensitiveProperties: AnalyticsJsonMap;\n saveDataRecording: boolean;\n readonly isAnonymous: boolean;\n readonly hasProperties: boolean;\n};\n\n/**\n * Platform adapter interface for analytics tracking\n * Implementations should handle platform-specific details (Segment SDK, etc.)\n */\nexport type AnalyticsPlatformAdapter = {\n /**\n * Track an analytics event.\n *\n * This is the same as trackEvent in the old analytics system\n *\n * @param eventName - The name of the event\n * @param properties - Event properties\n */\n track(eventName: string, properties: AnalyticsEventProperties): void;\n\n /**\n * Identify a user with traits.\n *\n * @param userId - The user identifier (e.g., metametrics ID)\n * @param traits - User traits/properties\n */\n identify(userId: string, traits?: AnalyticsUserTraits): void;\n\n /**\n * Track a UI unit (page or screen) view depending on the platform\n *\n * This is the same as page in Segment web SDK and screen in Segment mobile SDK.\n * Each platform adapter should implement this method to track UI views\n * using the appropriate method for the platform: \"pages\" for web, \"screen\" for mobile.\n *\n * @param name - The name of the UI item being viewed (pages for web, screen for mobile)\n * @param properties - Page properties\n */\n view(name: string, properties?: AnalyticsEventProperties): void;\n\n /**\n * Lifecycle hook called after the AnalyticsController is fully initialized.\n *\n * This hook allows platform-specific adapters to perform setup that requires\n * access to the controller's state (e.g., analyticsId).\n *\n * The controller calls this method once after initialization, passing the\n * analyticsId from controller state. The analyticsId is guaranteed to be set\n * when this method is called - this is the definition of \"completed\" setup.\n *\n * @param analyticsId - The analytics ID from controller state. Always set (never empty).\n * @throws {AnalyticsPlatformAdapterSetupError} May throw errors during setup (e.g., configuration errors, network failures).\n * Errors thrown by this method are caught and logged by the controller, but do not prevent\n * controller initialization from completing successfully.\n *\n * @example\n * ```typescript\n * onSetupCompleted(analyticsId: string): void {\n * // Add platform-specific plugins that require analyticsId\n * client.add({\n * plugin: new PrivacyPlugin(analyticsId),\n * });\n * }\n * ```\n */\n onSetupCompleted(analyticsId: string): void;\n};\n"]}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AnalyticsPlatformAdapterSetupError = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Error thrown when platform adapter setup fails during the onSetupCompleted lifecycle hook.
|
|
6
|
+
*/
|
|
7
|
+
class AnalyticsPlatformAdapterSetupError extends Error {
|
|
8
|
+
constructor(message, cause) {
|
|
9
|
+
super(message);
|
|
10
|
+
this.name = this.constructor.name;
|
|
11
|
+
if (cause) {
|
|
12
|
+
this.cause = cause;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
exports.AnalyticsPlatformAdapterSetupError = AnalyticsPlatformAdapterSetupError;
|
|
17
|
+
//# sourceMappingURL=AnalyticsPlatformAdapterSetupError.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"AnalyticsPlatformAdapterSetupError.cjs","sourceRoot":"","sources":["../src/AnalyticsPlatformAdapterSetupError.ts"],"names":[],"mappings":";;;AAAA;;GAEG;AACH,MAAa,kCAAmC,SAAQ,KAAK;IAG3D,YAAY,OAAe,EAAE,KAAa;QACxC,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC;QAClC,IAAI,KAAK,EAAE,CAAC;YACV,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;QACrB,CAAC;IACH,CAAC;CACF;AAVD,gFAUC","sourcesContent":["/**\n * Error thrown when platform adapter setup fails during the onSetupCompleted lifecycle hook.\n */\nexport class AnalyticsPlatformAdapterSetupError extends Error {\n cause?: Error;\n\n constructor(message: string, cause?: Error) {\n super(message);\n this.name = this.constructor.name;\n if (cause) {\n this.cause = cause;\n }\n }\n}\n"]}
|