@metamask-previews/analytics-controller 0.0.0-preview-de995bed → 0.0.0-preview-cb4a07d5
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 +30 -174
- package/dist/AnalyticsController-method-action-types.cjs.map +1 -1
- package/dist/AnalyticsController-method-action-types.d.cts +8 -24
- package/dist/AnalyticsController-method-action-types.d.cts.map +1 -1
- package/dist/AnalyticsController-method-action-types.d.mts +8 -24
- 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 +102 -78
- package/dist/AnalyticsController.cjs.map +1 -1
- package/dist/AnalyticsController.d.cts +48 -34
- package/dist/AnalyticsController.d.cts.map +1 -1
- package/dist/AnalyticsController.d.mts +48 -34
- package/dist/AnalyticsController.d.mts.map +1 -1
- package/dist/AnalyticsController.mjs +103 -79
- package/dist/AnalyticsController.mjs.map +1 -1
- package/dist/AnalyticsPlatformAdapter.types.cjs.map +1 -1
- package/dist/AnalyticsPlatformAdapter.types.d.cts +63 -15
- package/dist/AnalyticsPlatformAdapter.types.d.cts.map +1 -1
- package/dist/AnalyticsPlatformAdapter.types.d.mts +63 -15
- 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/analyticsControllerStateValidator.cjs +34 -0
- package/dist/analyticsControllerStateValidator.cjs.map +1 -0
- package/dist/analyticsControllerStateValidator.d.cts +16 -0
- package/dist/analyticsControllerStateValidator.d.cts.map +1 -0
- package/dist/analyticsControllerStateValidator.d.mts +16 -0
- package/dist/analyticsControllerStateValidator.d.mts.map +1 -0
- package/dist/analyticsControllerStateValidator.mjs +29 -0
- package/dist/analyticsControllerStateValidator.mjs.map +1 -0
- package/dist/index.cjs +9 -4
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +6 -5
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +6 -5
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +6 -3
- package/dist/index.mjs.map +1 -1
- package/dist/selectors.cjs +36 -0
- package/dist/selectors.cjs.map +1 -0
- package/dist/selectors.d.cts +11 -0
- package/dist/selectors.d.cts.map +1 -0
- package/dist/selectors.d.mts +11 -0
- package/dist/selectors.d.mts.map +1 -0
- package/dist/selectors.mjs +33 -0
- package/dist/selectors.mjs.map +1 -0
- package/package.json +2 -3
|
@@ -10,12 +10,13 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
|
|
|
10
10
|
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");
|
|
11
11
|
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
12
12
|
};
|
|
13
|
-
var _AnalyticsController_platformAdapter;
|
|
13
|
+
var _AnalyticsController_platformAdapter, _AnalyticsController_isAnonymousEventsFeatureEnabled, _AnalyticsController_initialized;
|
|
14
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
15
|
exports.AnalyticsController = exports.getDefaultAnalyticsControllerState = exports.controllerName = void 0;
|
|
16
16
|
const base_controller_1 = require("@metamask/base-controller");
|
|
17
|
-
const
|
|
17
|
+
const analyticsControllerStateValidator_1 = require("./analyticsControllerStateValidator.cjs");
|
|
18
18
|
const AnalyticsLogger_1 = require("./AnalyticsLogger.cjs");
|
|
19
|
+
const selectors_1 = require("./selectors.cjs");
|
|
19
20
|
// === GENERAL ===
|
|
20
21
|
/**
|
|
21
22
|
* The name of the {@link AnalyticsController}, used to namespace the
|
|
@@ -23,16 +24,29 @@ const AnalyticsLogger_1 = require("./AnalyticsLogger.cjs");
|
|
|
23
24
|
* when composed with other controllers.
|
|
24
25
|
*/
|
|
25
26
|
exports.controllerName = 'AnalyticsController';
|
|
27
|
+
/**
|
|
28
|
+
* Returns default values for AnalyticsController state.
|
|
29
|
+
*
|
|
30
|
+
* Note: analyticsId is NOT included - it's an identity that must be
|
|
31
|
+
* provided by the platform (generated once on first run, then persisted).
|
|
32
|
+
*
|
|
33
|
+
* @returns Default state without analyticsId
|
|
34
|
+
*/
|
|
35
|
+
function getDefaultAnalyticsControllerState() {
|
|
36
|
+
return {
|
|
37
|
+
optedIn: false,
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
exports.getDefaultAnalyticsControllerState = getDefaultAnalyticsControllerState;
|
|
26
41
|
/**
|
|
27
42
|
* The metadata for each property in {@link AnalyticsControllerState}.
|
|
43
|
+
*
|
|
44
|
+
* Note: `optedIn` is persisted by the controller (`persist: true`).
|
|
45
|
+
* `analyticsId` is persisted by the platform (`persist: false`) and provided
|
|
46
|
+
* via initial state. The platform should subscribe to `stateChange` events
|
|
47
|
+
* to persist any state changes.
|
|
28
48
|
*/
|
|
29
49
|
const analyticsControllerMetadata = {
|
|
30
|
-
enabled: {
|
|
31
|
-
includeInStateLogs: true,
|
|
32
|
-
persist: true,
|
|
33
|
-
includeInDebugSnapshot: true,
|
|
34
|
-
usedInUi: true,
|
|
35
|
-
},
|
|
36
50
|
optedIn: {
|
|
37
51
|
includeInStateLogs: true,
|
|
38
52
|
persist: true,
|
|
@@ -41,34 +55,16 @@ const analyticsControllerMetadata = {
|
|
|
41
55
|
},
|
|
42
56
|
analyticsId: {
|
|
43
57
|
includeInStateLogs: true,
|
|
44
|
-
persist:
|
|
58
|
+
persist: false,
|
|
45
59
|
includeInDebugSnapshot: true,
|
|
46
60
|
usedInUi: false,
|
|
47
61
|
},
|
|
48
62
|
};
|
|
49
|
-
/**
|
|
50
|
-
* Constructs the default {@link AnalyticsController} state. This allows
|
|
51
|
-
* consumers to provide a partial state object when initializing the controller
|
|
52
|
-
* and also helps in constructing complete state objects for this controller in
|
|
53
|
-
* tests.
|
|
54
|
-
*
|
|
55
|
-
* @returns The default {@link AnalyticsController} state.
|
|
56
|
-
*/
|
|
57
|
-
function getDefaultAnalyticsControllerState() {
|
|
58
|
-
return {
|
|
59
|
-
enabled: true,
|
|
60
|
-
optedIn: false,
|
|
61
|
-
analyticsId: (0, uuid_1.v4)(),
|
|
62
|
-
};
|
|
63
|
-
}
|
|
64
|
-
exports.getDefaultAnalyticsControllerState = getDefaultAnalyticsControllerState;
|
|
65
63
|
// === MESSENGER ===
|
|
66
64
|
const MESSENGER_EXPOSED_METHODS = [
|
|
67
65
|
'trackEvent',
|
|
68
66
|
'identify',
|
|
69
|
-
'
|
|
70
|
-
'enable',
|
|
71
|
-
'disable',
|
|
67
|
+
'trackView',
|
|
72
68
|
'optIn',
|
|
73
69
|
'optOut',
|
|
74
70
|
];
|
|
@@ -81,100 +77,128 @@ const MESSENGER_EXPOSED_METHODS = [
|
|
|
81
77
|
* This controller follows the MetaMask controller pattern and integrates with the
|
|
82
78
|
* messenger system to allow other controllers and components to track analytics events.
|
|
83
79
|
* It delegates platform-specific implementation to an {@link AnalyticsPlatformAdapter}.
|
|
80
|
+
*
|
|
81
|
+
* Note: The controller persists `optedIn` internally. The `analyticsId` is persisted
|
|
82
|
+
* by the platform and must be provided via initial state. The platform should subscribe
|
|
83
|
+
* to `AnalyticsController:stateChange` events to persist any state changes.
|
|
84
84
|
*/
|
|
85
85
|
class AnalyticsController extends base_controller_1.BaseController {
|
|
86
86
|
/**
|
|
87
87
|
* Constructs an AnalyticsController instance.
|
|
88
88
|
*
|
|
89
89
|
* @param options - Controller options
|
|
90
|
-
* @param options.state - Initial controller state
|
|
90
|
+
* @param options.state - Initial controller state. Must include a valid UUIDv4 `analyticsId`.
|
|
91
|
+
* Use `getDefaultAnalyticsControllerState()` for default opt-in preferences.
|
|
91
92
|
* @param options.messenger - Messenger used to communicate with BaseController
|
|
92
93
|
* @param options.platformAdapter - Platform adapter implementation for tracking
|
|
94
|
+
* @param options.isAnonymousEventsFeatureEnabled - Whether the anonymous events feature is enabled
|
|
95
|
+
* @throws Error if state.analyticsId is missing or not a valid UUIDv4
|
|
96
|
+
* @remarks After construction, call {@link AnalyticsController.init} to complete initialization.
|
|
93
97
|
*/
|
|
94
|
-
constructor({ state
|
|
98
|
+
constructor({ state, messenger, platformAdapter, isAnonymousEventsFeatureEnabled = false, }) {
|
|
99
|
+
const initialState = {
|
|
100
|
+
...getDefaultAnalyticsControllerState(),
|
|
101
|
+
...state,
|
|
102
|
+
};
|
|
103
|
+
(0, analyticsControllerStateValidator_1.validateAnalyticsControllerState)(initialState);
|
|
95
104
|
super({
|
|
96
105
|
name: exports.controllerName,
|
|
97
106
|
metadata: analyticsControllerMetadata,
|
|
98
|
-
state:
|
|
99
|
-
...getDefaultAnalyticsControllerState(),
|
|
100
|
-
...state,
|
|
101
|
-
},
|
|
107
|
+
state: initialState,
|
|
102
108
|
messenger,
|
|
103
109
|
});
|
|
104
110
|
_AnalyticsController_platformAdapter.set(this, void 0);
|
|
111
|
+
_AnalyticsController_isAnonymousEventsFeatureEnabled.set(this, void 0);
|
|
112
|
+
_AnalyticsController_initialized.set(this, void 0);
|
|
113
|
+
__classPrivateFieldSet(this, _AnalyticsController_isAnonymousEventsFeatureEnabled, isAnonymousEventsFeatureEnabled, "f");
|
|
105
114
|
__classPrivateFieldSet(this, _AnalyticsController_platformAdapter, platformAdapter, "f");
|
|
115
|
+
__classPrivateFieldSet(this, _AnalyticsController_initialized, false, "f");
|
|
106
116
|
this.messenger.registerMethodActionHandlers(this, MESSENGER_EXPOSED_METHODS);
|
|
107
117
|
(0, AnalyticsLogger_1.projectLogger)('AnalyticsController initialized and ready', {
|
|
108
|
-
enabled: this.state
|
|
118
|
+
enabled: selectors_1.analyticsControllerSelectors.selectEnabled(this.state),
|
|
109
119
|
optedIn: this.state.optedIn,
|
|
110
120
|
analyticsId: this.state.analyticsId,
|
|
111
121
|
});
|
|
112
122
|
}
|
|
123
|
+
/**
|
|
124
|
+
* Initialize the controller by calling the platform adapter's onSetupCompleted lifecycle hook.
|
|
125
|
+
* This method must be called after construction to complete the setup process.
|
|
126
|
+
*/
|
|
127
|
+
init() {
|
|
128
|
+
if (__classPrivateFieldGet(this, _AnalyticsController_initialized, "f")) {
|
|
129
|
+
(0, AnalyticsLogger_1.projectLogger)('AnalyticsController already initialized.');
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
__classPrivateFieldSet(this, _AnalyticsController_initialized, true, "f");
|
|
133
|
+
// Call onSetupCompleted lifecycle hook after initialization
|
|
134
|
+
// State is already validated, so analyticsId is guaranteed to be a valid UUIDv4
|
|
135
|
+
try {
|
|
136
|
+
__classPrivateFieldGet(this, _AnalyticsController_platformAdapter, "f").onSetupCompleted(this.state.analyticsId);
|
|
137
|
+
}
|
|
138
|
+
catch (error) {
|
|
139
|
+
// Log error but don't throw - adapter setup failure shouldn't break controller
|
|
140
|
+
(0, AnalyticsLogger_1.projectLogger)('Error calling platformAdapter.onSetupCompleted', error);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
113
143
|
/**
|
|
114
144
|
* Track an analytics event.
|
|
115
145
|
*
|
|
116
146
|
* Events are only tracked if analytics is enabled.
|
|
117
147
|
*
|
|
118
|
-
* @param
|
|
119
|
-
* @param properties - Event properties
|
|
148
|
+
* @param event - Analytics event with properties and sensitive properties
|
|
120
149
|
*/
|
|
121
|
-
trackEvent(
|
|
150
|
+
trackEvent(event) {
|
|
122
151
|
// Don't track if analytics is disabled
|
|
123
|
-
if (!this.state
|
|
152
|
+
if (!selectors_1.analyticsControllerSelectors.selectEnabled(this.state)) {
|
|
124
153
|
return;
|
|
125
154
|
}
|
|
126
|
-
//
|
|
127
|
-
|
|
155
|
+
// if event does not have properties, send event without properties
|
|
156
|
+
// and return to prevent any additional processing
|
|
157
|
+
if (!event.hasProperties) {
|
|
158
|
+
__classPrivateFieldGet(this, _AnalyticsController_platformAdapter, "f").track(event.name);
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
// Track regular properties first if anonymous events feature is enabled
|
|
162
|
+
if (__classPrivateFieldGet(this, _AnalyticsController_isAnonymousEventsFeatureEnabled, "f")) {
|
|
163
|
+
// Note: Even if regular properties object is empty, we still send it to ensure
|
|
164
|
+
// an event with user ID is tracked.
|
|
165
|
+
__classPrivateFieldGet(this, _AnalyticsController_platformAdapter, "f").track(event.name, {
|
|
166
|
+
...event.properties,
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
const hasSensitiveProperties = Object.keys(event.sensitiveProperties).length > 0;
|
|
170
|
+
if (!__classPrivateFieldGet(this, _AnalyticsController_isAnonymousEventsFeatureEnabled, "f") || hasSensitiveProperties) {
|
|
171
|
+
__classPrivateFieldGet(this, _AnalyticsController_platformAdapter, "f").track(event.name, {
|
|
172
|
+
...event.properties,
|
|
173
|
+
...event.sensitiveProperties,
|
|
174
|
+
...(hasSensitiveProperties && { anonymous: true }),
|
|
175
|
+
});
|
|
176
|
+
}
|
|
128
177
|
}
|
|
129
178
|
/**
|
|
130
179
|
* Identify a user for analytics.
|
|
131
180
|
*
|
|
132
|
-
* @param userId - The user identifier (e.g., metametrics ID)
|
|
133
181
|
* @param traits - User traits/properties
|
|
134
182
|
*/
|
|
135
|
-
identify(
|
|
136
|
-
if (!this.state
|
|
183
|
+
identify(traits) {
|
|
184
|
+
if (!selectors_1.analyticsControllerSelectors.selectEnabled(this.state)) {
|
|
137
185
|
return;
|
|
138
186
|
}
|
|
139
|
-
//
|
|
140
|
-
this.
|
|
141
|
-
state.analyticsId = userId;
|
|
142
|
-
});
|
|
143
|
-
// Delegate to platform adapter if supported
|
|
144
|
-
if (__classPrivateFieldGet(this, _AnalyticsController_platformAdapter, "f").identify) {
|
|
145
|
-
__classPrivateFieldGet(this, _AnalyticsController_platformAdapter, "f").identify(userId, traits);
|
|
146
|
-
}
|
|
187
|
+
// Delegate to platform adapter using the current analytics ID
|
|
188
|
+
__classPrivateFieldGet(this, _AnalyticsController_platformAdapter, "f").identify(this.state.analyticsId, traits);
|
|
147
189
|
}
|
|
148
190
|
/**
|
|
149
|
-
* Track a page view.
|
|
191
|
+
* Track a page or screen view.
|
|
150
192
|
*
|
|
151
|
-
* @param
|
|
152
|
-
* @param properties -
|
|
193
|
+
* @param name - The identifier/name of the page or screen being viewed (e.g., "home", "settings", "wallet")
|
|
194
|
+
* @param properties - Optional properties associated with the view
|
|
153
195
|
*/
|
|
154
|
-
|
|
155
|
-
if (!this.state
|
|
196
|
+
trackView(name, properties) {
|
|
197
|
+
if (!selectors_1.analyticsControllerSelectors.selectEnabled(this.state)) {
|
|
156
198
|
return;
|
|
157
199
|
}
|
|
158
|
-
// Delegate to platform adapter
|
|
159
|
-
|
|
160
|
-
__classPrivateFieldGet(this, _AnalyticsController_platformAdapter, "f").trackPage(pageName, properties);
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
/**
|
|
164
|
-
* Enable analytics tracking.
|
|
165
|
-
*/
|
|
166
|
-
enable() {
|
|
167
|
-
this.update((state) => {
|
|
168
|
-
state.enabled = true;
|
|
169
|
-
});
|
|
170
|
-
}
|
|
171
|
-
/**
|
|
172
|
-
* Disable analytics tracking.
|
|
173
|
-
*/
|
|
174
|
-
disable() {
|
|
175
|
-
this.update((state) => {
|
|
176
|
-
state.enabled = false;
|
|
177
|
-
});
|
|
200
|
+
// Delegate to platform adapter
|
|
201
|
+
__classPrivateFieldGet(this, _AnalyticsController_platformAdapter, "f").view(name, properties);
|
|
178
202
|
}
|
|
179
203
|
/**
|
|
180
204
|
* Opt in to analytics.
|
|
@@ -194,5 +218,5 @@ class AnalyticsController extends base_controller_1.BaseController {
|
|
|
194
218
|
}
|
|
195
219
|
}
|
|
196
220
|
exports.AnalyticsController = AnalyticsController;
|
|
197
|
-
_AnalyticsController_platformAdapter = new WeakMap();
|
|
221
|
+
_AnalyticsController_platformAdapter = new WeakMap(), _AnalyticsController_isAnonymousEventsFeatureEnabled = new WeakMap(), _AnalyticsController_initialized = new WeakMap();
|
|
198
222
|
//# sourceMappingURL=AnalyticsController.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AnalyticsController.cjs","sourceRoot":"","sources":["../src/AnalyticsController.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAKA,+DAA2D;AAE3D,+BAAoC;AAGpC,2DAAkD;AAMlD,kBAAkB;AAElB;;;;GAIG;AACU,QAAA,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,SAAgB,kCAAkC;IAChD,OAAO;QACL,OAAO,EAAE,IAAI;QACb,OAAO,EAAE,KAAK;QACd,WAAW,EAAE,IAAA,SAAM,GAAE;KACtB,CAAC;AACJ,CAAC;AAND,gFAMC;AAED,oBAAoB;AAEpB,MAAM,yBAAyB,GAAG;IAChC,YAAY;IACZ,UAAU;IACV,WAAW;IACX,QAAQ;IACR,SAAS;IACT,OAAO;IACP,QAAQ;CACA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AAA,IAAI,4CAAiB,CAAC,QAAQ,EAAE,CAAC;YACnC,uBAAA,IAAI,4CAAiB,CAAC,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;QACjD,CAAC;IACH,CAAC;IAED;;;;;OAKG;IACH,SAAS,CAAC,QAAgB,EAAE,UAAqC;QAC/D,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,OAAO,EAAE,CAAC;YACxB,OAAO;QACT,CAAC;QAED,4CAA4C;QAC5C,IAAI,uBAAA,IAAI,4CAAiB,CAAC,SAAS,EAAE,CAAC;YACpC,uBAAA,IAAI,4CAAiB,CAAC,SAAS,CAAC,QAAQ,EAAE,UAAU,CAAC,CAAC;QACxD,CAAC;IACH,CAAC;IAED;;OAEG;IACH,MAAM;QACJ,IAAI,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE;YACpB,KAAK,CAAC,OAAO,GAAG,IAAI,CAAC;QACvB,CAAC,CAAC,CAAC;IACL,CAAC;IAED;;OAEG;IACH,OAAO;QACL,IAAI,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE;YACpB,KAAK,CAAC,OAAO,GAAG,KAAK,CAAC;QACxB,CAAC,CAAC,CAAC;IACL,CAAC;IAED;;OAEG;IACH,KAAK;QACH,IAAI,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE;YACpB,KAAK,CAAC,OAAO,GAAG,IAAI,CAAC;QACvB,CAAC,CAAC,CAAC;IACL,CAAC;IAED;;OAEG;IACH,MAAM;QACJ,IAAI,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE;YACpB,KAAK,CAAC,OAAO,GAAG,KAAK,CAAC;QACxB,CAAC,CAAC,CAAC;IACL,CAAC;CACF;AA3ID,kDA2IC","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 { 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.cjs","sourceRoot":"","sources":["../src/AnalyticsController.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAKA,+DAA2D;AAI3D,+FAAuF;AACvF,2DAAyD;AAOzD,+CAA2D;AAE3D,kBAAkB;AAElB;;;;GAIG;AACU,QAAA,cAAc,GAAG,qBAAqB,CAAC;AAqBpD;;;;;;;GAOG;AACH,SAAgB,kCAAkC;IAIhD,OAAO;QACL,OAAO,EAAE,KAAK;KACf,CAAC;AACJ,CAAC;AAPD,gFAOC;AAED;;;;;;;GAOG;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,WAAW,EAAE;QACX,kBAAkB,EAAE,IAAI;QACxB,OAAO,EAAE,KAAK;QACd,sBAAsB,EAAE,IAAI;QAC5B,QAAQ,EAAE,KAAK;KAChB;CACgD,CAAC;AAEpD,oBAAoB;AAEpB,MAAM,yBAAyB,GAAG;IAChC,YAAY;IACZ,UAAU;IACV,WAAW;IACX,OAAO;IACP,QAAQ;CACA,CAAC;AA8EX;;;;;;;;;;;;;GAaG;AACH,MAAa,mBAAoB,SAAQ,gCAIxC;IAOC;;;;;;;;;;;OAWG;IACH,YAAY,EACV,KAAK,EACL,SAAS,EACT,eAAe,EACf,+BAA+B,GAAG,KAAK,GACZ;QAC3B,MAAM,YAAY,GAA6B;YAC7C,GAAG,kCAAkC,EAAE;YACvC,GAAG,KAAK;SACT,CAAC;QAEF,IAAA,oEAAgC,EAAC,YAAY,CAAC,CAAC;QAE/C,KAAK,CAAC;YACJ,IAAI,EAAE,sBAAc;YACp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BAAA,IAAI,4CAAiB,CAAC,IAAI,CAAC,IAAI,EAAE,UAAU,CAAC,CAAC;IAC/C,CAAC;IAED;;OAEG;IACH,KAAK;QACH,IAAI,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE;YACpB,KAAK,CAAC,OAAO,GAAG,IAAI,CAAC;QACvB,CAAC,CAAC,CAAC;IACL,CAAC;IAED;;OAEG;IACH,MAAM;QACJ,IAAI,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE;YACpB,KAAK,CAAC,OAAO,GAAG,KAAK,CAAC;QACxB,CAAC,CAAC,CAAC;IACL,CAAC;CACF;AAxKD,kDAwKC","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';\n\nimport type { AnalyticsControllerMethodActions } from './AnalyticsController-method-action-types';\nimport { validateAnalyticsControllerState } from './analyticsControllerStateValidator';\nimport { projectLogger as log } from './AnalyticsLogger';\nimport type {\n AnalyticsPlatformAdapter,\n AnalyticsEventProperties,\n AnalyticsUserTraits,\n AnalyticsTrackingEvent,\n} from './AnalyticsPlatformAdapter.types';\nimport { analyticsControllerSelectors } from './selectors';\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 the user has opted in to analytics.\n */\n optedIn: boolean;\n\n /**\n * User's UUIDv4 analytics identifier.\n * This is an identity (unique per user), not a preference.\n * Must be provided by the platform - the controller does not generate it.\n */\n analyticsId: string;\n};\n\n/**\n * Returns default values for AnalyticsController state.\n *\n * Note: analyticsId is NOT included - it's an identity that must be\n * provided by the platform (generated once on first run, then persisted).\n *\n * @returns Default state without analyticsId\n */\nexport function getDefaultAnalyticsControllerState(): Omit<\n AnalyticsControllerState,\n 'analyticsId'\n> {\n return {\n optedIn: false,\n };\n}\n\n/**\n * The metadata for each property in {@link AnalyticsControllerState}.\n *\n * Note: `optedIn` is persisted by the controller (`persist: true`).\n * `analyticsId` is persisted by the platform (`persist: false`) and provided\n * via initial state. The platform should subscribe to `stateChange` events\n * to persist any state changes.\n */\nconst analyticsControllerMetadata = {\n optedIn: {\n includeInStateLogs: true,\n persist: true,\n includeInDebugSnapshot: true,\n usedInUi: true,\n },\n analyticsId: {\n includeInStateLogs: true,\n persist: false,\n includeInDebugSnapshot: true,\n usedInUi: false,\n },\n} satisfies StateMetadata<AnalyticsControllerState>;\n\n// === MESSENGER ===\n\nconst MESSENGER_EXPOSED_METHODS = [\n 'trackEvent',\n 'identify',\n 'trackView',\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 /**\n * Initial controller state. Must include a valid UUIDv4 `analyticsId`.\n * The platform is responsible for generating and persisting the analyticsId.\n */\n state: AnalyticsControllerState;\n /**\n * Messenger used to communicate with BaseController and other controllers.\n */\n messenger: AnalyticsControllerMessenger;\n /**\n * Platform adapter implementation for tracking events.\n */\n platformAdapter: AnalyticsPlatformAdapter;\n\n /**\n * Whether the anonymous events feature is enabled.\n *\n * @default false\n */\n isAnonymousEventsFeatureEnabled?: boolean;\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 *\n * Note: The controller persists `optedIn` internally. The `analyticsId` is persisted\n * by the platform and must be provided via initial state. The platform should subscribe\n * to `AnalyticsController:stateChange` events to persist any state changes.\n */\nexport class AnalyticsController extends BaseController<\n 'AnalyticsController',\n AnalyticsControllerState,\n AnalyticsControllerMessenger\n> {\n readonly #platformAdapter: AnalyticsPlatformAdapter;\n\n readonly #isAnonymousEventsFeatureEnabled: boolean;\n\n #initialized: boolean;\n\n /**\n * Constructs an AnalyticsController instance.\n *\n * @param options - Controller options\n * @param options.state - Initial controller state. Must include a valid UUIDv4 `analyticsId`.\n * Use `getDefaultAnalyticsControllerState()` for default opt-in preferences.\n * @param options.messenger - Messenger used to communicate with BaseController\n * @param options.platformAdapter - Platform adapter implementation for tracking\n * @param options.isAnonymousEventsFeatureEnabled - Whether the anonymous events feature is enabled\n * @throws Error if state.analyticsId is missing or not a valid UUIDv4\n * @remarks After construction, call {@link AnalyticsController.init} to complete initialization.\n */\n constructor({\n state,\n messenger,\n platformAdapter,\n isAnonymousEventsFeatureEnabled = false,\n }: AnalyticsControllerOptions) {\n const initialState: AnalyticsControllerState = {\n ...getDefaultAnalyticsControllerState(),\n ...state,\n };\n\n validateAnalyticsControllerState(initialState);\n\n super({\n name: controllerName,\n metadata: analyticsControllerMetadata,\n state: initialState,\n messenger,\n });\n\n this.#isAnonymousEventsFeatureEnabled = isAnonymousEventsFeatureEnabled;\n this.#platformAdapter = platformAdapter;\n this.#initialized = false;\n\n this.messenger.registerMethodActionHandlers(\n this,\n MESSENGER_EXPOSED_METHODS,\n );\n\n log('AnalyticsController initialized and ready', {\n enabled: analyticsControllerSelectors.selectEnabled(this.state),\n optedIn: this.state.optedIn,\n analyticsId: this.state.analyticsId,\n });\n }\n\n /**\n * Initialize the controller by calling the platform adapter's onSetupCompleted lifecycle hook.\n * This method must be called after construction to complete the setup process.\n */\n init(): void {\n if (this.#initialized) {\n log('AnalyticsController already initialized.');\n return;\n }\n\n this.#initialized = true;\n\n // Call onSetupCompleted lifecycle hook after initialization\n // State is already validated, so analyticsId is guaranteed to be a valid UUIDv4\n try {\n this.#platformAdapter.onSetupCompleted(this.state.analyticsId);\n } catch (error) {\n // Log error but don't throw - adapter setup failure shouldn't break controller\n log('Error calling platformAdapter.onSetupCompleted', error);\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 (!analyticsControllerSelectors.selectEnabled(this.state)) {\n return;\n }\n\n // if event does not have properties, send event without properties\n // and return to prevent any additional processing\n if (!event.hasProperties) {\n this.#platformAdapter.track(event.name);\n return;\n }\n\n // Track regular properties first if anonymous events feature is enabled\n if (this.#isAnonymousEventsFeatureEnabled) {\n // Note: Even if regular properties object is empty, we still send it to ensure\n // an event with user ID is tracked.\n this.#platformAdapter.track(event.name, {\n ...event.properties,\n });\n }\n\n const hasSensitiveProperties =\n Object.keys(event.sensitiveProperties).length > 0;\n\n if (!this.#isAnonymousEventsFeatureEnabled || hasSensitiveProperties) {\n this.#platformAdapter.track(event.name, {\n ...event.properties,\n ...event.sensitiveProperties,\n ...(hasSensitiveProperties && { anonymous: true }),\n });\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 (!analyticsControllerSelectors.selectEnabled(this.state)) {\n return;\n }\n\n // Delegate to platform adapter using the current analytics ID\n this.#platformAdapter.identify(this.state.analyticsId, traits);\n }\n\n /**\n * Track a page or screen view.\n *\n * @param name - The identifier/name of the page or screen being viewed (e.g., \"home\", \"settings\", \"wallet\")\n * @param properties - Optional properties associated with the view\n */\n trackView(name: string, properties?: AnalyticsEventProperties): void {\n if (!analyticsControllerSelectors.selectEnabled(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 */\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"]}
|
|
@@ -2,7 +2,7 @@ import type { ControllerGetStateAction, ControllerStateChangeEvent } from "@meta
|
|
|
2
2
|
import { BaseController } from "@metamask/base-controller";
|
|
3
3
|
import type { Messenger } from "@metamask/messenger";
|
|
4
4
|
import type { AnalyticsControllerMethodActions } from "./AnalyticsController-method-action-types.cjs";
|
|
5
|
-
import type { AnalyticsPlatformAdapter, AnalyticsEventProperties } from "./AnalyticsPlatformAdapter.types.cjs";
|
|
5
|
+
import type { AnalyticsPlatformAdapter, AnalyticsEventProperties, AnalyticsUserTraits, AnalyticsTrackingEvent } from "./AnalyticsPlatformAdapter.types.cjs";
|
|
6
6
|
/**
|
|
7
7
|
* The name of the {@link AnalyticsController}, used to namespace the
|
|
8
8
|
* controller's actions and events and to namespace the controller's state data
|
|
@@ -14,27 +14,25 @@ export declare const controllerName = "AnalyticsController";
|
|
|
14
14
|
*/
|
|
15
15
|
export type AnalyticsControllerState = {
|
|
16
16
|
/**
|
|
17
|
-
* Whether
|
|
18
|
-
*/
|
|
19
|
-
enabled: boolean;
|
|
20
|
-
/**
|
|
21
|
-
* Whether the user has opted in to analytics
|
|
17
|
+
* Whether the user has opted in to analytics.
|
|
22
18
|
*/
|
|
23
19
|
optedIn: boolean;
|
|
24
20
|
/**
|
|
25
|
-
* User's UUIDv4 analytics identifier
|
|
21
|
+
* User's UUIDv4 analytics identifier.
|
|
22
|
+
* This is an identity (unique per user), not a preference.
|
|
23
|
+
* Must be provided by the platform - the controller does not generate it.
|
|
26
24
|
*/
|
|
27
25
|
analyticsId: string;
|
|
28
26
|
};
|
|
29
27
|
/**
|
|
30
|
-
*
|
|
31
|
-
*
|
|
32
|
-
*
|
|
33
|
-
*
|
|
28
|
+
* Returns default values for AnalyticsController state.
|
|
29
|
+
*
|
|
30
|
+
* Note: analyticsId is NOT included - it's an identity that must be
|
|
31
|
+
* provided by the platform (generated once on first run, then persisted).
|
|
34
32
|
*
|
|
35
|
-
* @returns
|
|
33
|
+
* @returns Default state without analyticsId
|
|
36
34
|
*/
|
|
37
|
-
export declare function getDefaultAnalyticsControllerState(): AnalyticsControllerState
|
|
35
|
+
export declare function getDefaultAnalyticsControllerState(): Omit<AnalyticsControllerState, 'analyticsId'>;
|
|
38
36
|
/**
|
|
39
37
|
* Returns the state of the {@link AnalyticsController}.
|
|
40
38
|
*/
|
|
@@ -68,12 +66,25 @@ export type AnalyticsControllerMessenger = Messenger<typeof controllerName, Anal
|
|
|
68
66
|
* The options that AnalyticsController takes.
|
|
69
67
|
*/
|
|
70
68
|
export type AnalyticsControllerOptions = {
|
|
71
|
-
|
|
69
|
+
/**
|
|
70
|
+
* Initial controller state. Must include a valid UUIDv4 `analyticsId`.
|
|
71
|
+
* The platform is responsible for generating and persisting the analyticsId.
|
|
72
|
+
*/
|
|
73
|
+
state: AnalyticsControllerState;
|
|
74
|
+
/**
|
|
75
|
+
* Messenger used to communicate with BaseController and other controllers.
|
|
76
|
+
*/
|
|
72
77
|
messenger: AnalyticsControllerMessenger;
|
|
73
78
|
/**
|
|
74
|
-
* Platform adapter implementation for tracking events
|
|
79
|
+
* Platform adapter implementation for tracking events.
|
|
75
80
|
*/
|
|
76
81
|
platformAdapter: AnalyticsPlatformAdapter;
|
|
82
|
+
/**
|
|
83
|
+
* Whether the anonymous events feature is enabled.
|
|
84
|
+
*
|
|
85
|
+
* @default false
|
|
86
|
+
*/
|
|
87
|
+
isAnonymousEventsFeatureEnabled?: boolean;
|
|
77
88
|
};
|
|
78
89
|
/**
|
|
79
90
|
* The AnalyticsController manages analytics tracking across platforms (Mobile/Extension).
|
|
@@ -84,6 +95,10 @@ export type AnalyticsControllerOptions = {
|
|
|
84
95
|
* This controller follows the MetaMask controller pattern and integrates with the
|
|
85
96
|
* messenger system to allow other controllers and components to track analytics events.
|
|
86
97
|
* It delegates platform-specific implementation to an {@link AnalyticsPlatformAdapter}.
|
|
98
|
+
*
|
|
99
|
+
* Note: The controller persists `optedIn` internally. The `analyticsId` is persisted
|
|
100
|
+
* by the platform and must be provided via initial state. The platform should subscribe
|
|
101
|
+
* to `AnalyticsController:stateChange` events to persist any state changes.
|
|
87
102
|
*/
|
|
88
103
|
export declare class AnalyticsController extends BaseController<'AnalyticsController', AnalyticsControllerState, AnalyticsControllerMessenger> {
|
|
89
104
|
#private;
|
|
@@ -91,42 +106,41 @@ export declare class AnalyticsController extends BaseController<'AnalyticsContro
|
|
|
91
106
|
* Constructs an AnalyticsController instance.
|
|
92
107
|
*
|
|
93
108
|
* @param options - Controller options
|
|
94
|
-
* @param options.state - Initial controller state
|
|
109
|
+
* @param options.state - Initial controller state. Must include a valid UUIDv4 `analyticsId`.
|
|
110
|
+
* Use `getDefaultAnalyticsControllerState()` for default opt-in preferences.
|
|
95
111
|
* @param options.messenger - Messenger used to communicate with BaseController
|
|
96
112
|
* @param options.platformAdapter - Platform adapter implementation for tracking
|
|
113
|
+
* @param options.isAnonymousEventsFeatureEnabled - Whether the anonymous events feature is enabled
|
|
114
|
+
* @throws Error if state.analyticsId is missing or not a valid UUIDv4
|
|
115
|
+
* @remarks After construction, call {@link AnalyticsController.init} to complete initialization.
|
|
116
|
+
*/
|
|
117
|
+
constructor({ state, messenger, platformAdapter, isAnonymousEventsFeatureEnabled, }: AnalyticsControllerOptions);
|
|
118
|
+
/**
|
|
119
|
+
* Initialize the controller by calling the platform adapter's onSetupCompleted lifecycle hook.
|
|
120
|
+
* This method must be called after construction to complete the setup process.
|
|
97
121
|
*/
|
|
98
|
-
|
|
122
|
+
init(): void;
|
|
99
123
|
/**
|
|
100
124
|
* Track an analytics event.
|
|
101
125
|
*
|
|
102
126
|
* Events are only tracked if analytics is enabled.
|
|
103
127
|
*
|
|
104
|
-
* @param
|
|
105
|
-
* @param properties - Event properties
|
|
128
|
+
* @param event - Analytics event with properties and sensitive properties
|
|
106
129
|
*/
|
|
107
|
-
trackEvent(
|
|
130
|
+
trackEvent(event: AnalyticsTrackingEvent): void;
|
|
108
131
|
/**
|
|
109
132
|
* Identify a user for analytics.
|
|
110
133
|
*
|
|
111
|
-
* @param userId - The user identifier (e.g., metametrics ID)
|
|
112
134
|
* @param traits - User traits/properties
|
|
113
135
|
*/
|
|
114
|
-
identify(
|
|
136
|
+
identify(traits?: AnalyticsUserTraits): void;
|
|
115
137
|
/**
|
|
116
|
-
* Track a page view.
|
|
138
|
+
* Track a page or screen view.
|
|
117
139
|
*
|
|
118
|
-
* @param
|
|
119
|
-
* @param properties -
|
|
120
|
-
*/
|
|
121
|
-
trackPage(pageName: string, properties?: AnalyticsEventProperties): void;
|
|
122
|
-
/**
|
|
123
|
-
* Enable analytics tracking.
|
|
124
|
-
*/
|
|
125
|
-
enable(): void;
|
|
126
|
-
/**
|
|
127
|
-
* Disable analytics tracking.
|
|
140
|
+
* @param name - The identifier/name of the page or screen being viewed (e.g., "home", "settings", "wallet")
|
|
141
|
+
* @param properties - Optional properties associated with the view
|
|
128
142
|
*/
|
|
129
|
-
|
|
143
|
+
trackView(name: string, properties?: AnalyticsEventProperties): void;
|
|
130
144
|
/**
|
|
131
145
|
* Opt in to analytics.
|
|
132
146
|
*/
|
|
@@ -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;AAErD,OAAO,KAAK,EAAE,gCAAgC,EAAE,sDAAkD;AAGlG,OAAO,KAAK,EACV,wBAAwB,EACxB,wBAAwB,EACxB,mBAAmB,EACnB,sBAAsB,EACvB,6CAAyC;AAK1C;;;;GAIG;AACH,eAAO,MAAM,cAAc,wBAAwB,CAAC;AAIpD;;GAEG;AACH,MAAM,MAAM,wBAAwB,GAAG;IACrC;;OAEG;IACH,OAAO,EAAE,OAAO,CAAC;IAEjB;;;;OAIG;IACH,WAAW,EAAE,MAAM,CAAC;CACrB,CAAC;AAEF;;;;;;;GAOG;AACH,wBAAgB,kCAAkC,IAAI,IAAI,CACxD,wBAAwB,EACxB,aAAa,CACd,CAIA;AAmCD;;GAEG;AACH,MAAM,MAAM,iCAAiC,GAAG,wBAAwB,CACtE,OAAO,cAAc,EACrB,wBAAwB,CACzB,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,0BAA0B,GAClC,iCAAiC,GACjC,gC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-2,7 +2,7 @@ import type { ControllerGetStateAction, ControllerStateChangeEvent } from "@meta
|
|
|
2
2
|
import { BaseController } from "@metamask/base-controller";
|
|
3
3
|
import type { Messenger } from "@metamask/messenger";
|
|
4
4
|
import type { AnalyticsControllerMethodActions } from "./AnalyticsController-method-action-types.mjs";
|
|
5
|
-
import type { AnalyticsPlatformAdapter, AnalyticsEventProperties } from "./AnalyticsPlatformAdapter.types.mjs";
|
|
5
|
+
import type { AnalyticsPlatformAdapter, AnalyticsEventProperties, AnalyticsUserTraits, AnalyticsTrackingEvent } from "./AnalyticsPlatformAdapter.types.mjs";
|
|
6
6
|
/**
|
|
7
7
|
* The name of the {@link AnalyticsController}, used to namespace the
|
|
8
8
|
* controller's actions and events and to namespace the controller's state data
|
|
@@ -14,27 +14,25 @@ export declare const controllerName = "AnalyticsController";
|
|
|
14
14
|
*/
|
|
15
15
|
export type AnalyticsControllerState = {
|
|
16
16
|
/**
|
|
17
|
-
* Whether
|
|
18
|
-
*/
|
|
19
|
-
enabled: boolean;
|
|
20
|
-
/**
|
|
21
|
-
* Whether the user has opted in to analytics
|
|
17
|
+
* Whether the user has opted in to analytics.
|
|
22
18
|
*/
|
|
23
19
|
optedIn: boolean;
|
|
24
20
|
/**
|
|
25
|
-
* User's UUIDv4 analytics identifier
|
|
21
|
+
* User's UUIDv4 analytics identifier.
|
|
22
|
+
* This is an identity (unique per user), not a preference.
|
|
23
|
+
* Must be provided by the platform - the controller does not generate it.
|
|
26
24
|
*/
|
|
27
25
|
analyticsId: string;
|
|
28
26
|
};
|
|
29
27
|
/**
|
|
30
|
-
*
|
|
31
|
-
*
|
|
32
|
-
*
|
|
33
|
-
*
|
|
28
|
+
* Returns default values for AnalyticsController state.
|
|
29
|
+
*
|
|
30
|
+
* Note: analyticsId is NOT included - it's an identity that must be
|
|
31
|
+
* provided by the platform (generated once on first run, then persisted).
|
|
34
32
|
*
|
|
35
|
-
* @returns
|
|
33
|
+
* @returns Default state without analyticsId
|
|
36
34
|
*/
|
|
37
|
-
export declare function getDefaultAnalyticsControllerState(): AnalyticsControllerState
|
|
35
|
+
export declare function getDefaultAnalyticsControllerState(): Omit<AnalyticsControllerState, 'analyticsId'>;
|
|
38
36
|
/**
|
|
39
37
|
* Returns the state of the {@link AnalyticsController}.
|
|
40
38
|
*/
|
|
@@ -68,12 +66,25 @@ export type AnalyticsControllerMessenger = Messenger<typeof controllerName, Anal
|
|
|
68
66
|
* The options that AnalyticsController takes.
|
|
69
67
|
*/
|
|
70
68
|
export type AnalyticsControllerOptions = {
|
|
71
|
-
|
|
69
|
+
/**
|
|
70
|
+
* Initial controller state. Must include a valid UUIDv4 `analyticsId`.
|
|
71
|
+
* The platform is responsible for generating and persisting the analyticsId.
|
|
72
|
+
*/
|
|
73
|
+
state: AnalyticsControllerState;
|
|
74
|
+
/**
|
|
75
|
+
* Messenger used to communicate with BaseController and other controllers.
|
|
76
|
+
*/
|
|
72
77
|
messenger: AnalyticsControllerMessenger;
|
|
73
78
|
/**
|
|
74
|
-
* Platform adapter implementation for tracking events
|
|
79
|
+
* Platform adapter implementation for tracking events.
|
|
75
80
|
*/
|
|
76
81
|
platformAdapter: AnalyticsPlatformAdapter;
|
|
82
|
+
/**
|
|
83
|
+
* Whether the anonymous events feature is enabled.
|
|
84
|
+
*
|
|
85
|
+
* @default false
|
|
86
|
+
*/
|
|
87
|
+
isAnonymousEventsFeatureEnabled?: boolean;
|
|
77
88
|
};
|
|
78
89
|
/**
|
|
79
90
|
* The AnalyticsController manages analytics tracking across platforms (Mobile/Extension).
|
|
@@ -84,6 +95,10 @@ export type AnalyticsControllerOptions = {
|
|
|
84
95
|
* This controller follows the MetaMask controller pattern and integrates with the
|
|
85
96
|
* messenger system to allow other controllers and components to track analytics events.
|
|
86
97
|
* It delegates platform-specific implementation to an {@link AnalyticsPlatformAdapter}.
|
|
98
|
+
*
|
|
99
|
+
* Note: The controller persists `optedIn` internally. The `analyticsId` is persisted
|
|
100
|
+
* by the platform and must be provided via initial state. The platform should subscribe
|
|
101
|
+
* to `AnalyticsController:stateChange` events to persist any state changes.
|
|
87
102
|
*/
|
|
88
103
|
export declare class AnalyticsController extends BaseController<'AnalyticsController', AnalyticsControllerState, AnalyticsControllerMessenger> {
|
|
89
104
|
#private;
|
|
@@ -91,42 +106,41 @@ export declare class AnalyticsController extends BaseController<'AnalyticsContro
|
|
|
91
106
|
* Constructs an AnalyticsController instance.
|
|
92
107
|
*
|
|
93
108
|
* @param options - Controller options
|
|
94
|
-
* @param options.state - Initial controller state
|
|
109
|
+
* @param options.state - Initial controller state. Must include a valid UUIDv4 `analyticsId`.
|
|
110
|
+
* Use `getDefaultAnalyticsControllerState()` for default opt-in preferences.
|
|
95
111
|
* @param options.messenger - Messenger used to communicate with BaseController
|
|
96
112
|
* @param options.platformAdapter - Platform adapter implementation for tracking
|
|
113
|
+
* @param options.isAnonymousEventsFeatureEnabled - Whether the anonymous events feature is enabled
|
|
114
|
+
* @throws Error if state.analyticsId is missing or not a valid UUIDv4
|
|
115
|
+
* @remarks After construction, call {@link AnalyticsController.init} to complete initialization.
|
|
116
|
+
*/
|
|
117
|
+
constructor({ state, messenger, platformAdapter, isAnonymousEventsFeatureEnabled, }: AnalyticsControllerOptions);
|
|
118
|
+
/**
|
|
119
|
+
* Initialize the controller by calling the platform adapter's onSetupCompleted lifecycle hook.
|
|
120
|
+
* This method must be called after construction to complete the setup process.
|
|
97
121
|
*/
|
|
98
|
-
|
|
122
|
+
init(): void;
|
|
99
123
|
/**
|
|
100
124
|
* Track an analytics event.
|
|
101
125
|
*
|
|
102
126
|
* Events are only tracked if analytics is enabled.
|
|
103
127
|
*
|
|
104
|
-
* @param
|
|
105
|
-
* @param properties - Event properties
|
|
128
|
+
* @param event - Analytics event with properties and sensitive properties
|
|
106
129
|
*/
|
|
107
|
-
trackEvent(
|
|
130
|
+
trackEvent(event: AnalyticsTrackingEvent): void;
|
|
108
131
|
/**
|
|
109
132
|
* Identify a user for analytics.
|
|
110
133
|
*
|
|
111
|
-
* @param userId - The user identifier (e.g., metametrics ID)
|
|
112
134
|
* @param traits - User traits/properties
|
|
113
135
|
*/
|
|
114
|
-
identify(
|
|
136
|
+
identify(traits?: AnalyticsUserTraits): void;
|
|
115
137
|
/**
|
|
116
|
-
* Track a page view.
|
|
138
|
+
* Track a page or screen view.
|
|
117
139
|
*
|
|
118
|
-
* @param
|
|
119
|
-
* @param properties -
|
|
120
|
-
*/
|
|
121
|
-
trackPage(pageName: string, properties?: AnalyticsEventProperties): void;
|
|
122
|
-
/**
|
|
123
|
-
* Enable analytics tracking.
|
|
124
|
-
*/
|
|
125
|
-
enable(): void;
|
|
126
|
-
/**
|
|
127
|
-
* Disable analytics tracking.
|
|
140
|
+
* @param name - The identifier/name of the page or screen being viewed (e.g., "home", "settings", "wallet")
|
|
141
|
+
* @param properties - Optional properties associated with the view
|
|
128
142
|
*/
|
|
129
|
-
|
|
143
|
+
trackView(name: string, properties?: AnalyticsEventProperties): void;
|
|
130
144
|
/**
|
|
131
145
|
* Opt in to analytics.
|
|
132
146
|
*/
|