@metamask-previews/analytics-controller 0.0.0-preview-152da47f → 0.0.0-preview-21a5ddac
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 +72 -11
- package/dist/AnalyticsController-method-action-types.cjs.map +1 -1
- package/dist/AnalyticsController-method-action-types.d.cts +28 -26
- package/dist/AnalyticsController-method-action-types.d.cts.map +1 -1
- package/dist/AnalyticsController-method-action-types.d.mts +28 -26
- 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 +81 -52
- package/dist/AnalyticsController.cjs.map +1 -1
- package/dist/AnalyticsController.d.cts +27 -24
- package/dist/AnalyticsController.d.cts.map +1 -1
- package/dist/AnalyticsController.d.mts +27 -24
- package/dist/AnalyticsController.d.mts.map +1 -1
- package/dist/AnalyticsController.mjs +81 -52
- package/dist/AnalyticsController.mjs.map +1 -1
- package/dist/AnalyticsPlatformAdapter.types.cjs.map +1 -1
- package/dist/AnalyticsPlatformAdapter.types.d.cts +58 -15
- package/dist/AnalyticsPlatformAdapter.types.d.cts.map +1 -1
- package/dist/AnalyticsPlatformAdapter.types.d.mts +58 -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/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/analyticsStateValidator.cjs +19 -0
- package/dist/analyticsStateValidator.cjs.map +1 -0
- package/dist/analyticsStateValidator.d.cts +9 -0
- package/dist/analyticsStateValidator.d.cts.map +1 -0
- package/dist/analyticsStateValidator.d.mts +9 -0
- package/dist/analyticsStateValidator.d.mts.map +1 -0
- package/dist/analyticsStateValidator.mjs +15 -0
- package/dist/analyticsStateValidator.mjs.map +1 -0
- package/dist/index.cjs +7 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +4 -2
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +4 -2
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +4 -0
- package/dist/index.mjs.map +1 -1
- package/dist/selectors.cjs +44 -0
- package/dist/selectors.cjs.map +1 -0
- package/dist/selectors.d.cts +12 -0
- package/dist/selectors.d.cts.map +1 -0
- package/dist/selectors.d.mts +12 -0
- package/dist/selectors.d.mts.map +1 -0
- package/dist/selectors.mjs +41 -0
- package/dist/selectors.mjs.map +1 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -30,15 +30,24 @@ The controller delegates platform-specific analytics implementation to a `Analyt
|
|
|
30
30
|
import type { AnalyticsPlatformAdapter } from '@metamask/analytics-controller';
|
|
31
31
|
|
|
32
32
|
const platformAdapter: AnalyticsPlatformAdapter = {
|
|
33
|
-
|
|
33
|
+
track: (eventName: string, properties: Record<string, unknown>) => {
|
|
34
34
|
// Platform-specific implementation (e.g., Segment, Mixpanel, etc.)
|
|
35
35
|
segment.track(eventName, properties);
|
|
36
36
|
},
|
|
37
37
|
identify: (userId: string, traits?: Record<string, unknown>) => {
|
|
38
38
|
segment.identify(userId, traits);
|
|
39
39
|
},
|
|
40
|
-
|
|
41
|
-
segment.page(
|
|
40
|
+
view: (name: string, properties?: Record<string, unknown>) => {
|
|
41
|
+
segment.page(name, properties);
|
|
42
|
+
},
|
|
43
|
+
onSetupCompleted: (analyticsId: string) => {
|
|
44
|
+
// Lifecycle hook called after controller initialization
|
|
45
|
+
// The analyticsId is guaranteed to be set when this method is called
|
|
46
|
+
// Use this for platform-specific setup that requires the analytics ID
|
|
47
|
+
// For example, adding plugins that need the analytics ID:
|
|
48
|
+
segment.add({
|
|
49
|
+
plugin: new PrivacyPlugin(analyticsId),
|
|
50
|
+
});
|
|
42
51
|
},
|
|
43
52
|
};
|
|
44
53
|
```
|
|
@@ -112,7 +121,7 @@ console.log(controller.state.analyticsId); // '550e8400-e29b-41d4-a716-446655440
|
|
|
112
121
|
### 5. Track Page Views
|
|
113
122
|
|
|
114
123
|
```typescript
|
|
115
|
-
controller.
|
|
124
|
+
controller.trackView('home', {
|
|
116
125
|
referrer: 'google',
|
|
117
126
|
campaign: 'summer-2024',
|
|
118
127
|
});
|
|
@@ -125,9 +134,13 @@ controller.trackPage('home', {
|
|
|
125
134
|
controller.enable();
|
|
126
135
|
controller.disable();
|
|
127
136
|
|
|
128
|
-
// Opt in/out
|
|
129
|
-
controller.
|
|
130
|
-
controller.
|
|
137
|
+
// Opt in/out for regular account
|
|
138
|
+
controller.optInForRegularAccount();
|
|
139
|
+
controller.optOutForRegularAccount();
|
|
140
|
+
|
|
141
|
+
// Opt in/out for social account
|
|
142
|
+
controller.optInForSocialAccount();
|
|
143
|
+
controller.optOutForSocialAccount();
|
|
131
144
|
```
|
|
132
145
|
|
|
133
146
|
### 7. Use Messenger Actions
|
|
@@ -148,10 +161,10 @@ messenger.call(
|
|
|
148
161
|
},
|
|
149
162
|
);
|
|
150
163
|
|
|
151
|
-
messenger.call('AnalyticsController:
|
|
152
|
-
messenger.call('AnalyticsController:
|
|
153
|
-
messenger.call('AnalyticsController:
|
|
154
|
-
messenger.call('AnalyticsController:
|
|
164
|
+
messenger.call('AnalyticsController:optInForRegularAccount');
|
|
165
|
+
messenger.call('AnalyticsController:optOutForRegularAccount');
|
|
166
|
+
messenger.call('AnalyticsController:optInForSocialAccount');
|
|
167
|
+
messenger.call('AnalyticsController:optOutForSocialAccount');
|
|
155
168
|
```
|
|
156
169
|
|
|
157
170
|
### 8. Subscribe to State Changes
|
|
@@ -193,6 +206,54 @@ const defaultState = getDefaultAnalyticsControllerState();
|
|
|
193
206
|
|
|
194
207
|
**Analytics ID:** The `analyticsId` is a UUIDv4 string. If not provided in the `state` parameter, the controller automatically generates one on initialization. This ID is persisted in state and remains consistent across restarts. If you provide an `analyticsId` in the `state` parameter, it will be used instead (useful for migrations).
|
|
195
208
|
|
|
209
|
+
## Lifecycle Hooks
|
|
210
|
+
|
|
211
|
+
### `onSetupCompleted`
|
|
212
|
+
|
|
213
|
+
The `onSetupCompleted` lifecycle hook is called once after the `AnalyticsController` is fully initialized. This hook allows platform-specific adapters to perform setup that requires access to the controller's state (e.g., `analyticsId`).
|
|
214
|
+
|
|
215
|
+
**When it's called:**
|
|
216
|
+
|
|
217
|
+
- After the controller is fully initialized
|
|
218
|
+
- Only when `analyticsId` is set in controller state (the presence of `analyticsId` is the definition of "completed" setup)
|
|
219
|
+
- The `analyticsId` parameter is guaranteed to be set and be a valid UUIDv4 when this method is called
|
|
220
|
+
|
|
221
|
+
**What it's used for:**
|
|
222
|
+
|
|
223
|
+
Platform-specific setup that requires access to adapter implementation like adding plugins or middleware that need the `analyticsId`
|
|
224
|
+
Any initialization that depends on the controller being ready
|
|
225
|
+
|
|
226
|
+
**Example usage:**
|
|
227
|
+
|
|
228
|
+
```typescript
|
|
229
|
+
const platformAdapter: AnalyticsPlatformAdapter = {
|
|
230
|
+
// ... other methods ...
|
|
231
|
+
onSetupCompleted: (analyticsId: string) => {
|
|
232
|
+
// Add platform-specific plugins that require analyticsId
|
|
233
|
+
client.add({
|
|
234
|
+
plugin: new PrivacyPlugin(analyticsId),
|
|
235
|
+
});
|
|
236
|
+
},
|
|
237
|
+
};
|
|
238
|
+
```
|
|
239
|
+
|
|
240
|
+
**Error handling:**
|
|
241
|
+
|
|
242
|
+
- Errors thrown in `onSetupCompleted` are caught and logged
|
|
243
|
+
|
|
244
|
+
**Best practices:**
|
|
245
|
+
|
|
246
|
+
- Use `onSetupCompleted` for setup that requires controller state
|
|
247
|
+
- Keep setup logic minimal and focused
|
|
248
|
+
- Handle errors gracefully within the hook
|
|
249
|
+
- If you don't need setup, provide a no-op implementation:
|
|
250
|
+
|
|
251
|
+
```typescript
|
|
252
|
+
onSetupCompleted: (_analyticsId: string) => {
|
|
253
|
+
// No-op: this adapter doesn't need setup
|
|
254
|
+
},
|
|
255
|
+
```
|
|
256
|
+
|
|
196
257
|
## Debugging
|
|
197
258
|
|
|
198
259
|
To display analytics-controller logs in the mobile app, you can add the following to your `.js.env` file:
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AnalyticsController-method-action-types.cjs","sourceRoot":"","sources":["../src/AnalyticsController-method-action-types.ts"],"names":[],"mappings":";AAAA;;;GAGG","sourcesContent":["/**\n * This file is auto generated by `scripts/generate-method-action-types.ts`.\n * Do not edit manually.\n */\n\nimport type { AnalyticsController } from './AnalyticsController';\n\n/**\n * Track an analytics event.\n *\n * Events are only tracked if analytics is enabled.\n *\n * @param
|
|
1
|
+
{"version":3,"file":"AnalyticsController-method-action-types.cjs","sourceRoot":"","sources":["../src/AnalyticsController-method-action-types.ts"],"names":[],"mappings":";AAAA;;;GAGG","sourcesContent":["/**\n * This file is auto generated by `scripts/generate-method-action-types.ts`.\n * Do not edit manually.\n */\n\nimport type { AnalyticsController } from './AnalyticsController';\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 */\nexport type AnalyticsControllerTrackEventAction = {\n type: `AnalyticsController:trackEvent`;\n handler: AnalyticsController['trackEvent'];\n};\n\n/**\n * Identify a user for analytics.\n *\n * @param traits - User traits/properties\n */\nexport type AnalyticsControllerIdentifyAction = {\n type: `AnalyticsController:identify`;\n handler: AnalyticsController['identify'];\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 */\nexport type AnalyticsControllerTrackViewAction = {\n type: `AnalyticsController:trackView`;\n handler: AnalyticsController['trackView'];\n};\n\n/**\n * Opt in to analytics for regular account.\n * This updates the user's opt-in status for regular account.\n */\nexport type AnalyticsControllerOptInForRegularAccountAction = {\n type: `AnalyticsController:optInForRegularAccount`;\n handler: AnalyticsController['optInForRegularAccount'];\n};\n\n/**\n * Opt out of analytics for regular account.\n * This updates the user's opt-in status for regular account.\n */\nexport type AnalyticsControllerOptOutForRegularAccountAction = {\n type: `AnalyticsController:optOutForRegularAccount`;\n handler: AnalyticsController['optOutForRegularAccount'];\n};\n\n/**\n * Opt in to analytics for social account.\n * This updates the user's opt-in status for social account.\n */\nexport type AnalyticsControllerOptInForSocialAccountAction = {\n type: `AnalyticsController:optInForSocialAccount`;\n handler: AnalyticsController['optInForSocialAccount'];\n};\n\n/**\n * Opt out of analytics for social account.\n * This updates the user's opt-in status for social account.\n */\nexport type AnalyticsControllerOptOutForSocialAccountAction = {\n type: `AnalyticsController:optOutForSocialAccount`;\n handler: AnalyticsController['optOutForSocialAccount'];\n};\n\n/**\n * Union of all AnalyticsController action types.\n */\nexport type AnalyticsControllerMethodActions =\n | AnalyticsControllerTrackEventAction\n | AnalyticsControllerIdentifyAction\n | AnalyticsControllerTrackViewAction\n | AnalyticsControllerOptInForRegularAccountAction\n | AnalyticsControllerOptOutForRegularAccountAction\n | AnalyticsControllerOptInForSocialAccountAction\n | AnalyticsControllerOptOutForSocialAccountAction;\n"]}
|
|
@@ -8,8 +8,7 @@ import type { AnalyticsController } from "./AnalyticsController.cjs";
|
|
|
8
8
|
*
|
|
9
9
|
* Events are only tracked if analytics is enabled.
|
|
10
10
|
*
|
|
11
|
-
* @param
|
|
12
|
-
* @param properties - Event properties
|
|
11
|
+
* @param event - Analytics event with properties and sensitive properties
|
|
13
12
|
*/
|
|
14
13
|
export type AnalyticsControllerTrackEventAction = {
|
|
15
14
|
type: `AnalyticsController:trackEvent`;
|
|
@@ -18,7 +17,6 @@ export type AnalyticsControllerTrackEventAction = {
|
|
|
18
17
|
/**
|
|
19
18
|
* Identify a user for analytics.
|
|
20
19
|
*
|
|
21
|
-
* @param userId - The user identifier (e.g., metametrics ID)
|
|
22
20
|
* @param traits - User traits/properties
|
|
23
21
|
*/
|
|
24
22
|
export type AnalyticsControllerIdentifyAction = {
|
|
@@ -26,45 +24,49 @@ export type AnalyticsControllerIdentifyAction = {
|
|
|
26
24
|
handler: AnalyticsController['identify'];
|
|
27
25
|
};
|
|
28
26
|
/**
|
|
29
|
-
* Track a page view.
|
|
27
|
+
* Track a page or screen view.
|
|
30
28
|
*
|
|
31
|
-
* @param
|
|
32
|
-
* @param properties -
|
|
29
|
+
* @param name - The identifier/name of the page or screen being viewed (e.g., "home", "settings", "wallet")
|
|
30
|
+
* @param properties - Optional properties associated with the view
|
|
33
31
|
*/
|
|
34
|
-
export type
|
|
35
|
-
type: `AnalyticsController:
|
|
36
|
-
handler: AnalyticsController['
|
|
32
|
+
export type AnalyticsControllerTrackViewAction = {
|
|
33
|
+
type: `AnalyticsController:trackView`;
|
|
34
|
+
handler: AnalyticsController['trackView'];
|
|
37
35
|
};
|
|
38
36
|
/**
|
|
39
|
-
*
|
|
37
|
+
* Opt in to analytics for regular account.
|
|
38
|
+
* This updates the user's opt-in status for regular account.
|
|
40
39
|
*/
|
|
41
|
-
export type
|
|
42
|
-
type: `AnalyticsController:
|
|
43
|
-
handler: AnalyticsController['
|
|
40
|
+
export type AnalyticsControllerOptInForRegularAccountAction = {
|
|
41
|
+
type: `AnalyticsController:optInForRegularAccount`;
|
|
42
|
+
handler: AnalyticsController['optInForRegularAccount'];
|
|
44
43
|
};
|
|
45
44
|
/**
|
|
46
|
-
*
|
|
45
|
+
* Opt out of analytics for regular account.
|
|
46
|
+
* This updates the user's opt-in status for regular account.
|
|
47
47
|
*/
|
|
48
|
-
export type
|
|
49
|
-
type: `AnalyticsController:
|
|
50
|
-
handler: AnalyticsController['
|
|
48
|
+
export type AnalyticsControllerOptOutForRegularAccountAction = {
|
|
49
|
+
type: `AnalyticsController:optOutForRegularAccount`;
|
|
50
|
+
handler: AnalyticsController['optOutForRegularAccount'];
|
|
51
51
|
};
|
|
52
52
|
/**
|
|
53
|
-
* Opt in to analytics.
|
|
53
|
+
* Opt in to analytics for social account.
|
|
54
|
+
* This updates the user's opt-in status for social account.
|
|
54
55
|
*/
|
|
55
|
-
export type
|
|
56
|
-
type: `AnalyticsController:
|
|
57
|
-
handler: AnalyticsController['
|
|
56
|
+
export type AnalyticsControllerOptInForSocialAccountAction = {
|
|
57
|
+
type: `AnalyticsController:optInForSocialAccount`;
|
|
58
|
+
handler: AnalyticsController['optInForSocialAccount'];
|
|
58
59
|
};
|
|
59
60
|
/**
|
|
60
|
-
* Opt out of analytics.
|
|
61
|
+
* Opt out of analytics for social account.
|
|
62
|
+
* This updates the user's opt-in status for social account.
|
|
61
63
|
*/
|
|
62
|
-
export type
|
|
63
|
-
type: `AnalyticsController:
|
|
64
|
-
handler: AnalyticsController['
|
|
64
|
+
export type AnalyticsControllerOptOutForSocialAccountAction = {
|
|
65
|
+
type: `AnalyticsController:optOutForSocialAccount`;
|
|
66
|
+
handler: AnalyticsController['optOutForSocialAccount'];
|
|
65
67
|
};
|
|
66
68
|
/**
|
|
67
69
|
* Union of all AnalyticsController action types.
|
|
68
70
|
*/
|
|
69
|
-
export type AnalyticsControllerMethodActions = AnalyticsControllerTrackEventAction | AnalyticsControllerIdentifyAction |
|
|
71
|
+
export type AnalyticsControllerMethodActions = AnalyticsControllerTrackEventAction | AnalyticsControllerIdentifyAction | AnalyticsControllerTrackViewAction | AnalyticsControllerOptInForRegularAccountAction | AnalyticsControllerOptOutForRegularAccountAction | AnalyticsControllerOptInForSocialAccountAction | AnalyticsControllerOptOutForSocialAccountAction;
|
|
70
72
|
//# sourceMappingURL=AnalyticsController-method-action-types.d.cts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AnalyticsController-method-action-types.d.cts","sourceRoot":"","sources":["../src/AnalyticsController-method-action-types.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EAAE,mBAAmB,EAAE,kCAA8B;AAEjE
|
|
1
|
+
{"version":3,"file":"AnalyticsController-method-action-types.d.cts","sourceRoot":"","sources":["../src/AnalyticsController-method-action-types.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EAAE,mBAAmB,EAAE,kCAA8B;AAEjE;;;;;;GAMG;AACH,MAAM,MAAM,mCAAmC,GAAG;IAChD,IAAI,EAAE,gCAAgC,CAAC;IACvC,OAAO,EAAE,mBAAmB,CAAC,YAAY,CAAC,CAAC;CAC5C,CAAC;AAEF;;;;GAIG;AACH,MAAM,MAAM,iCAAiC,GAAG;IAC9C,IAAI,EAAE,8BAA8B,CAAC;IACrC,OAAO,EAAE,mBAAmB,CAAC,UAAU,CAAC,CAAC;CAC1C,CAAC;AAEF;;;;;GAKG;AACH,MAAM,MAAM,kCAAkC,GAAG;IAC/C,IAAI,EAAE,+BAA+B,CAAC;IACtC,OAAO,EAAE,mBAAmB,CAAC,WAAW,CAAC,CAAC;CAC3C,CAAC;AAEF;;;GAGG;AACH,MAAM,MAAM,+CAA+C,GAAG;IAC5D,IAAI,EAAE,4CAA4C,CAAC;IACnD,OAAO,EAAE,mBAAmB,CAAC,wBAAwB,CAAC,CAAC;CACxD,CAAC;AAEF;;;GAGG;AACH,MAAM,MAAM,gDAAgD,GAAG;IAC7D,IAAI,EAAE,6CAA6C,CAAC;IACpD,OAAO,EAAE,mBAAmB,CAAC,yBAAyB,CAAC,CAAC;CACzD,CAAC;AAEF;;;GAGG;AACH,MAAM,MAAM,8CAA8C,GAAG;IAC3D,IAAI,EAAE,2CAA2C,CAAC;IAClD,OAAO,EAAE,mBAAmB,CAAC,uBAAuB,CAAC,CAAC;CACvD,CAAC;AAEF;;;GAGG;AACH,MAAM,MAAM,+CAA+C,GAAG;IAC5D,IAAI,EAAE,4CAA4C,CAAC;IACnD,OAAO,EAAE,mBAAmB,CAAC,wBAAwB,CAAC,CAAC;CACxD,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,gCAAgC,GACxC,mCAAmC,GACnC,iCAAiC,GACjC,kCAAkC,GAClC,+CAA+C,GAC/C,gDAAgD,GAChD,8CAA8C,GAC9C,+CAA+C,CAAC"}
|
|
@@ -8,8 +8,7 @@ import type { AnalyticsController } from "./AnalyticsController.mjs";
|
|
|
8
8
|
*
|
|
9
9
|
* Events are only tracked if analytics is enabled.
|
|
10
10
|
*
|
|
11
|
-
* @param
|
|
12
|
-
* @param properties - Event properties
|
|
11
|
+
* @param event - Analytics event with properties and sensitive properties
|
|
13
12
|
*/
|
|
14
13
|
export type AnalyticsControllerTrackEventAction = {
|
|
15
14
|
type: `AnalyticsController:trackEvent`;
|
|
@@ -18,7 +17,6 @@ export type AnalyticsControllerTrackEventAction = {
|
|
|
18
17
|
/**
|
|
19
18
|
* Identify a user for analytics.
|
|
20
19
|
*
|
|
21
|
-
* @param userId - The user identifier (e.g., metametrics ID)
|
|
22
20
|
* @param traits - User traits/properties
|
|
23
21
|
*/
|
|
24
22
|
export type AnalyticsControllerIdentifyAction = {
|
|
@@ -26,45 +24,49 @@ export type AnalyticsControllerIdentifyAction = {
|
|
|
26
24
|
handler: AnalyticsController['identify'];
|
|
27
25
|
};
|
|
28
26
|
/**
|
|
29
|
-
* Track a page view.
|
|
27
|
+
* Track a page or screen view.
|
|
30
28
|
*
|
|
31
|
-
* @param
|
|
32
|
-
* @param properties -
|
|
29
|
+
* @param name - The identifier/name of the page or screen being viewed (e.g., "home", "settings", "wallet")
|
|
30
|
+
* @param properties - Optional properties associated with the view
|
|
33
31
|
*/
|
|
34
|
-
export type
|
|
35
|
-
type: `AnalyticsController:
|
|
36
|
-
handler: AnalyticsController['
|
|
32
|
+
export type AnalyticsControllerTrackViewAction = {
|
|
33
|
+
type: `AnalyticsController:trackView`;
|
|
34
|
+
handler: AnalyticsController['trackView'];
|
|
37
35
|
};
|
|
38
36
|
/**
|
|
39
|
-
*
|
|
37
|
+
* Opt in to analytics for regular account.
|
|
38
|
+
* This updates the user's opt-in status for regular account.
|
|
40
39
|
*/
|
|
41
|
-
export type
|
|
42
|
-
type: `AnalyticsController:
|
|
43
|
-
handler: AnalyticsController['
|
|
40
|
+
export type AnalyticsControllerOptInForRegularAccountAction = {
|
|
41
|
+
type: `AnalyticsController:optInForRegularAccount`;
|
|
42
|
+
handler: AnalyticsController['optInForRegularAccount'];
|
|
44
43
|
};
|
|
45
44
|
/**
|
|
46
|
-
*
|
|
45
|
+
* Opt out of analytics for regular account.
|
|
46
|
+
* This updates the user's opt-in status for regular account.
|
|
47
47
|
*/
|
|
48
|
-
export type
|
|
49
|
-
type: `AnalyticsController:
|
|
50
|
-
handler: AnalyticsController['
|
|
48
|
+
export type AnalyticsControllerOptOutForRegularAccountAction = {
|
|
49
|
+
type: `AnalyticsController:optOutForRegularAccount`;
|
|
50
|
+
handler: AnalyticsController['optOutForRegularAccount'];
|
|
51
51
|
};
|
|
52
52
|
/**
|
|
53
|
-
* Opt in to analytics.
|
|
53
|
+
* Opt in to analytics for social account.
|
|
54
|
+
* This updates the user's opt-in status for social account.
|
|
54
55
|
*/
|
|
55
|
-
export type
|
|
56
|
-
type: `AnalyticsController:
|
|
57
|
-
handler: AnalyticsController['
|
|
56
|
+
export type AnalyticsControllerOptInForSocialAccountAction = {
|
|
57
|
+
type: `AnalyticsController:optInForSocialAccount`;
|
|
58
|
+
handler: AnalyticsController['optInForSocialAccount'];
|
|
58
59
|
};
|
|
59
60
|
/**
|
|
60
|
-
* Opt out of analytics.
|
|
61
|
+
* Opt out of analytics for social account.
|
|
62
|
+
* This updates the user's opt-in status for social account.
|
|
61
63
|
*/
|
|
62
|
-
export type
|
|
63
|
-
type: `AnalyticsController:
|
|
64
|
-
handler: AnalyticsController['
|
|
64
|
+
export type AnalyticsControllerOptOutForSocialAccountAction = {
|
|
65
|
+
type: `AnalyticsController:optOutForSocialAccount`;
|
|
66
|
+
handler: AnalyticsController['optOutForSocialAccount'];
|
|
65
67
|
};
|
|
66
68
|
/**
|
|
67
69
|
* Union of all AnalyticsController action types.
|
|
68
70
|
*/
|
|
69
|
-
export type AnalyticsControllerMethodActions = AnalyticsControllerTrackEventAction | AnalyticsControllerIdentifyAction |
|
|
71
|
+
export type AnalyticsControllerMethodActions = AnalyticsControllerTrackEventAction | AnalyticsControllerIdentifyAction | AnalyticsControllerTrackViewAction | AnalyticsControllerOptInForRegularAccountAction | AnalyticsControllerOptOutForRegularAccountAction | AnalyticsControllerOptInForSocialAccountAction | AnalyticsControllerOptOutForSocialAccountAction;
|
|
70
72
|
//# sourceMappingURL=AnalyticsController-method-action-types.d.mts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AnalyticsController-method-action-types.d.mts","sourceRoot":"","sources":["../src/AnalyticsController-method-action-types.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EAAE,mBAAmB,EAAE,kCAA8B;AAEjE
|
|
1
|
+
{"version":3,"file":"AnalyticsController-method-action-types.d.mts","sourceRoot":"","sources":["../src/AnalyticsController-method-action-types.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EAAE,mBAAmB,EAAE,kCAA8B;AAEjE;;;;;;GAMG;AACH,MAAM,MAAM,mCAAmC,GAAG;IAChD,IAAI,EAAE,gCAAgC,CAAC;IACvC,OAAO,EAAE,mBAAmB,CAAC,YAAY,CAAC,CAAC;CAC5C,CAAC;AAEF;;;;GAIG;AACH,MAAM,MAAM,iCAAiC,GAAG;IAC9C,IAAI,EAAE,8BAA8B,CAAC;IACrC,OAAO,EAAE,mBAAmB,CAAC,UAAU,CAAC,CAAC;CAC1C,CAAC;AAEF;;;;;GAKG;AACH,MAAM,MAAM,kCAAkC,GAAG;IAC/C,IAAI,EAAE,+BAA+B,CAAC;IACtC,OAAO,EAAE,mBAAmB,CAAC,WAAW,CAAC,CAAC;CAC3C,CAAC;AAEF;;;GAGG;AACH,MAAM,MAAM,+CAA+C,GAAG;IAC5D,IAAI,EAAE,4CAA4C,CAAC;IACnD,OAAO,EAAE,mBAAmB,CAAC,wBAAwB,CAAC,CAAC;CACxD,CAAC;AAEF;;;GAGG;AACH,MAAM,MAAM,gDAAgD,GAAG;IAC7D,IAAI,EAAE,6CAA6C,CAAC;IACpD,OAAO,EAAE,mBAAmB,CAAC,yBAAyB,CAAC,CAAC;CACzD,CAAC;AAEF;;;GAGG;AACH,MAAM,MAAM,8CAA8C,GAAG;IAC3D,IAAI,EAAE,2CAA2C,CAAC;IAClD,OAAO,EAAE,mBAAmB,CAAC,uBAAuB,CAAC,CAAC;CACvD,CAAC;AAEF;;;GAGG;AACH,MAAM,MAAM,+CAA+C,GAAG;IAC5D,IAAI,EAAE,4CAA4C,CAAC;IACnD,OAAO,EAAE,mBAAmB,CAAC,wBAAwB,CAAC,CAAC;CACxD,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,gCAAgC,GACxC,mCAAmC,GACnC,iCAAiC,GACjC,kCAAkC,GAClC,+CAA+C,GAC/C,gDAAgD,GAChD,8CAA8C,GAC9C,+CAA+C,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AnalyticsController-method-action-types.mjs","sourceRoot":"","sources":["../src/AnalyticsController-method-action-types.ts"],"names":[],"mappings":"AAAA;;;GAGG","sourcesContent":["/**\n * This file is auto generated by `scripts/generate-method-action-types.ts`.\n * Do not edit manually.\n */\n\nimport type { AnalyticsController } from './AnalyticsController';\n\n/**\n * Track an analytics event.\n *\n * Events are only tracked if analytics is enabled.\n *\n * @param
|
|
1
|
+
{"version":3,"file":"AnalyticsController-method-action-types.mjs","sourceRoot":"","sources":["../src/AnalyticsController-method-action-types.ts"],"names":[],"mappings":"AAAA;;;GAGG","sourcesContent":["/**\n * This file is auto generated by `scripts/generate-method-action-types.ts`.\n * Do not edit manually.\n */\n\nimport type { AnalyticsController } from './AnalyticsController';\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 */\nexport type AnalyticsControllerTrackEventAction = {\n type: `AnalyticsController:trackEvent`;\n handler: AnalyticsController['trackEvent'];\n};\n\n/**\n * Identify a user for analytics.\n *\n * @param traits - User traits/properties\n */\nexport type AnalyticsControllerIdentifyAction = {\n type: `AnalyticsController:identify`;\n handler: AnalyticsController['identify'];\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 */\nexport type AnalyticsControllerTrackViewAction = {\n type: `AnalyticsController:trackView`;\n handler: AnalyticsController['trackView'];\n};\n\n/**\n * Opt in to analytics for regular account.\n * This updates the user's opt-in status for regular account.\n */\nexport type AnalyticsControllerOptInForRegularAccountAction = {\n type: `AnalyticsController:optInForRegularAccount`;\n handler: AnalyticsController['optInForRegularAccount'];\n};\n\n/**\n * Opt out of analytics for regular account.\n * This updates the user's opt-in status for regular account.\n */\nexport type AnalyticsControllerOptOutForRegularAccountAction = {\n type: `AnalyticsController:optOutForRegularAccount`;\n handler: AnalyticsController['optOutForRegularAccount'];\n};\n\n/**\n * Opt in to analytics for social account.\n * This updates the user's opt-in status for social account.\n */\nexport type AnalyticsControllerOptInForSocialAccountAction = {\n type: `AnalyticsController:optInForSocialAccount`;\n handler: AnalyticsController['optInForSocialAccount'];\n};\n\n/**\n * Opt out of analytics for social account.\n * This updates the user's opt-in status for social account.\n */\nexport type AnalyticsControllerOptOutForSocialAccountAction = {\n type: `AnalyticsController:optOutForSocialAccount`;\n handler: AnalyticsController['optOutForSocialAccount'];\n};\n\n/**\n * Union of all AnalyticsController action types.\n */\nexport type AnalyticsControllerMethodActions =\n | AnalyticsControllerTrackEventAction\n | AnalyticsControllerIdentifyAction\n | AnalyticsControllerTrackViewAction\n | AnalyticsControllerOptInForRegularAccountAction\n | AnalyticsControllerOptOutForRegularAccountAction\n | AnalyticsControllerOptInForSocialAccountAction\n | AnalyticsControllerOptOutForSocialAccountAction;\n"]}
|
|
@@ -16,6 +16,8 @@ exports.AnalyticsController = exports.getDefaultAnalyticsControllerState = expor
|
|
|
16
16
|
const base_controller_1 = require("@metamask/base-controller");
|
|
17
17
|
const uuid_1 = require("uuid");
|
|
18
18
|
const AnalyticsLogger_1 = require("./AnalyticsLogger.cjs");
|
|
19
|
+
const analyticsStateComputer_1 = require("./analyticsStateComputer.cjs");
|
|
20
|
+
const analyticsStateValidator_1 = require("./analyticsStateValidator.cjs");
|
|
19
21
|
// === GENERAL ===
|
|
20
22
|
/**
|
|
21
23
|
* The name of the {@link AnalyticsController}, used to namespace the
|
|
@@ -27,13 +29,13 @@ exports.controllerName = 'AnalyticsController';
|
|
|
27
29
|
* The metadata for each property in {@link AnalyticsControllerState}.
|
|
28
30
|
*/
|
|
29
31
|
const analyticsControllerMetadata = {
|
|
30
|
-
|
|
32
|
+
optedInForRegularAccount: {
|
|
31
33
|
includeInStateLogs: true,
|
|
32
34
|
persist: true,
|
|
33
35
|
includeInDebugSnapshot: true,
|
|
34
36
|
usedInUi: true,
|
|
35
37
|
},
|
|
36
|
-
|
|
38
|
+
optedInForSocialAccount: {
|
|
37
39
|
includeInStateLogs: true,
|
|
38
40
|
persist: true,
|
|
39
41
|
includeInDebugSnapshot: true,
|
|
@@ -56,8 +58,8 @@ const analyticsControllerMetadata = {
|
|
|
56
58
|
*/
|
|
57
59
|
function getDefaultAnalyticsControllerState() {
|
|
58
60
|
return {
|
|
59
|
-
|
|
60
|
-
|
|
61
|
+
optedInForRegularAccount: false,
|
|
62
|
+
optedInForSocialAccount: false,
|
|
61
63
|
analyticsId: (0, uuid_1.v4)(),
|
|
62
64
|
};
|
|
63
65
|
}
|
|
@@ -66,11 +68,11 @@ exports.getDefaultAnalyticsControllerState = getDefaultAnalyticsControllerState;
|
|
|
66
68
|
const MESSENGER_EXPOSED_METHODS = [
|
|
67
69
|
'trackEvent',
|
|
68
70
|
'identify',
|
|
69
|
-
'
|
|
70
|
-
'
|
|
71
|
-
'
|
|
72
|
-
'
|
|
73
|
-
'
|
|
71
|
+
'trackView',
|
|
72
|
+
'optInForRegularAccount',
|
|
73
|
+
'optOutForRegularAccount',
|
|
74
|
+
'optInForSocialAccount',
|
|
75
|
+
'optOutForSocialAccount',
|
|
74
76
|
];
|
|
75
77
|
/**
|
|
76
78
|
* The AnalyticsController manages analytics tracking across platforms (Mobile/Extension).
|
|
@@ -87,109 +89,136 @@ class AnalyticsController extends base_controller_1.BaseController {
|
|
|
87
89
|
* Constructs an AnalyticsController instance.
|
|
88
90
|
*
|
|
89
91
|
* @param options - Controller options
|
|
90
|
-
* @param options.state - Initial controller state (defaults from getDefaultAnalyticsControllerState)
|
|
92
|
+
* @param options.state - Initial controller state (defaults from getDefaultAnalyticsControllerState).
|
|
93
|
+
* For migration from a previous system, pass the existing analytics ID via state.analyticsId.
|
|
91
94
|
* @param options.messenger - Messenger used to communicate with BaseController
|
|
92
95
|
* @param options.platformAdapter - Platform adapter implementation for tracking
|
|
93
96
|
*/
|
|
94
97
|
constructor({ state = {}, messenger, platformAdapter, }) {
|
|
98
|
+
const initialState = {
|
|
99
|
+
...getDefaultAnalyticsControllerState(),
|
|
100
|
+
...state,
|
|
101
|
+
};
|
|
102
|
+
(0, analyticsStateValidator_1.validateAnalyticsState)(initialState);
|
|
95
103
|
super({
|
|
96
104
|
name: exports.controllerName,
|
|
97
105
|
metadata: analyticsControllerMetadata,
|
|
98
|
-
state:
|
|
99
|
-
...getDefaultAnalyticsControllerState(),
|
|
100
|
-
...state,
|
|
101
|
-
},
|
|
106
|
+
state: initialState,
|
|
102
107
|
messenger,
|
|
103
108
|
});
|
|
104
109
|
_AnalyticsController_platformAdapter.set(this, void 0);
|
|
105
110
|
__classPrivateFieldSet(this, _AnalyticsController_platformAdapter, platformAdapter, "f");
|
|
106
111
|
this.messenger.registerMethodActionHandlers(this, MESSENGER_EXPOSED_METHODS);
|
|
107
112
|
(0, AnalyticsLogger_1.projectLogger)('AnalyticsController initialized and ready', {
|
|
108
|
-
enabled: this.state
|
|
109
|
-
optedIn: this.state.
|
|
113
|
+
enabled: (0, analyticsStateComputer_1.computeEnabledState)(this.state),
|
|
114
|
+
optedIn: this.state.optedInForRegularAccount,
|
|
115
|
+
socialOptedIn: this.state.optedInForSocialAccount,
|
|
110
116
|
analyticsId: this.state.analyticsId,
|
|
111
117
|
});
|
|
118
|
+
// Call onSetupCompleted lifecycle hook after initialization
|
|
119
|
+
// State is already validated, so analyticsId is guaranteed to be a valid UUIDv4
|
|
120
|
+
try {
|
|
121
|
+
__classPrivateFieldGet(this, _AnalyticsController_platformAdapter, "f").onSetupCompleted(this.state.analyticsId);
|
|
122
|
+
}
|
|
123
|
+
catch (error) {
|
|
124
|
+
// Log error but don't throw - adapter setup failure shouldn't break controller
|
|
125
|
+
(0, AnalyticsLogger_1.projectLogger)('Error calling platformAdapter.onSetupCompleted', error);
|
|
126
|
+
}
|
|
112
127
|
}
|
|
113
128
|
/**
|
|
114
129
|
* Track an analytics event.
|
|
115
130
|
*
|
|
116
131
|
* Events are only tracked if analytics is enabled.
|
|
117
132
|
*
|
|
118
|
-
* @param
|
|
119
|
-
* @param properties - Event properties
|
|
133
|
+
* @param event - Analytics event with properties and sensitive properties
|
|
120
134
|
*/
|
|
121
|
-
trackEvent(
|
|
135
|
+
trackEvent(event) {
|
|
122
136
|
// Don't track if analytics is disabled
|
|
123
|
-
if (!this.state
|
|
137
|
+
if (!(0, analyticsStateComputer_1.computeEnabledState)(this.state)) {
|
|
124
138
|
return;
|
|
125
139
|
}
|
|
126
|
-
//
|
|
127
|
-
|
|
140
|
+
// Derive sensitivity from presence of sensitiveProperties
|
|
141
|
+
const hasSensitiveProperties = Object.keys(event.sensitiveProperties).length > 0;
|
|
142
|
+
// if event does not have properties, send event without properties
|
|
143
|
+
// and return to prevent any additional processing
|
|
144
|
+
if (!event.hasProperties) {
|
|
145
|
+
__classPrivateFieldGet(this, _AnalyticsController_platformAdapter, "f").track(event.name);
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
// Track regular properties (without isSensitive flag - it's the default)
|
|
149
|
+
__classPrivateFieldGet(this, _AnalyticsController_platformAdapter, "f").track(event.name, {
|
|
150
|
+
...event.properties,
|
|
151
|
+
});
|
|
152
|
+
// Track sensitive properties in a separate event with isSensitive flag
|
|
153
|
+
if (hasSensitiveProperties) {
|
|
154
|
+
__classPrivateFieldGet(this, _AnalyticsController_platformAdapter, "f").track(event.name, {
|
|
155
|
+
isSensitive: true,
|
|
156
|
+
...event.properties,
|
|
157
|
+
...event.sensitiveProperties,
|
|
158
|
+
});
|
|
159
|
+
}
|
|
128
160
|
}
|
|
129
161
|
/**
|
|
130
162
|
* Identify a user for analytics.
|
|
131
163
|
*
|
|
132
|
-
* @param userId - The user identifier (e.g., metametrics ID)
|
|
133
164
|
* @param traits - User traits/properties
|
|
134
165
|
*/
|
|
135
|
-
identify(
|
|
136
|
-
if (!this.state
|
|
166
|
+
identify(traits) {
|
|
167
|
+
if (!(0, analyticsStateComputer_1.computeEnabledState)(this.state)) {
|
|
137
168
|
return;
|
|
138
169
|
}
|
|
139
|
-
//
|
|
140
|
-
this.update((state) => {
|
|
141
|
-
state.analyticsId = userId;
|
|
142
|
-
});
|
|
143
|
-
// Delegate to platform adapter if supported
|
|
170
|
+
// Delegate to platform adapter if supported, using the current analytics ID
|
|
144
171
|
if (__classPrivateFieldGet(this, _AnalyticsController_platformAdapter, "f").identify) {
|
|
145
|
-
__classPrivateFieldGet(this, _AnalyticsController_platformAdapter, "f").identify(
|
|
172
|
+
__classPrivateFieldGet(this, _AnalyticsController_platformAdapter, "f").identify(this.state.analyticsId, traits);
|
|
146
173
|
}
|
|
147
174
|
}
|
|
148
175
|
/**
|
|
149
|
-
* Track a page view.
|
|
176
|
+
* Track a page or screen view.
|
|
150
177
|
*
|
|
151
|
-
* @param
|
|
152
|
-
* @param properties -
|
|
178
|
+
* @param name - The identifier/name of the page or screen being viewed (e.g., "home", "settings", "wallet")
|
|
179
|
+
* @param properties - Optional properties associated with the view
|
|
153
180
|
*/
|
|
154
|
-
|
|
155
|
-
if (!this.state
|
|
181
|
+
trackView(name, properties) {
|
|
182
|
+
if (!(0, analyticsStateComputer_1.computeEnabledState)(this.state)) {
|
|
156
183
|
return;
|
|
157
184
|
}
|
|
158
|
-
// Delegate to platform adapter
|
|
159
|
-
|
|
160
|
-
__classPrivateFieldGet(this, _AnalyticsController_platformAdapter, "f").trackPage(pageName, properties);
|
|
161
|
-
}
|
|
185
|
+
// Delegate to platform adapter
|
|
186
|
+
__classPrivateFieldGet(this, _AnalyticsController_platformAdapter, "f").view(name, properties);
|
|
162
187
|
}
|
|
163
188
|
/**
|
|
164
|
-
*
|
|
189
|
+
* Opt in to analytics for regular account.
|
|
190
|
+
* This updates the user's opt-in status for regular account.
|
|
165
191
|
*/
|
|
166
|
-
|
|
192
|
+
optInForRegularAccount() {
|
|
167
193
|
this.update((state) => {
|
|
168
|
-
state.
|
|
194
|
+
state.optedInForRegularAccount = true;
|
|
169
195
|
});
|
|
170
196
|
}
|
|
171
197
|
/**
|
|
172
|
-
*
|
|
198
|
+
* Opt out of analytics for regular account.
|
|
199
|
+
* This updates the user's opt-in status for regular account.
|
|
173
200
|
*/
|
|
174
|
-
|
|
201
|
+
optOutForRegularAccount() {
|
|
175
202
|
this.update((state) => {
|
|
176
|
-
state.
|
|
203
|
+
state.optedInForRegularAccount = false;
|
|
177
204
|
});
|
|
178
205
|
}
|
|
179
206
|
/**
|
|
180
|
-
* Opt in to analytics.
|
|
207
|
+
* Opt in to analytics for social account.
|
|
208
|
+
* This updates the user's opt-in status for social account.
|
|
181
209
|
*/
|
|
182
|
-
|
|
210
|
+
optInForSocialAccount() {
|
|
183
211
|
this.update((state) => {
|
|
184
|
-
state.
|
|
212
|
+
state.optedInForSocialAccount = true;
|
|
185
213
|
});
|
|
186
214
|
}
|
|
187
215
|
/**
|
|
188
|
-
* Opt out of analytics.
|
|
216
|
+
* Opt out of analytics for social account.
|
|
217
|
+
* This updates the user's opt-in status for social account.
|
|
189
218
|
*/
|
|
190
|
-
|
|
219
|
+
optOutForSocialAccount() {
|
|
191
220
|
this.update((state) => {
|
|
192
|
-
state.
|
|
221
|
+
state.optedInForSocialAccount = false;
|
|
193
222
|
});
|
|
194
223
|
}
|
|
195
224
|
}
|