@metamask-previews/analytics-controller 1.0.1-preview-784cc181c → 1.0.1-preview-681bd3562
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +1 -0
- package/dist/AnalyticsController-method-action-types.cjs.map +1 -1
- package/dist/AnalyticsController-method-action-types.d.cts +3 -0
- package/dist/AnalyticsController-method-action-types.d.cts.map +1 -1
- package/dist/AnalyticsController-method-action-types.d.mts +3 -0
- package/dist/AnalyticsController-method-action-types.d.mts.map +1 -1
- package/dist/AnalyticsController-method-action-types.mjs.map +1 -1
- package/dist/AnalyticsController.cjs +40 -10
- package/dist/AnalyticsController.cjs.map +1 -1
- package/dist/AnalyticsController.d.cts +7 -4
- package/dist/AnalyticsController.d.cts.map +1 -1
- package/dist/AnalyticsController.d.mts +7 -4
- package/dist/AnalyticsController.d.mts.map +1 -1
- package/dist/AnalyticsController.mjs +40 -10
- package/dist/AnalyticsController.mjs.map +1 -1
- package/dist/AnalyticsPlatformAdapter.types.cjs.map +1 -1
- package/dist/AnalyticsPlatformAdapter.types.d.cts +10 -3
- package/dist/AnalyticsPlatformAdapter.types.d.cts.map +1 -1
- package/dist/AnalyticsPlatformAdapter.types.d.mts +10 -3
- package/dist/AnalyticsPlatformAdapter.types.d.mts.map +1 -1
- package/dist/AnalyticsPlatformAdapter.types.mjs.map +1 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1 -1
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +1 -1
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
9
9
|
|
|
10
10
|
### Added
|
|
11
11
|
|
|
12
|
+
- Add optional analytics context on `trackEvent`, `identify`, and `trackView` to forward platform-specific context to `AnalyticsPlatformAdapter` implementations ([#8835](https://github.com/MetaMask/core/pull/8835))
|
|
12
13
|
- Optional `skipUUIDv4Check` on `AnalyticsPlatformAdapter` to allow non-UUIDv4 `analyticsId` strings when constructing `AnalyticsController` ([#8543](https://github.com/MetaMask/core/pull/8543))
|
|
13
14
|
|
|
14
15
|
### Changed
|
|
@@ -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.\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.\n */\nexport type AnalyticsControllerOptInAction = {\n type: `AnalyticsController:optIn`;\n handler: AnalyticsController['optIn'];\n};\n\n/**\n * Opt out of analytics.\n */\nexport type AnalyticsControllerOptOutAction = {\n type: `AnalyticsController:optOut`;\n handler: AnalyticsController['optOut'];\n};\n\n/**\n * Union of all AnalyticsController action types.\n */\nexport type AnalyticsControllerMethodActions =\n | AnalyticsControllerTrackEventAction\n | AnalyticsControllerIdentifyAction\n | AnalyticsControllerTrackViewAction\n | AnalyticsControllerOptInAction\n | AnalyticsControllerOptOutAction;\n"]}
|
|
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.\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 * @param context - Optional platform-specific context forwarded to the platform adapter.\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 * @param context - Optional platform-specific context forwarded to the platform adapter.\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 * @param context - Optional platform-specific context forwarded to the platform adapter.\n */\nexport type AnalyticsControllerTrackViewAction = {\n type: `AnalyticsController:trackView`;\n handler: AnalyticsController['trackView'];\n};\n\n/**\n * Opt in to analytics.\n */\nexport type AnalyticsControllerOptInAction = {\n type: `AnalyticsController:optIn`;\n handler: AnalyticsController['optIn'];\n};\n\n/**\n * Opt out of analytics.\n */\nexport type AnalyticsControllerOptOutAction = {\n type: `AnalyticsController:optOut`;\n handler: AnalyticsController['optOut'];\n};\n\n/**\n * Union of all AnalyticsController action types.\n */\nexport type AnalyticsControllerMethodActions =\n | AnalyticsControllerTrackEventAction\n | AnalyticsControllerIdentifyAction\n | AnalyticsControllerTrackViewAction\n | AnalyticsControllerOptInAction\n | AnalyticsControllerOptOutAction;\n"]}
|
|
@@ -9,6 +9,7 @@ import type { AnalyticsController } from "./AnalyticsController.cjs";
|
|
|
9
9
|
* Events are only tracked if analytics is enabled.
|
|
10
10
|
*
|
|
11
11
|
* @param event - Analytics event with properties and sensitive properties
|
|
12
|
+
* @param context - Optional platform-specific context forwarded to the platform adapter.
|
|
12
13
|
*/
|
|
13
14
|
export type AnalyticsControllerTrackEventAction = {
|
|
14
15
|
type: `AnalyticsController:trackEvent`;
|
|
@@ -18,6 +19,7 @@ export type AnalyticsControllerTrackEventAction = {
|
|
|
18
19
|
* Identify a user for analytics.
|
|
19
20
|
*
|
|
20
21
|
* @param traits - User traits/properties
|
|
22
|
+
* @param context - Optional platform-specific context forwarded to the platform adapter.
|
|
21
23
|
*/
|
|
22
24
|
export type AnalyticsControllerIdentifyAction = {
|
|
23
25
|
type: `AnalyticsController:identify`;
|
|
@@ -28,6 +30,7 @@ export type AnalyticsControllerIdentifyAction = {
|
|
|
28
30
|
*
|
|
29
31
|
* @param name - The identifier/name of the page or screen being viewed (e.g., "home", "settings", "wallet")
|
|
30
32
|
* @param properties - Optional properties associated with the view
|
|
33
|
+
* @param context - Optional platform-specific context forwarded to the platform adapter.
|
|
31
34
|
*/
|
|
32
35
|
export type AnalyticsControllerTrackViewAction = {
|
|
33
36
|
type: `AnalyticsController:trackView`;
|
|
@@ -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;;;;;;;GAOG;AACH,MAAM,MAAM,mCAAmC,GAAG;IAChD,IAAI,EAAE,gCAAgC,CAAC;IACvC,OAAO,EAAE,mBAAmB,CAAC,YAAY,CAAC,CAAC;CAC5C,CAAC;AAEF;;;;;GAKG;AACH,MAAM,MAAM,iCAAiC,GAAG;IAC9C,IAAI,EAAE,8BAA8B,CAAC;IACrC,OAAO,EAAE,mBAAmB,CAAC,UAAU,CAAC,CAAC;CAC1C,CAAC;AAEF;;;;;;GAMG;AACH,MAAM,MAAM,kCAAkC,GAAG;IAC/C,IAAI,EAAE,+BAA+B,CAAC;IACtC,OAAO,EAAE,mBAAmB,CAAC,WAAW,CAAC,CAAC;CAC3C,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,8BAA8B,GAAG;IAC3C,IAAI,EAAE,2BAA2B,CAAC;IAClC,OAAO,EAAE,mBAAmB,CAAC,OAAO,CAAC,CAAC;CACvC,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,+BAA+B,GAAG;IAC5C,IAAI,EAAE,4BAA4B,CAAC;IACnC,OAAO,EAAE,mBAAmB,CAAC,QAAQ,CAAC,CAAC;CACxC,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,gCAAgC,GACxC,mCAAmC,GACnC,iCAAiC,GACjC,kCAAkC,GAClC,8BAA8B,GAC9B,+BAA+B,CAAC"}
|
|
@@ -9,6 +9,7 @@ import type { AnalyticsController } from "./AnalyticsController.mjs";
|
|
|
9
9
|
* Events are only tracked if analytics is enabled.
|
|
10
10
|
*
|
|
11
11
|
* @param event - Analytics event with properties and sensitive properties
|
|
12
|
+
* @param context - Optional platform-specific context forwarded to the platform adapter.
|
|
12
13
|
*/
|
|
13
14
|
export type AnalyticsControllerTrackEventAction = {
|
|
14
15
|
type: `AnalyticsController:trackEvent`;
|
|
@@ -18,6 +19,7 @@ export type AnalyticsControllerTrackEventAction = {
|
|
|
18
19
|
* Identify a user for analytics.
|
|
19
20
|
*
|
|
20
21
|
* @param traits - User traits/properties
|
|
22
|
+
* @param context - Optional platform-specific context forwarded to the platform adapter.
|
|
21
23
|
*/
|
|
22
24
|
export type AnalyticsControllerIdentifyAction = {
|
|
23
25
|
type: `AnalyticsController:identify`;
|
|
@@ -28,6 +30,7 @@ export type AnalyticsControllerIdentifyAction = {
|
|
|
28
30
|
*
|
|
29
31
|
* @param name - The identifier/name of the page or screen being viewed (e.g., "home", "settings", "wallet")
|
|
30
32
|
* @param properties - Optional properties associated with the view
|
|
33
|
+
* @param context - Optional platform-specific context forwarded to the platform adapter.
|
|
31
34
|
*/
|
|
32
35
|
export type AnalyticsControllerTrackViewAction = {
|
|
33
36
|
type: `AnalyticsController:trackView`;
|
|
@@ -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;;;;;;;GAOG;AACH,MAAM,MAAM,mCAAmC,GAAG;IAChD,IAAI,EAAE,gCAAgC,CAAC;IACvC,OAAO,EAAE,mBAAmB,CAAC,YAAY,CAAC,CAAC;CAC5C,CAAC;AAEF;;;;;GAKG;AACH,MAAM,MAAM,iCAAiC,GAAG;IAC9C,IAAI,EAAE,8BAA8B,CAAC;IACrC,OAAO,EAAE,mBAAmB,CAAC,UAAU,CAAC,CAAC;CAC1C,CAAC;AAEF;;;;;;GAMG;AACH,MAAM,MAAM,kCAAkC,GAAG;IAC/C,IAAI,EAAE,+BAA+B,CAAC;IACtC,OAAO,EAAE,mBAAmB,CAAC,WAAW,CAAC,CAAC;CAC3C,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,8BAA8B,GAAG;IAC3C,IAAI,EAAE,2BAA2B,CAAC;IAClC,OAAO,EAAE,mBAAmB,CAAC,OAAO,CAAC,CAAC;CACvC,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,+BAA+B,GAAG;IAC5C,IAAI,EAAE,4BAA4B,CAAC;IACnC,OAAO,EAAE,mBAAmB,CAAC,QAAQ,CAAC,CAAC;CACxC,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,gCAAgC,GACxC,mCAAmC,GACnC,iCAAiC,GACjC,kCAAkC,GAClC,8BAA8B,GAC9B,+BAA+B,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.\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.\n */\nexport type AnalyticsControllerOptInAction = {\n type: `AnalyticsController:optIn`;\n handler: AnalyticsController['optIn'];\n};\n\n/**\n * Opt out of analytics.\n */\nexport type AnalyticsControllerOptOutAction = {\n type: `AnalyticsController:optOut`;\n handler: AnalyticsController['optOut'];\n};\n\n/**\n * Union of all AnalyticsController action types.\n */\nexport type AnalyticsControllerMethodActions =\n | AnalyticsControllerTrackEventAction\n | AnalyticsControllerIdentifyAction\n | AnalyticsControllerTrackViewAction\n | AnalyticsControllerOptInAction\n | AnalyticsControllerOptOutAction;\n"]}
|
|
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.\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 * @param context - Optional platform-specific context forwarded to the platform adapter.\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 * @param context - Optional platform-specific context forwarded to the platform adapter.\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 * @param context - Optional platform-specific context forwarded to the platform adapter.\n */\nexport type AnalyticsControllerTrackViewAction = {\n type: `AnalyticsController:trackView`;\n handler: AnalyticsController['trackView'];\n};\n\n/**\n * Opt in to analytics.\n */\nexport type AnalyticsControllerOptInAction = {\n type: `AnalyticsController:optIn`;\n handler: AnalyticsController['optIn'];\n};\n\n/**\n * Opt out of analytics.\n */\nexport type AnalyticsControllerOptOutAction = {\n type: `AnalyticsController:optOut`;\n handler: AnalyticsController['optOut'];\n};\n\n/**\n * Union of all AnalyticsController action types.\n */\nexport type AnalyticsControllerMethodActions =\n | AnalyticsControllerTrackEventAction\n | AnalyticsControllerIdentifyAction\n | AnalyticsControllerTrackViewAction\n | AnalyticsControllerOptInAction\n | AnalyticsControllerOptOutAction;\n"]}
|
|
@@ -143,8 +143,9 @@ class AnalyticsController extends base_controller_1.BaseController {
|
|
|
143
143
|
* Events are only tracked if analytics is enabled.
|
|
144
144
|
*
|
|
145
145
|
* @param event - Analytics event with properties and sensitive properties
|
|
146
|
+
* @param context - Optional platform-specific context forwarded to the platform adapter.
|
|
146
147
|
*/
|
|
147
|
-
trackEvent(event) {
|
|
148
|
+
trackEvent(event, context) {
|
|
148
149
|
// Don't track if analytics is disabled
|
|
149
150
|
if (!selectors_1.analyticsControllerSelectors.selectEnabled(this.state)) {
|
|
150
151
|
return;
|
|
@@ -152,50 +153,79 @@ class AnalyticsController extends base_controller_1.BaseController {
|
|
|
152
153
|
// if event does not have properties, send event without properties
|
|
153
154
|
// and return to prevent any additional processing
|
|
154
155
|
if (!event.hasProperties) {
|
|
155
|
-
|
|
156
|
+
if (context) {
|
|
157
|
+
__classPrivateFieldGet(this, _AnalyticsController_platformAdapter, "f").track(event.name, undefined, context);
|
|
158
|
+
}
|
|
159
|
+
else {
|
|
160
|
+
__classPrivateFieldGet(this, _AnalyticsController_platformAdapter, "f").track(event.name);
|
|
161
|
+
}
|
|
156
162
|
return;
|
|
157
163
|
}
|
|
158
164
|
// Track regular properties first if anonymous events feature is enabled
|
|
159
165
|
if (__classPrivateFieldGet(this, _AnalyticsController_isAnonymousEventsFeatureEnabled, "f")) {
|
|
160
166
|
// Note: Even if regular properties object is empty, we still send it to ensure
|
|
161
167
|
// an event with user ID is tracked.
|
|
162
|
-
|
|
168
|
+
const properties = {
|
|
163
169
|
...event.properties,
|
|
164
|
-
}
|
|
170
|
+
};
|
|
171
|
+
if (context) {
|
|
172
|
+
__classPrivateFieldGet(this, _AnalyticsController_platformAdapter, "f").track(event.name, properties, context);
|
|
173
|
+
}
|
|
174
|
+
else {
|
|
175
|
+
__classPrivateFieldGet(this, _AnalyticsController_platformAdapter, "f").track(event.name, properties);
|
|
176
|
+
}
|
|
165
177
|
}
|
|
166
178
|
const hasSensitiveProperties = Object.keys(event.sensitiveProperties).length > 0;
|
|
167
179
|
if (!__classPrivateFieldGet(this, _AnalyticsController_isAnonymousEventsFeatureEnabled, "f") || hasSensitiveProperties) {
|
|
168
|
-
|
|
180
|
+
const properties = {
|
|
169
181
|
...event.properties,
|
|
170
182
|
...event.sensitiveProperties,
|
|
171
183
|
...(hasSensitiveProperties && { anonymous: true }),
|
|
172
|
-
}
|
|
184
|
+
};
|
|
185
|
+
if (context) {
|
|
186
|
+
__classPrivateFieldGet(this, _AnalyticsController_platformAdapter, "f").track(event.name, properties, context);
|
|
187
|
+
}
|
|
188
|
+
else {
|
|
189
|
+
__classPrivateFieldGet(this, _AnalyticsController_platformAdapter, "f").track(event.name, properties);
|
|
190
|
+
}
|
|
173
191
|
}
|
|
174
192
|
}
|
|
175
193
|
/**
|
|
176
194
|
* Identify a user for analytics.
|
|
177
195
|
*
|
|
178
196
|
* @param traits - User traits/properties
|
|
197
|
+
* @param context - Optional platform-specific context forwarded to the platform adapter.
|
|
179
198
|
*/
|
|
180
|
-
identify(traits) {
|
|
199
|
+
identify(traits, context) {
|
|
181
200
|
if (!selectors_1.analyticsControllerSelectors.selectEnabled(this.state)) {
|
|
182
201
|
return;
|
|
183
202
|
}
|
|
184
203
|
// Delegate to platform adapter using the current analytics ID
|
|
185
|
-
|
|
204
|
+
if (context) {
|
|
205
|
+
__classPrivateFieldGet(this, _AnalyticsController_platformAdapter, "f").identify(this.state.analyticsId, traits, context);
|
|
206
|
+
}
|
|
207
|
+
else {
|
|
208
|
+
__classPrivateFieldGet(this, _AnalyticsController_platformAdapter, "f").identify(this.state.analyticsId, traits);
|
|
209
|
+
}
|
|
186
210
|
}
|
|
187
211
|
/**
|
|
188
212
|
* Track a page or screen view.
|
|
189
213
|
*
|
|
190
214
|
* @param name - The identifier/name of the page or screen being viewed (e.g., "home", "settings", "wallet")
|
|
191
215
|
* @param properties - Optional properties associated with the view
|
|
216
|
+
* @param context - Optional platform-specific context forwarded to the platform adapter.
|
|
192
217
|
*/
|
|
193
|
-
trackView(name, properties) {
|
|
218
|
+
trackView(name, properties, context) {
|
|
194
219
|
if (!selectors_1.analyticsControllerSelectors.selectEnabled(this.state)) {
|
|
195
220
|
return;
|
|
196
221
|
}
|
|
197
222
|
// Delegate to platform adapter
|
|
198
|
-
|
|
223
|
+
if (context) {
|
|
224
|
+
__classPrivateFieldGet(this, _AnalyticsController_platformAdapter, "f").view(name, properties, context);
|
|
225
|
+
}
|
|
226
|
+
else {
|
|
227
|
+
__classPrivateFieldGet(this, _AnalyticsController_platformAdapter, "f").view(name, properties);
|
|
228
|
+
}
|
|
199
229
|
}
|
|
200
230
|
/**
|
|
201
231
|
* Opt in to analytics.
|
|
@@ -1 +1 @@
|
|
|
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;;;;;GAKG;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,IAAI;QACb,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;AA+EX;;;;;;;;;;;;GAYG;AACH,MAAa,mBAAoB,SAAQ,gCAIxC;IAOC;;;;;;;;;;;OAWG;IACH,YAAY,EACV,KAAK,EACL,SAAS,EACT,eAAe,EACf,+BAA+B,GAAG,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AC,WAAW,EAAE,MAAM,CAAC,CAAC;IACjE,CAAC;IAED;;;;;OAKG;IACH,SAAS,CAAC,IAAY,EAAE,UAAqC;QAC3D,IAAI,CAAC,wCAA4B,CAAC,aAAa,CAAC,IAAI,CAAC,KAAK,CAAC,EAAE,CAAC;YAC5D,OAAO;QACT,CAAC;QAED,+BAA+B;QAC/B,uBAAA,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;AA3KD,kDA2KC","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 * Both `optedIn` and `analyticsId` are persisted (`persist: true`).\n * The platform must supply a valid UUIDv4 `analyticsId` on first run.\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: true,\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 the ID on first run.\n * It is then persisted with controller state when using a persisted store.\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 * The controller persists `optedIn` and `analyticsId` when composed with a persisted\n * store. The platform must supply a valid `analyticsId` on first launch.\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(\n initialState,\n platformAdapter.skipUUIDv4Check === true,\n );\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"]}
|
|
1
|
+
{"version":3,"file":"AnalyticsController.cjs","sourceRoot":"","sources":["../src/AnalyticsController.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAKA,+DAA2D;AAI3D,+FAAuF;AACvF,2DAAyD;AAQzD,+CAA2D;AAE3D,kBAAkB;AAElB;;;;GAIG;AACU,QAAA,cAAc,GAAG,qBAAqB,CAAC;AAqBpD;;;;;;;GAOG;AACH,SAAgB,kCAAkC;IAIhD,OAAO;QACL,O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IAAI,CAAC,KAAK,CAAC,WAAW,EAAE,MAAM,EAAE,OAAO,CAAC,CAAC;QAC1E,CAAC;aAAM,CAAC;YACN,uBAAA,IAAI,4CAAiB,CAAC,QAAQ,CAAC,IAAI,CAAC,KAAK,CAAC,WAAW,EAAE,MAAM,CAAC,CAAC;QACjE,CAAC;IACH,CAAC;IAED;;;;;;OAMG;IACH,SAAS,CACP,IAAY,EACZ,UAAqC,EACrC,OAA0B;QAE1B,IAAI,CAAC,wCAA4B,CAAC,aAAa,CAAC,IAAI,CAAC,KAAK,CAAC,EAAE,CAAC;YAC5D,OAAO;QACT,CAAC;QAED,+BAA+B;QAC/B,IAAI,OAAO,EAAE,CAAC;YACZ,uBAAA,IAAI,4CAAiB,CAAC,IAAI,CAAC,IAAI,EAAE,UAAU,EAAE,OAAO,CAAC,CAAC;QACxD,CAAC;aAAM,CAAC;YACN,uBAAA,IAAI,4CAAiB,CAAC,IAAI,CAAC,IAAI,EAAE,UAAU,CAAC,CAAC;QAC/C,CAAC;IACH,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;AAxMD,kDAwMC","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 AnalyticsContext,\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 * Both `optedIn` and `analyticsId` are persisted (`persist: true`).\n * The platform must supply a valid UUIDv4 `analyticsId` on first run.\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: true,\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 the ID on first run.\n * It is then persisted with controller state when using a persisted store.\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 * The controller persists `optedIn` and `analyticsId` when composed with a persisted\n * store. The platform must supply a valid `analyticsId` on first launch.\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(\n initialState,\n platformAdapter.skipUUIDv4Check === true,\n );\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 * @param context - Optional platform-specific context forwarded to the platform adapter.\n */\n trackEvent(event: AnalyticsTrackingEvent, context?: AnalyticsContext): 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 if (context) {\n this.#platformAdapter.track(event.name, undefined, context);\n } else {\n this.#platformAdapter.track(event.name);\n }\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 const properties = {\n ...event.properties,\n };\n if (context) {\n this.#platformAdapter.track(event.name, properties, context);\n } else {\n this.#platformAdapter.track(event.name, properties);\n }\n }\n\n const hasSensitiveProperties =\n Object.keys(event.sensitiveProperties).length > 0;\n\n if (!this.#isAnonymousEventsFeatureEnabled || hasSensitiveProperties) {\n const properties = {\n ...event.properties,\n ...event.sensitiveProperties,\n ...(hasSensitiveProperties && { anonymous: true }),\n };\n if (context) {\n this.#platformAdapter.track(event.name, properties, context);\n } else {\n this.#platformAdapter.track(event.name, properties);\n }\n }\n }\n\n /**\n * Identify a user for analytics.\n *\n * @param traits - User traits/properties\n * @param context - Optional platform-specific context forwarded to the platform adapter.\n */\n identify(traits?: AnalyticsUserTraits, context?: AnalyticsContext): void {\n if (!analyticsControllerSelectors.selectEnabled(this.state)) {\n return;\n }\n\n // Delegate to platform adapter using the current analytics ID\n if (context) {\n this.#platformAdapter.identify(this.state.analyticsId, traits, context);\n } else {\n this.#platformAdapter.identify(this.state.analyticsId, traits);\n }\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 * @param context - Optional platform-specific context forwarded to the platform adapter.\n */\n trackView(\n name: string,\n properties?: AnalyticsEventProperties,\n context?: AnalyticsContext,\n ): void {\n if (!analyticsControllerSelectors.selectEnabled(this.state)) {\n return;\n }\n\n // Delegate to platform adapter\n if (context) {\n this.#platformAdapter.view(name, properties, context);\n } else {\n this.#platformAdapter.view(name, properties);\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"]}
|
|
@@ -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, AnalyticsUserTraits, AnalyticsTrackingEvent } from "./AnalyticsPlatformAdapter.types.cjs";
|
|
5
|
+
import type { AnalyticsPlatformAdapter, AnalyticsContext, 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
|
|
@@ -126,21 +126,24 @@ export declare class AnalyticsController extends BaseController<'AnalyticsContro
|
|
|
126
126
|
* Events are only tracked if analytics is enabled.
|
|
127
127
|
*
|
|
128
128
|
* @param event - Analytics event with properties and sensitive properties
|
|
129
|
+
* @param context - Optional platform-specific context forwarded to the platform adapter.
|
|
129
130
|
*/
|
|
130
|
-
trackEvent(event: AnalyticsTrackingEvent): void;
|
|
131
|
+
trackEvent(event: AnalyticsTrackingEvent, context?: AnalyticsContext): void;
|
|
131
132
|
/**
|
|
132
133
|
* Identify a user for analytics.
|
|
133
134
|
*
|
|
134
135
|
* @param traits - User traits/properties
|
|
136
|
+
* @param context - Optional platform-specific context forwarded to the platform adapter.
|
|
135
137
|
*/
|
|
136
|
-
identify(traits?: AnalyticsUserTraits): void;
|
|
138
|
+
identify(traits?: AnalyticsUserTraits, context?: AnalyticsContext): void;
|
|
137
139
|
/**
|
|
138
140
|
* Track a page or screen view.
|
|
139
141
|
*
|
|
140
142
|
* @param name - The identifier/name of the page or screen being viewed (e.g., "home", "settings", "wallet")
|
|
141
143
|
* @param properties - Optional properties associated with the view
|
|
144
|
+
* @param context - Optional platform-specific context forwarded to the platform adapter.
|
|
142
145
|
*/
|
|
143
|
-
trackView(name: string, properties?: AnalyticsEventProperties): void;
|
|
146
|
+
trackView(name: string, properties?: AnalyticsEventProperties, context?: AnalyticsContext): void;
|
|
144
147
|
/**
|
|
145
148
|
* Opt in to analytics.
|
|
146
149
|
*/
|
|
@@ -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;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,M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AG;IACvC;;;;OAIG;IACH,KAAK,EAAE,wBAAwB,CAAC;IAChC;;OAEG;IACH,SAAS,EAAE,4BAA4B,CAAC;IACxC;;OAEG;IACH,eAAe,EAAE,wBAAwB,CAAC;IAE1C;;;;OAIG;IACH,+BAA+B,CAAC,EAAE,OAAO,CAAC;CAC3C,CAAC;AAEF;;;;;;;;;;;;GAYG;AACH,qBAAa,mBAAoB,SAAQ,cAAc,CACrD,qBAAqB,EACrB,wBAAwB,EACxB,4BAA4B,CAC7B;;IAOC;;;;;;;;;;;OAWG;gBACS,EACV,KAAK,EACL,SAAS,EACT,eAAe,EACf,+BAAuC,GACxC,EAAE,0BAA0B;IAkC7B;;;OAGG;IACH,IAAI,IAAI,IAAI;IAkBZ;;;;;;;OAOG;IACH,UAAU,CAAC,KAAK,EAAE,sBAAsB,EAAE,OAAO,CAAC,EAAE,gBAAgB,GAAG,IAAI;IAgD3E;;;;;OAKG;IACH,QAAQ,CAAC,MAAM,CAAC,EAAE,mBAAmB,EAAE,OAAO,CAAC,EAAE,gBAAgB,GAAG,IAAI;IAaxE;;;;;;OAMG;IACH,SAAS,CACP,IAAI,EAAE,MAAM,EACZ,UAAU,CAAC,EAAE,wBAAwB,EACrC,OAAO,CAAC,EAAE,gBAAgB,GACzB,IAAI;IAaP;;OAEG;IACH,KAAK,IAAI,IAAI;IAMb;;OAEG;IACH,MAAM,IAAI,IAAI;CAKf"}
|
|
@@ -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, AnalyticsUserTraits, AnalyticsTrackingEvent } from "./AnalyticsPlatformAdapter.types.mjs";
|
|
5
|
+
import type { AnalyticsPlatformAdapter, AnalyticsContext, 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
|
|
@@ -126,21 +126,24 @@ export declare class AnalyticsController extends BaseController<'AnalyticsContro
|
|
|
126
126
|
* Events are only tracked if analytics is enabled.
|
|
127
127
|
*
|
|
128
128
|
* @param event - Analytics event with properties and sensitive properties
|
|
129
|
+
* @param context - Optional platform-specific context forwarded to the platform adapter.
|
|
129
130
|
*/
|
|
130
|
-
trackEvent(event: AnalyticsTrackingEvent): void;
|
|
131
|
+
trackEvent(event: AnalyticsTrackingEvent, context?: AnalyticsContext): void;
|
|
131
132
|
/**
|
|
132
133
|
* Identify a user for analytics.
|
|
133
134
|
*
|
|
134
135
|
* @param traits - User traits/properties
|
|
136
|
+
* @param context - Optional platform-specific context forwarded to the platform adapter.
|
|
135
137
|
*/
|
|
136
|
-
identify(traits?: AnalyticsUserTraits): void;
|
|
138
|
+
identify(traits?: AnalyticsUserTraits, context?: AnalyticsContext): void;
|
|
137
139
|
/**
|
|
138
140
|
* Track a page or screen view.
|
|
139
141
|
*
|
|
140
142
|
* @param name - The identifier/name of the page or screen being viewed (e.g., "home", "settings", "wallet")
|
|
141
143
|
* @param properties - Optional properties associated with the view
|
|
144
|
+
* @param context - Optional platform-specific context forwarded to the platform adapter.
|
|
142
145
|
*/
|
|
143
|
-
trackView(name: string, properties?: AnalyticsEventProperties): void;
|
|
146
|
+
trackView(name: string, properties?: AnalyticsEventProperties, context?: AnalyticsContext): void;
|
|
144
147
|
/**
|
|
145
148
|
* Opt in to analytics.
|
|
146
149
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AnalyticsController.d.mts","sourceRoot":"","sources":["../src/AnalyticsController.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,wBAAwB,EACxB,0BAA0B,EAE3B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AEG;IACH,eAAe,EAAE,wBAAwB,CAAC;IAE1C;;;;OAIG;IACH,+BAA+B,CAAC,EAAE,OAAO,CAAC;CAC3C,CAAC;AAEF;;;;;;;;;;;;GAYG;AACH,qBAAa,mBAAoB,SAAQ,cAAc,CACrD,qBAAqB,EACrB,wBAAwB,EACxB,4BAA4B,CAC7B;;IAOC;;;;;;;;;;;OAWG;gBACS,EACV,KAAK,EACL,SAAS,EACT,eAAe,EACf,+BAAuC,GACxC,EAAE,0BAA0B;IAkC7B;;;OAGG;IACH,IAAI,IAAI,IAAI;IAkBZ
|
|
1
|
+
{"version":3,"file":"AnalyticsController.d.mts","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,gBAAgB,EAChB,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;AAiCD;;GAEG;AACH,MAAM,MAAM,iCAAiC,GAAG,wBAAwB,CACtE,OAAO,cAAc,EACrB,wBAAwB,CACzB,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,0BAA0B,GAClC,iCAAiC,GACjC,gCAAgC,CAAC;AAErC;;GAEG;AACH,KAAK,cAA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,IAAI;IAMb;;OAEG;IACH,MAAM,IAAI,IAAI;CAKf"}
|
|
@@ -139,8 +139,9 @@ export class AnalyticsController extends BaseController {
|
|
|
139
139
|
* Events are only tracked if analytics is enabled.
|
|
140
140
|
*
|
|
141
141
|
* @param event - Analytics event with properties and sensitive properties
|
|
142
|
+
* @param context - Optional platform-specific context forwarded to the platform adapter.
|
|
142
143
|
*/
|
|
143
|
-
trackEvent(event) {
|
|
144
|
+
trackEvent(event, context) {
|
|
144
145
|
// Don't track if analytics is disabled
|
|
145
146
|
if (!analyticsControllerSelectors.selectEnabled(this.state)) {
|
|
146
147
|
return;
|
|
@@ -148,50 +149,79 @@ export class AnalyticsController extends BaseController {
|
|
|
148
149
|
// if event does not have properties, send event without properties
|
|
149
150
|
// and return to prevent any additional processing
|
|
150
151
|
if (!event.hasProperties) {
|
|
151
|
-
|
|
152
|
+
if (context) {
|
|
153
|
+
__classPrivateFieldGet(this, _AnalyticsController_platformAdapter, "f").track(event.name, undefined, context);
|
|
154
|
+
}
|
|
155
|
+
else {
|
|
156
|
+
__classPrivateFieldGet(this, _AnalyticsController_platformAdapter, "f").track(event.name);
|
|
157
|
+
}
|
|
152
158
|
return;
|
|
153
159
|
}
|
|
154
160
|
// Track regular properties first if anonymous events feature is enabled
|
|
155
161
|
if (__classPrivateFieldGet(this, _AnalyticsController_isAnonymousEventsFeatureEnabled, "f")) {
|
|
156
162
|
// Note: Even if regular properties object is empty, we still send it to ensure
|
|
157
163
|
// an event with user ID is tracked.
|
|
158
|
-
|
|
164
|
+
const properties = {
|
|
159
165
|
...event.properties,
|
|
160
|
-
}
|
|
166
|
+
};
|
|
167
|
+
if (context) {
|
|
168
|
+
__classPrivateFieldGet(this, _AnalyticsController_platformAdapter, "f").track(event.name, properties, context);
|
|
169
|
+
}
|
|
170
|
+
else {
|
|
171
|
+
__classPrivateFieldGet(this, _AnalyticsController_platformAdapter, "f").track(event.name, properties);
|
|
172
|
+
}
|
|
161
173
|
}
|
|
162
174
|
const hasSensitiveProperties = Object.keys(event.sensitiveProperties).length > 0;
|
|
163
175
|
if (!__classPrivateFieldGet(this, _AnalyticsController_isAnonymousEventsFeatureEnabled, "f") || hasSensitiveProperties) {
|
|
164
|
-
|
|
176
|
+
const properties = {
|
|
165
177
|
...event.properties,
|
|
166
178
|
...event.sensitiveProperties,
|
|
167
179
|
...(hasSensitiveProperties && { anonymous: true }),
|
|
168
|
-
}
|
|
180
|
+
};
|
|
181
|
+
if (context) {
|
|
182
|
+
__classPrivateFieldGet(this, _AnalyticsController_platformAdapter, "f").track(event.name, properties, context);
|
|
183
|
+
}
|
|
184
|
+
else {
|
|
185
|
+
__classPrivateFieldGet(this, _AnalyticsController_platformAdapter, "f").track(event.name, properties);
|
|
186
|
+
}
|
|
169
187
|
}
|
|
170
188
|
}
|
|
171
189
|
/**
|
|
172
190
|
* Identify a user for analytics.
|
|
173
191
|
*
|
|
174
192
|
* @param traits - User traits/properties
|
|
193
|
+
* @param context - Optional platform-specific context forwarded to the platform adapter.
|
|
175
194
|
*/
|
|
176
|
-
identify(traits) {
|
|
195
|
+
identify(traits, context) {
|
|
177
196
|
if (!analyticsControllerSelectors.selectEnabled(this.state)) {
|
|
178
197
|
return;
|
|
179
198
|
}
|
|
180
199
|
// Delegate to platform adapter using the current analytics ID
|
|
181
|
-
|
|
200
|
+
if (context) {
|
|
201
|
+
__classPrivateFieldGet(this, _AnalyticsController_platformAdapter, "f").identify(this.state.analyticsId, traits, context);
|
|
202
|
+
}
|
|
203
|
+
else {
|
|
204
|
+
__classPrivateFieldGet(this, _AnalyticsController_platformAdapter, "f").identify(this.state.analyticsId, traits);
|
|
205
|
+
}
|
|
182
206
|
}
|
|
183
207
|
/**
|
|
184
208
|
* Track a page or screen view.
|
|
185
209
|
*
|
|
186
210
|
* @param name - The identifier/name of the page or screen being viewed (e.g., "home", "settings", "wallet")
|
|
187
211
|
* @param properties - Optional properties associated with the view
|
|
212
|
+
* @param context - Optional platform-specific context forwarded to the platform adapter.
|
|
188
213
|
*/
|
|
189
|
-
trackView(name, properties) {
|
|
214
|
+
trackView(name, properties, context) {
|
|
190
215
|
if (!analyticsControllerSelectors.selectEnabled(this.state)) {
|
|
191
216
|
return;
|
|
192
217
|
}
|
|
193
218
|
// Delegate to platform adapter
|
|
194
|
-
|
|
219
|
+
if (context) {
|
|
220
|
+
__classPrivateFieldGet(this, _AnalyticsController_platformAdapter, "f").view(name, properties, context);
|
|
221
|
+
}
|
|
222
|
+
else {
|
|
223
|
+
__classPrivateFieldGet(this, _AnalyticsController_platformAdapter, "f").view(name, properties);
|
|
224
|
+
}
|
|
195
225
|
}
|
|
196
226
|
/**
|
|
197
227
|
* Opt in to analytics.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AnalyticsController.mjs","sourceRoot":"","sources":["../src/AnalyticsController.ts"],"names":[],"mappings":";;;;;;;;;;;;AAKA,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAI3D,OAAO,EAAE,gCAAgC,EAAE,gDAA4C;AACvF,OAAO,EAAE,aAAa,IAAI,GAAG,EAAE,8BAA0B;AAOzD,OAAO,EAAE,4BAA4B,EAAE,wBAAoB;AAE3D,kBAAkB;AAElB;;;;GAIG;AACH,MAAM,CAAC,MAAM,cAAc,GAAG,qBAAqB,CAAC;AAqBpD;;;;;;;GAOG;AACH,MAAM,UAAU,kCAAkC;IAIhD,OAAO;QACL,OAAO,EAAE,KAAK;KACf,CAAC;AACJ,CAAC;AAED;;;;;GAKG;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,IAAI;QACb,sBAAsB,EAAE,IAAI;QAC5B,QAAQ,EAAE,KAAK;KAChB;CACgD,CAAC;AAEpD,oBAAoB;AAEpB,MAA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H,QAAQ,CAAC,MAA4B;QACnC,IAAI,CAAC,4BAA4B,CAAC,aAAa,CAAC,IAAI,CAAC,KAAK,CAAC,EAAE,CAAC;YAC5D,OAAO;QACT,CAAC;QAED,8DAA8D;QAC9D,uBAAA,IAAI,4CAAiB,CAAC,QAAQ,CAAC,IAAI,CAAC,KAAK,CAAC,WAAW,EAAE,MAAM,CAAC,CAAC;IACjE,CAAC;IAED;;;;;OAKG;IACH,SAAS,CAAC,IAAY,EAAE,UAAqC;QAC3D,IAAI,CAAC,4BAA4B,CAAC,aAAa,CAAC,IAAI,CAAC,KAAK,CAAC,EAAE,CAAC;YAC5D,OAAO;QACT,CAAC;QAED,+BAA+B;QAC/B,uBAAA,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","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 * Both `optedIn` and `analyticsId` are persisted (`persist: true`).\n * The platform must supply a valid UUIDv4 `analyticsId` on first run.\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: true,\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 the ID on first run.\n * It is then persisted with controller state when using a persisted store.\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 * The controller persists `optedIn` and `analyticsId` when composed with a persisted\n * store. The platform must supply a valid `analyticsId` on first launch.\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(\n initialState,\n platformAdapter.skipUUIDv4Check === true,\n );\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"]}
|
|
1
|
+
{"version":3,"file":"AnalyticsController.mjs","sourceRoot":"","sources":["../src/AnalyticsController.ts"],"names":[],"mappings":";;;;;;;;;;;;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F","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 AnalyticsContext,\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 * Both `optedIn` and `analyticsId` are persisted (`persist: true`).\n * The platform must supply a valid UUIDv4 `analyticsId` on first run.\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: true,\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 the ID on first run.\n * It is then persisted with controller state when using a persisted store.\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 * The controller persists `optedIn` and `analyticsId` when composed with a persisted\n * store. The platform must supply a valid `analyticsId` on first launch.\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(\n initialState,\n platformAdapter.skipUUIDv4Check === true,\n );\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 * @param context - Optional platform-specific context forwarded to the platform adapter.\n */\n trackEvent(event: AnalyticsTrackingEvent, context?: AnalyticsContext): 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 if (context) {\n this.#platformAdapter.track(event.name, undefined, context);\n } else {\n this.#platformAdapter.track(event.name);\n }\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 const properties = {\n ...event.properties,\n };\n if (context) {\n this.#platformAdapter.track(event.name, properties, context);\n } else {\n this.#platformAdapter.track(event.name, properties);\n }\n }\n\n const hasSensitiveProperties =\n Object.keys(event.sensitiveProperties).length > 0;\n\n if (!this.#isAnonymousEventsFeatureEnabled || hasSensitiveProperties) {\n const properties = {\n ...event.properties,\n ...event.sensitiveProperties,\n ...(hasSensitiveProperties && { anonymous: true }),\n };\n if (context) {\n this.#platformAdapter.track(event.name, properties, context);\n } else {\n this.#platformAdapter.track(event.name, properties);\n }\n }\n }\n\n /**\n * Identify a user for analytics.\n *\n * @param traits - User traits/properties\n * @param context - Optional platform-specific context forwarded to the platform adapter.\n */\n identify(traits?: AnalyticsUserTraits, context?: AnalyticsContext): void {\n if (!analyticsControllerSelectors.selectEnabled(this.state)) {\n return;\n }\n\n // Delegate to platform adapter using the current analytics ID\n if (context) {\n this.#platformAdapter.identify(this.state.analyticsId, traits, context);\n } else {\n this.#platformAdapter.identify(this.state.analyticsId, traits);\n }\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 * @param context - Optional platform-specific context forwarded to the platform adapter.\n */\n trackView(\n name: string,\n properties?: AnalyticsEventProperties,\n context?: AnalyticsContext,\n ): void {\n if (!analyticsControllerSelectors.selectEnabled(this.state)) {\n return;\n }\n\n // Delegate to platform adapter\n if (context) {\n this.#platformAdapter.view(name, properties, context);\n } else {\n this.#platformAdapter.view(name, properties);\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 +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 * 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 regular and sensitive data.\n * Similar to ITrackingEvent from legacy analytics but decoupled for platform agnosticism.\n * Sensitivity is derived from the presence of sensitiveProperties (if sensitiveProperties has keys, the event is sensitive).\n */\nexport type AnalyticsTrackingEvent = {\n readonly name: string;\n properties: AnalyticsEventProperties;\n sensitiveProperties: AnalyticsEventProperties;\n /**\n * Legacy property handled by the mobile app.\n * This property is ignored by the analytics controller and will be removed from the type in the future.\n * The mobile app will use the future analytics privacy controller to handle this functionality.\n */\n saveDataRecording: 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 * When `true`, the controller accepts any non-empty `analyticsId` string\n * instead of requiring UUIDv4 format. Defaults to validation against UUIDv4 when omitted or `false`.\n */\n skipUUIDv4Check?: boolean;\n\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. If not provided, the event has no properties.\n * The privacy plugin should check for `isSensitive === true` to determine if an event contains sensitive data.\n */\n track(eventName: string
|
|
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 * 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 regular and sensitive data.\n * Similar to ITrackingEvent from legacy analytics but decoupled for platform agnosticism.\n * Sensitivity is derived from the presence of sensitiveProperties (if sensitiveProperties has keys, the event is sensitive).\n */\nexport type AnalyticsTrackingEvent = {\n readonly name: string;\n properties: AnalyticsEventProperties;\n sensitiveProperties: AnalyticsEventProperties;\n /**\n * Legacy property handled by the mobile app.\n * This property is ignored by the analytics controller and will be removed from the type in the future.\n * The mobile app will use the future analytics privacy controller to handle this functionality.\n */\n saveDataRecording: boolean;\n readonly hasProperties: boolean;\n};\n\n/**\n * Optional analytics context payload (for example Segment-style context).\n */\nexport type AnalyticsContext = Record<string, Json>;\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 * When `true`, the controller accepts any non-empty `analyticsId` string\n * instead of requiring UUIDv4 format. Defaults to validation against UUIDv4 when omitted or `false`.\n */\n skipUUIDv4Check?: boolean;\n\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. If not provided, the event has no properties.\n * The privacy plugin should check for `isSensitive === true` to determine if an event contains sensitive data.\n * @param context - Optional platform-specific context attached to the invocation.\n */\n track(\n eventName: string,\n properties?: AnalyticsEventProperties,\n context?: AnalyticsContext,\n ): 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 * @param context - Optional platform-specific context attached to the invocation.\n */\n identify(\n userId: string,\n traits?: AnalyticsUserTraits,\n context?: AnalyticsContext,\n ): void;\n\n /**\n * Track a UI unit (page or screen) view depending on the platform\n *\n * This method delegates to platform-specific Segment SDK methods:\n * - Web adapters should call `analytics.page(name, properties)`\n * - Mobile adapters should call `analytics.screen(name, properties)`\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 * @param context - Optional platform-specific context attached to the invocation.\n */\n view(\n name: string,\n properties?: AnalyticsEventProperties,\n context?: AnalyticsContext,\n ): 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"]}
|
|
@@ -24,6 +24,10 @@ export type AnalyticsTrackingEvent = {
|
|
|
24
24
|
saveDataRecording: boolean;
|
|
25
25
|
readonly hasProperties: boolean;
|
|
26
26
|
};
|
|
27
|
+
/**
|
|
28
|
+
* Optional analytics context payload (for example Segment-style context).
|
|
29
|
+
*/
|
|
30
|
+
export type AnalyticsContext = Record<string, Json>;
|
|
27
31
|
/**
|
|
28
32
|
* Platform adapter interface for analytics tracking
|
|
29
33
|
* Implementations should handle platform-specific details (Segment SDK, etc.)
|
|
@@ -42,15 +46,17 @@ export type AnalyticsPlatformAdapter = {
|
|
|
42
46
|
* @param eventName - The name of the event
|
|
43
47
|
* @param properties - Event properties. If not provided, the event has no properties.
|
|
44
48
|
* The privacy plugin should check for `isSensitive === true` to determine if an event contains sensitive data.
|
|
49
|
+
* @param context - Optional platform-specific context attached to the invocation.
|
|
45
50
|
*/
|
|
46
|
-
track(eventName: string, properties?: AnalyticsEventProperties): void;
|
|
51
|
+
track(eventName: string, properties?: AnalyticsEventProperties, context?: AnalyticsContext): void;
|
|
47
52
|
/**
|
|
48
53
|
* Identify a user with traits.
|
|
49
54
|
*
|
|
50
55
|
* @param userId - The user identifier (e.g., metametrics ID)
|
|
51
56
|
* @param traits - User traits/properties
|
|
57
|
+
* @param context - Optional platform-specific context attached to the invocation.
|
|
52
58
|
*/
|
|
53
|
-
identify(userId: string, traits?: AnalyticsUserTraits): void;
|
|
59
|
+
identify(userId: string, traits?: AnalyticsUserTraits, context?: AnalyticsContext): void;
|
|
54
60
|
/**
|
|
55
61
|
* Track a UI unit (page or screen) view depending on the platform
|
|
56
62
|
*
|
|
@@ -60,8 +66,9 @@ export type AnalyticsPlatformAdapter = {
|
|
|
60
66
|
*
|
|
61
67
|
* @param name - The identifier/name of the page or screen being viewed (e.g., "home", "settings", "wallet")
|
|
62
68
|
* @param properties - Optional properties associated with the view
|
|
69
|
+
* @param context - Optional platform-specific context attached to the invocation.
|
|
63
70
|
*/
|
|
64
|
-
view(name: string, properties?: AnalyticsEventProperties): void;
|
|
71
|
+
view(name: string, properties?: AnalyticsEventProperties, context?: AnalyticsContext): void;
|
|
65
72
|
/**
|
|
66
73
|
* Lifecycle hook called after the AnalyticsController is fully initialized.
|
|
67
74
|
*
|
|
@@ -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;;GAEG;AACH,MAAM,MAAM,mBAAmB,GAAG,MAAM,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC;AAEvD;;;;GAIG;AACH,MAAM,MAAM,sBAAsB,GAAG;IACnC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,UAAU,EAAE,wBAAwB,CAAC;IACrC,mBAAmB,EAAE,wBAAwB,CAAC;IAC9C;;;;OAIG;IACH,iBAAiB,EAAE,OAAO,CAAC;IAC3B,QAAQ,CAAC,aAAa,EAAE,OAAO,CAAC;CACjC,CAAC;AAEF;;;GAGG;AACH,MAAM,MAAM,wBAAwB,GAAG;IACrC;;;OAGG;IACH,eAAe,CAAC,EAAE,OAAO,CAAC;IAE1B
|
|
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;;GAEG;AACH,MAAM,MAAM,mBAAmB,GAAG,MAAM,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC;AAEvD;;;;GAIG;AACH,MAAM,MAAM,sBAAsB,GAAG;IACnC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,UAAU,EAAE,wBAAwB,CAAC;IACrC,mBAAmB,EAAE,wBAAwB,CAAC;IAC9C;;;;OAIG;IACH,iBAAiB,EAAE,OAAO,CAAC;IAC3B,QAAQ,CAAC,aAAa,EAAE,OAAO,CAAC;CACjC,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,gBAAgB,GAAG,MAAM,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC;AAEpD;;;GAGG;AACH,MAAM,MAAM,wBAAwB,GAAG;IACrC;;;OAGG;IACH,eAAe,CAAC,EAAE,OAAO,CAAC;IAE1B;;;;;;;;;OASG;IACH,KAAK,CACH,SAAS,EAAE,MAAM,EACjB,UAAU,CAAC,EAAE,wBAAwB,EACrC,OAAO,CAAC,EAAE,gBAAgB,GACzB,IAAI,CAAC;IAER;;;;;;OAMG;IACH,QAAQ,CACN,MAAM,EAAE,MAAM,EACd,MAAM,CAAC,EAAE,mBAAmB,EAC5B,OAAO,CAAC,EAAE,gBAAgB,GACzB,IAAI,CAAC;IAER;;;;;;;;;;OAUG;IACH,IAAI,CACF,IAAI,EAAE,MAAM,EACZ,UAAU,CAAC,EAAE,wBAAwB,EACrC,OAAO,CAAC,EAAE,gBAAgB,GACzB,IAAI,CAAC;IAER;;;;;;;;;;;;;;;;;;;;;;;;OAwBG;IACH,gBAAgB,CAAC,WAAW,EAAE,MAAM,GAAG,IAAI,CAAC;CAC7C,CAAC"}
|
|
@@ -24,6 +24,10 @@ export type AnalyticsTrackingEvent = {
|
|
|
24
24
|
saveDataRecording: boolean;
|
|
25
25
|
readonly hasProperties: boolean;
|
|
26
26
|
};
|
|
27
|
+
/**
|
|
28
|
+
* Optional analytics context payload (for example Segment-style context).
|
|
29
|
+
*/
|
|
30
|
+
export type AnalyticsContext = Record<string, Json>;
|
|
27
31
|
/**
|
|
28
32
|
* Platform adapter interface for analytics tracking
|
|
29
33
|
* Implementations should handle platform-specific details (Segment SDK, etc.)
|
|
@@ -42,15 +46,17 @@ export type AnalyticsPlatformAdapter = {
|
|
|
42
46
|
* @param eventName - The name of the event
|
|
43
47
|
* @param properties - Event properties. If not provided, the event has no properties.
|
|
44
48
|
* The privacy plugin should check for `isSensitive === true` to determine if an event contains sensitive data.
|
|
49
|
+
* @param context - Optional platform-specific context attached to the invocation.
|
|
45
50
|
*/
|
|
46
|
-
track(eventName: string, properties?: AnalyticsEventProperties): void;
|
|
51
|
+
track(eventName: string, properties?: AnalyticsEventProperties, context?: AnalyticsContext): void;
|
|
47
52
|
/**
|
|
48
53
|
* Identify a user with traits.
|
|
49
54
|
*
|
|
50
55
|
* @param userId - The user identifier (e.g., metametrics ID)
|
|
51
56
|
* @param traits - User traits/properties
|
|
57
|
+
* @param context - Optional platform-specific context attached to the invocation.
|
|
52
58
|
*/
|
|
53
|
-
identify(userId: string, traits?: AnalyticsUserTraits): void;
|
|
59
|
+
identify(userId: string, traits?: AnalyticsUserTraits, context?: AnalyticsContext): void;
|
|
54
60
|
/**
|
|
55
61
|
* Track a UI unit (page or screen) view depending on the platform
|
|
56
62
|
*
|
|
@@ -60,8 +66,9 @@ export type AnalyticsPlatformAdapter = {
|
|
|
60
66
|
*
|
|
61
67
|
* @param name - The identifier/name of the page or screen being viewed (e.g., "home", "settings", "wallet")
|
|
62
68
|
* @param properties - Optional properties associated with the view
|
|
69
|
+
* @param context - Optional platform-specific context attached to the invocation.
|
|
63
70
|
*/
|
|
64
|
-
view(name: string, properties?: AnalyticsEventProperties): void;
|
|
71
|
+
view(name: string, properties?: AnalyticsEventProperties, context?: AnalyticsContext): void;
|
|
65
72
|
/**
|
|
66
73
|
* Lifecycle hook called after the AnalyticsController is fully initialized.
|
|
67
74
|
*
|
|
@@ -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;;GAEG;AACH,MAAM,MAAM,mBAAmB,GAAG,MAAM,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC;AAEvD;;;;GAIG;AACH,MAAM,MAAM,sBAAsB,GAAG;IACnC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,UAAU,EAAE,wBAAwB,CAAC;IACrC,mBAAmB,EAAE,wBAAwB,CAAC;IAC9C;;;;OAIG;IACH,iBAAiB,EAAE,OAAO,CAAC;IAC3B,QAAQ,CAAC,aAAa,EAAE,OAAO,CAAC;CACjC,CAAC;AAEF;;;GAGG;AACH,MAAM,MAAM,wBAAwB,GAAG;IACrC;;;OAGG;IACH,eAAe,CAAC,EAAE,OAAO,CAAC;IAE1B
|
|
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;;GAEG;AACH,MAAM,MAAM,mBAAmB,GAAG,MAAM,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC;AAEvD;;;;GAIG;AACH,MAAM,MAAM,sBAAsB,GAAG;IACnC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,UAAU,EAAE,wBAAwB,CAAC;IACrC,mBAAmB,EAAE,wBAAwB,CAAC;IAC9C;;;;OAIG;IACH,iBAAiB,EAAE,OAAO,CAAC;IAC3B,QAAQ,CAAC,aAAa,EAAE,OAAO,CAAC;CACjC,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,gBAAgB,GAAG,MAAM,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC;AAEpD;;;GAGG;AACH,MAAM,MAAM,wBAAwB,GAAG;IACrC;;;OAGG;IACH,eAAe,CAAC,EAAE,OAAO,CAAC;IAE1B;;;;;;;;;OASG;IACH,KAAK,CACH,SAAS,EAAE,MAAM,EACjB,UAAU,CAAC,EAAE,wBAAwB,EACrC,OAAO,CAAC,EAAE,gBAAgB,GACzB,IAAI,CAAC;IAER;;;;;;OAMG;IACH,QAAQ,CACN,MAAM,EAAE,MAAM,EACd,MAAM,CAAC,EAAE,mBAAmB,EAC5B,OAAO,CAAC,EAAE,gBAAgB,GACzB,IAAI,CAAC;IAER;;;;;;;;;;OAUG;IACH,IAAI,CACF,IAAI,EAAE,MAAM,EACZ,UAAU,CAAC,EAAE,wBAAwB,EACrC,OAAO,CAAC,EAAE,gBAAgB,GACzB,IAAI,CAAC;IAER;;;;;;;;;;;;;;;;;;;;;;;;OAwBG;IACH,gBAAgB,CAAC,WAAW,EAAE,MAAM,GAAG,IAAI,CAAC;CAC7C,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
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 * 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 regular and sensitive data.\n * Similar to ITrackingEvent from legacy analytics but decoupled for platform agnosticism.\n * Sensitivity is derived from the presence of sensitiveProperties (if sensitiveProperties has keys, the event is sensitive).\n */\nexport type AnalyticsTrackingEvent = {\n readonly name: string;\n properties: AnalyticsEventProperties;\n sensitiveProperties: AnalyticsEventProperties;\n /**\n * Legacy property handled by the mobile app.\n * This property is ignored by the analytics controller and will be removed from the type in the future.\n * The mobile app will use the future analytics privacy controller to handle this functionality.\n */\n saveDataRecording: 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 * When `true`, the controller accepts any non-empty `analyticsId` string\n * instead of requiring UUIDv4 format. Defaults to validation against UUIDv4 when omitted or `false`.\n */\n skipUUIDv4Check?: boolean;\n\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. If not provided, the event has no properties.\n * The privacy plugin should check for `isSensitive === true` to determine if an event contains sensitive data.\n */\n track(eventName: string
|
|
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 * 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 regular and sensitive data.\n * Similar to ITrackingEvent from legacy analytics but decoupled for platform agnosticism.\n * Sensitivity is derived from the presence of sensitiveProperties (if sensitiveProperties has keys, the event is sensitive).\n */\nexport type AnalyticsTrackingEvent = {\n readonly name: string;\n properties: AnalyticsEventProperties;\n sensitiveProperties: AnalyticsEventProperties;\n /**\n * Legacy property handled by the mobile app.\n * This property is ignored by the analytics controller and will be removed from the type in the future.\n * The mobile app will use the future analytics privacy controller to handle this functionality.\n */\n saveDataRecording: boolean;\n readonly hasProperties: boolean;\n};\n\n/**\n * Optional analytics context payload (for example Segment-style context).\n */\nexport type AnalyticsContext = Record<string, Json>;\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 * When `true`, the controller accepts any non-empty `analyticsId` string\n * instead of requiring UUIDv4 format. Defaults to validation against UUIDv4 when omitted or `false`.\n */\n skipUUIDv4Check?: boolean;\n\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. If not provided, the event has no properties.\n * The privacy plugin should check for `isSensitive === true` to determine if an event contains sensitive data.\n * @param context - Optional platform-specific context attached to the invocation.\n */\n track(\n eventName: string,\n properties?: AnalyticsEventProperties,\n context?: AnalyticsContext,\n ): 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 * @param context - Optional platform-specific context attached to the invocation.\n */\n identify(\n userId: string,\n traits?: AnalyticsUserTraits,\n context?: AnalyticsContext,\n ): void;\n\n /**\n * Track a UI unit (page or screen) view depending on the platform\n *\n * This method delegates to platform-specific Segment SDK methods:\n * - Web adapters should call `analytics.page(name, properties)`\n * - Mobile adapters should call `analytics.screen(name, properties)`\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 * @param context - Optional platform-specific context attached to the invocation.\n */\n view(\n name: string,\n properties?: AnalyticsEventProperties,\n context?: AnalyticsContext,\n ): 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"]}
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,8CAA8C;AAC9C,iEAG+B;AAF7B,0HAAA,mBAAmB,OAAA;AACnB,yIAAA,kCAAkC,OAAA;AAIpC,gBAAgB;AAChB,+FAA0F;AAAjF,wJAAA,kCAAkC,OAAA;
|
|
1
|
+
{"version":3,"file":"index.cjs","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,8CAA8C;AAC9C,iEAG+B;AAF7B,0HAAA,mBAAmB,OAAA;AACnB,yIAAA,kCAAkC,OAAA;AAIpC,gBAAgB;AAChB,+FAA0F;AAAjF,wJAAA,kCAAkC,OAAA;AAc3C,mBAAmB;AACnB,6CAA2D;AAAlD,yHAAA,4BAA4B,OAAA","sourcesContent":["// Export controller class and state utilities\nexport {\n AnalyticsController,\n getDefaultAnalyticsControllerState,\n} from './AnalyticsController';\nexport type { AnalyticsControllerOptions } from './AnalyticsController';\n\n// Export errors\nexport { AnalyticsPlatformAdapterSetupError } from './AnalyticsPlatformAdapterSetupError';\n\n// Export types\nexport type {\n AnalyticsContext,\n AnalyticsEventProperties,\n AnalyticsUserTraits,\n AnalyticsPlatformAdapter,\n AnalyticsTrackingEvent,\n} from './AnalyticsPlatformAdapter.types';\n\n// Export state types\nexport type { AnalyticsControllerState } from './AnalyticsController';\n\n// Export selectors\nexport { analyticsControllerSelectors } from './selectors';\n\n// Export messenger types\nexport type { AnalyticsControllerMessenger } from './AnalyticsController';\n\n// Export action and event types\nexport type {\n AnalyticsControllerActions,\n AnalyticsControllerEvents,\n AnalyticsControllerGetStateAction,\n AnalyticsControllerStateChangeEvent,\n} from './AnalyticsController';\nexport type {\n AnalyticsControllerTrackEventAction,\n AnalyticsControllerIdentifyAction,\n AnalyticsControllerTrackViewAction,\n AnalyticsControllerOptInAction,\n AnalyticsControllerOptOutAction,\n AnalyticsControllerMethodActions,\n} from './AnalyticsController-method-action-types';\n"]}
|
package/dist/index.d.cts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
export { AnalyticsController, getDefaultAnalyticsControllerState, } from "./AnalyticsController.cjs";
|
|
2
2
|
export type { AnalyticsControllerOptions } from "./AnalyticsController.cjs";
|
|
3
3
|
export { AnalyticsPlatformAdapterSetupError } from "./AnalyticsPlatformAdapterSetupError.cjs";
|
|
4
|
-
export type { AnalyticsEventProperties, AnalyticsUserTraits, AnalyticsPlatformAdapter, AnalyticsTrackingEvent, } from "./AnalyticsPlatformAdapter.types.cjs";
|
|
4
|
+
export type { AnalyticsContext, AnalyticsEventProperties, AnalyticsUserTraits, AnalyticsPlatformAdapter, AnalyticsTrackingEvent, } from "./AnalyticsPlatformAdapter.types.cjs";
|
|
5
5
|
export type { AnalyticsControllerState } from "./AnalyticsController.cjs";
|
|
6
6
|
export { analyticsControllerSelectors } from "./selectors.cjs";
|
|
7
7
|
export type { AnalyticsControllerMessenger } from "./AnalyticsController.cjs";
|
package/dist/index.d.cts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.cts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EACL,mBAAmB,EACnB,kCAAkC,GACnC,kCAA8B;AAC/B,YAAY,EAAE,0BAA0B,EAAE,kCAA8B;AAGxE,OAAO,EAAE,kCAAkC,EAAE,iDAA6C;AAG1F,YAAY,EACV,wBAAwB,EACxB,mBAAmB,EACnB,wBAAwB,EACxB,sBAAsB,GACvB,6CAAyC;AAG1C,YAAY,EAAE,wBAAwB,EAAE,kCAA8B;AAGtE,OAAO,EAAE,4BAA4B,EAAE,wBAAoB;AAG3D,YAAY,EAAE,4BAA4B,EAAE,kCAA8B;AAG1E,YAAY,EACV,0BAA0B,EAC1B,yBAAyB,EACzB,iCAAiC,EACjC,mCAAmC,GACpC,kCAA8B;AAC/B,YAAY,EACV,mCAAmC,EACnC,iCAAiC,EACjC,kCAAkC,EAClC,8BAA8B,EAC9B,+BAA+B,EAC/B,gCAAgC,GACjC,sDAAkD"}
|
|
1
|
+
{"version":3,"file":"index.d.cts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EACL,mBAAmB,EACnB,kCAAkC,GACnC,kCAA8B;AAC/B,YAAY,EAAE,0BAA0B,EAAE,kCAA8B;AAGxE,OAAO,EAAE,kCAAkC,EAAE,iDAA6C;AAG1F,YAAY,EACV,gBAAgB,EAChB,wBAAwB,EACxB,mBAAmB,EACnB,wBAAwB,EACxB,sBAAsB,GACvB,6CAAyC;AAG1C,YAAY,EAAE,wBAAwB,EAAE,kCAA8B;AAGtE,OAAO,EAAE,4BAA4B,EAAE,wBAAoB;AAG3D,YAAY,EAAE,4BAA4B,EAAE,kCAA8B;AAG1E,YAAY,EACV,0BAA0B,EAC1B,yBAAyB,EACzB,iCAAiC,EACjC,mCAAmC,GACpC,kCAA8B;AAC/B,YAAY,EACV,mCAAmC,EACnC,iCAAiC,EACjC,kCAAkC,EAClC,8BAA8B,EAC9B,+BAA+B,EAC/B,gCAAgC,GACjC,sDAAkD"}
|
package/dist/index.d.mts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
export { AnalyticsController, getDefaultAnalyticsControllerState, } from "./AnalyticsController.mjs";
|
|
2
2
|
export type { AnalyticsControllerOptions } from "./AnalyticsController.mjs";
|
|
3
3
|
export { AnalyticsPlatformAdapterSetupError } from "./AnalyticsPlatformAdapterSetupError.mjs";
|
|
4
|
-
export type { AnalyticsEventProperties, AnalyticsUserTraits, AnalyticsPlatformAdapter, AnalyticsTrackingEvent, } from "./AnalyticsPlatformAdapter.types.mjs";
|
|
4
|
+
export type { AnalyticsContext, AnalyticsEventProperties, AnalyticsUserTraits, AnalyticsPlatformAdapter, AnalyticsTrackingEvent, } from "./AnalyticsPlatformAdapter.types.mjs";
|
|
5
5
|
export type { AnalyticsControllerState } from "./AnalyticsController.mjs";
|
|
6
6
|
export { analyticsControllerSelectors } from "./selectors.mjs";
|
|
7
7
|
export type { AnalyticsControllerMessenger } from "./AnalyticsController.mjs";
|
package/dist/index.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EACL,mBAAmB,EACnB,kCAAkC,GACnC,kCAA8B;AAC/B,YAAY,EAAE,0BAA0B,EAAE,kCAA8B;AAGxE,OAAO,EAAE,kCAAkC,EAAE,iDAA6C;AAG1F,YAAY,EACV,wBAAwB,EACxB,mBAAmB,EACnB,wBAAwB,EACxB,sBAAsB,GACvB,6CAAyC;AAG1C,YAAY,EAAE,wBAAwB,EAAE,kCAA8B;AAGtE,OAAO,EAAE,4BAA4B,EAAE,wBAAoB;AAG3D,YAAY,EAAE,4BAA4B,EAAE,kCAA8B;AAG1E,YAAY,EACV,0BAA0B,EAC1B,yBAAyB,EACzB,iCAAiC,EACjC,mCAAmC,GACpC,kCAA8B;AAC/B,YAAY,EACV,mCAAmC,EACnC,iCAAiC,EACjC,kCAAkC,EAClC,8BAA8B,EAC9B,+BAA+B,EAC/B,gCAAgC,GACjC,sDAAkD"}
|
|
1
|
+
{"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EACL,mBAAmB,EACnB,kCAAkC,GACnC,kCAA8B;AAC/B,YAAY,EAAE,0BAA0B,EAAE,kCAA8B;AAGxE,OAAO,EAAE,kCAAkC,EAAE,iDAA6C;AAG1F,YAAY,EACV,gBAAgB,EAChB,wBAAwB,EACxB,mBAAmB,EACnB,wBAAwB,EACxB,sBAAsB,GACvB,6CAAyC;AAG1C,YAAY,EAAE,wBAAwB,EAAE,kCAA8B;AAGtE,OAAO,EAAE,4BAA4B,EAAE,wBAAoB;AAG3D,YAAY,EAAE,4BAA4B,EAAE,kCAA8B;AAG1E,YAAY,EACV,0BAA0B,EAC1B,yBAAyB,EACzB,iCAAiC,EACjC,mCAAmC,GACpC,kCAA8B;AAC/B,YAAY,EACV,mCAAmC,EACnC,iCAAiC,EACjC,kCAAkC,EAClC,8BAA8B,EAC9B,+BAA+B,EAC/B,gCAAgC,GACjC,sDAAkD"}
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,8CAA8C;AAC9C,OAAO,EACL,mBAAmB,EACnB,kCAAkC,EACnC,kCAA8B;AAG/B,gBAAgB;AAChB,OAAO,EAAE,kCAAkC,EAAE,iDAA6C;
|
|
1
|
+
{"version":3,"file":"index.mjs","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,8CAA8C;AAC9C,OAAO,EACL,mBAAmB,EACnB,kCAAkC,EACnC,kCAA8B;AAG/B,gBAAgB;AAChB,OAAO,EAAE,kCAAkC,EAAE,iDAA6C;AAc1F,mBAAmB;AACnB,OAAO,EAAE,4BAA4B,EAAE,wBAAoB","sourcesContent":["// Export controller class and state utilities\nexport {\n AnalyticsController,\n getDefaultAnalyticsControllerState,\n} from './AnalyticsController';\nexport type { AnalyticsControllerOptions } from './AnalyticsController';\n\n// Export errors\nexport { AnalyticsPlatformAdapterSetupError } from './AnalyticsPlatformAdapterSetupError';\n\n// Export types\nexport type {\n AnalyticsContext,\n AnalyticsEventProperties,\n AnalyticsUserTraits,\n AnalyticsPlatformAdapter,\n AnalyticsTrackingEvent,\n} from './AnalyticsPlatformAdapter.types';\n\n// Export state types\nexport type { AnalyticsControllerState } from './AnalyticsController';\n\n// Export selectors\nexport { analyticsControllerSelectors } from './selectors';\n\n// Export messenger types\nexport type { AnalyticsControllerMessenger } from './AnalyticsController';\n\n// Export action and event types\nexport type {\n AnalyticsControllerActions,\n AnalyticsControllerEvents,\n AnalyticsControllerGetStateAction,\n AnalyticsControllerStateChangeEvent,\n} from './AnalyticsController';\nexport type {\n AnalyticsControllerTrackEventAction,\n AnalyticsControllerIdentifyAction,\n AnalyticsControllerTrackViewAction,\n AnalyticsControllerOptInAction,\n AnalyticsControllerOptOutAction,\n AnalyticsControllerMethodActions,\n} from './AnalyticsController-method-action-types';\n"]}
|