@metamask-previews/analytics-controller 0.0.0-preview-b8454d32

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.
Files changed (46) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/LICENSE +6 -0
  3. package/LICENSE.APACHE2 +201 -0
  4. package/LICENSE.MIT +21 -0
  5. package/README.md +198 -0
  6. package/dist/AnalyticsController-method-action-types.cjs +7 -0
  7. package/dist/AnalyticsController-method-action-types.cjs.map +1 -0
  8. package/dist/AnalyticsController-method-action-types.d.cts +70 -0
  9. package/dist/AnalyticsController-method-action-types.d.cts.map +1 -0
  10. package/dist/AnalyticsController-method-action-types.d.mts +70 -0
  11. package/dist/AnalyticsController-method-action-types.d.mts.map +1 -0
  12. package/dist/AnalyticsController-method-action-types.mjs +6 -0
  13. package/dist/AnalyticsController-method-action-types.mjs.map +1 -0
  14. package/dist/AnalyticsController.cjs +205 -0
  15. package/dist/AnalyticsController.cjs.map +1 -0
  16. package/dist/AnalyticsController.d.cts +140 -0
  17. package/dist/AnalyticsController.d.cts.map +1 -0
  18. package/dist/AnalyticsController.d.mts +140 -0
  19. package/dist/AnalyticsController.d.mts.map +1 -0
  20. package/dist/AnalyticsController.mjs +200 -0
  21. package/dist/AnalyticsController.mjs.map +1 -0
  22. package/dist/AnalyticsLogger.cjs +8 -0
  23. package/dist/AnalyticsLogger.cjs.map +1 -0
  24. package/dist/AnalyticsLogger.d.cts +5 -0
  25. package/dist/AnalyticsLogger.d.cts.map +1 -0
  26. package/dist/AnalyticsLogger.d.mts +5 -0
  27. package/dist/AnalyticsLogger.d.mts.map +1 -0
  28. package/dist/AnalyticsLogger.mjs +5 -0
  29. package/dist/AnalyticsLogger.mjs.map +1 -0
  30. package/dist/AnalyticsPlatformAdapter.types.cjs +3 -0
  31. package/dist/AnalyticsPlatformAdapter.types.cjs.map +1 -0
  32. package/dist/AnalyticsPlatformAdapter.types.d.cts +33 -0
  33. package/dist/AnalyticsPlatformAdapter.types.d.cts.map +1 -0
  34. package/dist/AnalyticsPlatformAdapter.types.d.mts +33 -0
  35. package/dist/AnalyticsPlatformAdapter.types.d.mts.map +1 -0
  36. package/dist/AnalyticsPlatformAdapter.types.mjs +2 -0
  37. package/dist/AnalyticsPlatformAdapter.types.mjs.map +1 -0
  38. package/dist/index.cjs +9 -0
  39. package/dist/index.cjs.map +1 -0
  40. package/dist/index.d.cts +9 -0
  41. package/dist/index.d.cts.map +1 -0
  42. package/dist/index.d.mts +9 -0
  43. package/dist/index.d.mts.map +1 -0
  44. package/dist/index.mjs +4 -0
  45. package/dist/index.mjs.map +1 -0
  46. package/package.json +74 -0
@@ -0,0 +1,205 @@
1
+ "use strict";
2
+ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
3
+ if (kind === "m") throw new TypeError("Private method is not writable");
4
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
5
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
6
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
7
+ };
8
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
9
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
10
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
11
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
12
+ };
13
+ var _AnalyticsController_platformAdapter;
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.AnalyticsController = exports.getDefaultAnalyticsControllerState = exports.controllerName = void 0;
16
+ const base_controller_1 = require("@metamask/base-controller");
17
+ const uuid_1 = require("uuid");
18
+ const AnalyticsLogger_1 = require("./AnalyticsLogger.cjs");
19
+ // === GENERAL ===
20
+ /**
21
+ * The name of the {@link AnalyticsController}, used to namespace the
22
+ * controller's actions and events and to namespace the controller's state data
23
+ * when composed with other controllers.
24
+ */
25
+ exports.controllerName = 'AnalyticsController';
26
+ /**
27
+ * The metadata for each property in {@link AnalyticsControllerState}.
28
+ */
29
+ const analyticsControllerMetadata = {
30
+ enabled: {
31
+ includeInStateLogs: true,
32
+ persist: true,
33
+ includeInDebugSnapshot: true,
34
+ usedInUi: true,
35
+ },
36
+ optedIn: {
37
+ includeInStateLogs: true,
38
+ persist: true,
39
+ includeInDebugSnapshot: true,
40
+ usedInUi: true,
41
+ },
42
+ analyticsId: {
43
+ includeInStateLogs: false,
44
+ persist: true,
45
+ includeInDebugSnapshot: false,
46
+ usedInUi: false,
47
+ },
48
+ };
49
+ /**
50
+ * Constructs the default {@link AnalyticsController} state. This allows
51
+ * consumers to provide a partial state object when initializing the controller
52
+ * and also helps in constructing complete state objects for this controller in
53
+ * tests.
54
+ *
55
+ * @returns The default {@link AnalyticsController} state.
56
+ */
57
+ function getDefaultAnalyticsControllerState() {
58
+ return {
59
+ enabled: true,
60
+ optedIn: false,
61
+ analyticsId: (0, uuid_1.v4)(),
62
+ };
63
+ }
64
+ exports.getDefaultAnalyticsControllerState = getDefaultAnalyticsControllerState;
65
+ // === MESSENGER ===
66
+ const MESSENGER_EXPOSED_METHODS = [
67
+ 'trackEvent',
68
+ 'identify',
69
+ 'trackPage',
70
+ 'enable',
71
+ 'disable',
72
+ 'optIn',
73
+ 'optOut',
74
+ ];
75
+ /**
76
+ * The AnalyticsController manages analytics tracking across platforms (Mobile/Extension).
77
+ * It provides a unified interface for tracking events, identifying users, and managing
78
+ * analytics preferences while delegating platform-specific implementation to an
79
+ * {@link AnalyticsPlatformAdapter}.
80
+ *
81
+ * This controller follows the MetaMask controller pattern and integrates with the
82
+ * messenger system to allow other controllers and components to track analytics events.
83
+ * It delegates platform-specific implementation to an {@link AnalyticsPlatformAdapter}.
84
+ */
85
+ class AnalyticsController extends base_controller_1.BaseController {
86
+ /**
87
+ * Constructs an AnalyticsController instance.
88
+ *
89
+ * @param options - Controller options
90
+ * @param options.state - Initial controller state (defaults from getDefaultAnalyticsControllerState)
91
+ * @param options.messenger - Messenger used to communicate with BaseController
92
+ * @param options.platformAdapter - Platform adapter implementation for tracking
93
+ */
94
+ constructor({ state = {}, messenger, platformAdapter, }) {
95
+ const defaultState = getDefaultAnalyticsControllerState();
96
+ // Use analyticsId from state if provided, otherwise use the one from defaultState
97
+ const analyticsId = state.analyticsId !== undefined
98
+ ? state.analyticsId
99
+ : defaultState.analyticsId;
100
+ const mergedState = {
101
+ ...defaultState,
102
+ ...state,
103
+ analyticsId,
104
+ };
105
+ super({
106
+ name: exports.controllerName,
107
+ metadata: analyticsControllerMetadata,
108
+ state: mergedState,
109
+ messenger,
110
+ });
111
+ _AnalyticsController_platformAdapter.set(this, void 0);
112
+ __classPrivateFieldSet(this, _AnalyticsController_platformAdapter, platformAdapter, "f");
113
+ this.messenger.registerMethodActionHandlers(this, MESSENGER_EXPOSED_METHODS);
114
+ (0, AnalyticsLogger_1.projectLogger)('AnalyticsController initialized and ready', {
115
+ enabled: this.state.enabled,
116
+ optedIn: this.state.optedIn,
117
+ analyticsId: this.state.analyticsId,
118
+ });
119
+ }
120
+ /**
121
+ * Track an analytics event.
122
+ *
123
+ * Events are only tracked if analytics is enabled.
124
+ *
125
+ * @param eventName - The name of the event
126
+ * @param properties - Event properties
127
+ */
128
+ trackEvent(eventName, properties = {}) {
129
+ // Don't track if analytics is disabled
130
+ if (!this.state.enabled) {
131
+ return;
132
+ }
133
+ // Delegate to platform adapter
134
+ __classPrivateFieldGet(this, _AnalyticsController_platformAdapter, "f").trackEvent(eventName, properties);
135
+ }
136
+ /**
137
+ * Identify a user for analytics.
138
+ *
139
+ * @param userId - The user identifier (e.g., metametrics ID)
140
+ * @param traits - User traits/properties
141
+ */
142
+ identify(userId, traits) {
143
+ if (!this.state.enabled) {
144
+ return;
145
+ }
146
+ // Update state with analytics ID
147
+ this.update((state) => {
148
+ state.analyticsId = userId;
149
+ });
150
+ // Delegate to platform adapter if supported
151
+ if (__classPrivateFieldGet(this, _AnalyticsController_platformAdapter, "f").identify) {
152
+ __classPrivateFieldGet(this, _AnalyticsController_platformAdapter, "f").identify(userId, traits);
153
+ }
154
+ }
155
+ /**
156
+ * Track a page view.
157
+ *
158
+ * @param pageName - The name of the page
159
+ * @param properties - Page properties
160
+ */
161
+ trackPage(pageName, properties) {
162
+ if (!this.state.enabled) {
163
+ return;
164
+ }
165
+ // Delegate to platform adapter if supported
166
+ if (__classPrivateFieldGet(this, _AnalyticsController_platformAdapter, "f").trackPage) {
167
+ __classPrivateFieldGet(this, _AnalyticsController_platformAdapter, "f").trackPage(pageName, properties);
168
+ }
169
+ }
170
+ /**
171
+ * Enable analytics tracking.
172
+ */
173
+ enable() {
174
+ this.update((state) => {
175
+ state.enabled = true;
176
+ });
177
+ }
178
+ /**
179
+ * Disable analytics tracking.
180
+ */
181
+ disable() {
182
+ this.update((state) => {
183
+ state.enabled = false;
184
+ });
185
+ }
186
+ /**
187
+ * Opt in to analytics.
188
+ */
189
+ optIn() {
190
+ this.update((state) => {
191
+ state.optedIn = true;
192
+ });
193
+ }
194
+ /**
195
+ * Opt out of analytics.
196
+ */
197
+ optOut() {
198
+ this.update((state) => {
199
+ state.optedIn = false;
200
+ });
201
+ }
202
+ }
203
+ exports.AnalyticsController = AnalyticsController;
204
+ _AnalyticsController_platformAdapter = new WeakMap();
205
+ //# sourceMappingURL=AnalyticsController.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"AnalyticsController.cjs","sourceRoot":"","sources":["../src/AnalyticsController.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAKA,+DAA2D;AAE3D,+BAAoC;AAGpC,2DAAkD;AAMlD,kBAAkB;AAElB;;;;GAIG;AACU,QAAA,cAAc,GAAG,qBAAqB,CAAC;AAwBpD;;GAEG;AACH,MAAM,2BAA2B,GAAG;IAClC,OAAO,EAAE;QACP,kBAAkB,EAAE,IAA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U,CAAC,CAAC;SACvD;IACH,CAAC;IAED;;OAEG;IACH,MAAM;QACJ,IAAI,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE;YACpB,KAAK,CAAC,OAAO,GAAG,IAAI,CAAC;QACvB,CAAC,CAAC,CAAC;IACL,CAAC;IAED;;OAEG;IACH,OAAO;QACL,IAAI,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE;YACpB,KAAK,CAAC,OAAO,GAAG,KAAK,CAAC;QACxB,CAAC,CAAC,CAAC;IACL,CAAC;IAED;;OAEG;IACH,KAAK;QACH,IAAI,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE;YACpB,KAAK,CAAC,OAAO,GAAG,IAAI,CAAC;QACvB,CAAC,CAAC,CAAC;IACL,CAAC;IAED;;OAEG;IACH,MAAM;QACJ,IAAI,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE;YACpB,KAAK,CAAC,OAAO,GAAG,KAAK,CAAC;QACxB,CAAC,CAAC,CAAC;IACL,CAAC;CACF;AApJD,kDAoJC","sourcesContent":["import type {\n ControllerGetStateAction,\n ControllerStateChangeEvent,\n StateMetadata,\n} from '@metamask/base-controller';\nimport { BaseController } from '@metamask/base-controller';\nimport type { Messenger } from '@metamask/messenger';\nimport { v4 as uuidv4 } from 'uuid';\n\nimport type { AnalyticsControllerMethodActions } from './AnalyticsController-method-action-types';\nimport { projectLogger } from './AnalyticsLogger';\nimport type {\n AnalyticsPlatformAdapter,\n AnalyticsEventProperties,\n} from './AnalyticsPlatformAdapter.types';\n\n// === GENERAL ===\n\n/**\n * The name of the {@link AnalyticsController}, used to namespace the\n * controller's actions and events and to namespace the controller's state data\n * when composed with other controllers.\n */\nexport const controllerName = 'AnalyticsController';\n\n// === STATE ===\n\n/**\n * Describes the shape of the state object for {@link AnalyticsController}.\n */\nexport type AnalyticsControllerState = {\n /**\n * Whether analytics tracking is enabled\n */\n enabled: boolean;\n\n /**\n * Whether the user has opted in to analytics\n */\n optedIn: boolean;\n\n /**\n * User's UUIDv4 analytics identifier\n */\n analyticsId: string;\n};\n\n/**\n * The metadata for each property in {@link AnalyticsControllerState}.\n */\nconst analyticsControllerMetadata = {\n enabled: {\n includeInStateLogs: true,\n persist: true,\n includeInDebugSnapshot: true,\n usedInUi: true,\n },\n optedIn: {\n includeInStateLogs: true,\n persist: true,\n includeInDebugSnapshot: true,\n usedInUi: true,\n },\n analyticsId: {\n includeInStateLogs: false,\n persist: true,\n includeInDebugSnapshot: false,\n usedInUi: false,\n },\n} satisfies StateMetadata<AnalyticsControllerState>;\n\n/**\n * Constructs the default {@link AnalyticsController} state. This allows\n * consumers to provide a partial state object when initializing the controller\n * and also helps in constructing complete state objects for this controller in\n * tests.\n *\n * @returns The default {@link AnalyticsController} state.\n */\nexport function getDefaultAnalyticsControllerState(): AnalyticsControllerState {\n return {\n enabled: true,\n optedIn: false,\n analyticsId: uuidv4(),\n };\n}\n\n// === MESSENGER ===\n\nconst MESSENGER_EXPOSED_METHODS = [\n 'trackEvent',\n 'identify',\n 'trackPage',\n 'enable',\n 'disable',\n 'optIn',\n 'optOut',\n] as const;\n\n/**\n * Returns the state of the {@link AnalyticsController}.\n */\nexport type AnalyticsControllerGetStateAction = ControllerGetStateAction<\n typeof controllerName,\n AnalyticsControllerState\n>;\n\n/**\n * Actions that {@link AnalyticsControllerMessenger} exposes to other consumers.\n */\nexport type AnalyticsControllerActions =\n | AnalyticsControllerGetStateAction\n | AnalyticsControllerMethodActions;\n\n/**\n * Actions from other messengers that {@link AnalyticsControllerMessenger} calls.\n */\ntype AllowedActions = never;\n\n/**\n * Event emitted when the state of the {@link AnalyticsController} changes.\n */\nexport type AnalyticsControllerStateChangeEvent = ControllerStateChangeEvent<\n typeof controllerName,\n AnalyticsControllerState\n>;\n\n/**\n * Events that {@link AnalyticsControllerMessenger} exposes to other consumers.\n */\nexport type AnalyticsControllerEvents = AnalyticsControllerStateChangeEvent;\n\n/**\n * Events from other messengers that {@link AnalyticsControllerMessenger} subscribes to.\n */\ntype AllowedEvents = never;\n\n/**\n * The messenger restricted to actions and events accessed by\n * {@link AnalyticsController}.\n */\nexport type AnalyticsControllerMessenger = Messenger<\n typeof controllerName,\n AnalyticsControllerActions | AllowedActions,\n AnalyticsControllerEvents | AllowedEvents\n>;\n\n// === CONTROLLER DEFINITION ===\n\n/**\n * The options that AnalyticsController takes.\n */\nexport type AnalyticsControllerOptions = {\n state?: Partial<AnalyticsControllerState>;\n messenger: AnalyticsControllerMessenger;\n /**\n * Platform adapter implementation for tracking events\n */\n platformAdapter: AnalyticsPlatformAdapter;\n};\n\n/**\n * The AnalyticsController manages analytics tracking across platforms (Mobile/Extension).\n * It provides a unified interface for tracking events, identifying users, and managing\n * analytics preferences while delegating platform-specific implementation to an\n * {@link AnalyticsPlatformAdapter}.\n *\n * This controller follows the MetaMask controller pattern and integrates with the\n * messenger system to allow other controllers and components to track analytics events.\n * It delegates platform-specific implementation to an {@link AnalyticsPlatformAdapter}.\n */\nexport class AnalyticsController extends BaseController<\n 'AnalyticsController',\n AnalyticsControllerState,\n AnalyticsControllerMessenger\n> {\n readonly #platformAdapter: AnalyticsPlatformAdapter;\n\n /**\n * Constructs an AnalyticsController instance.\n *\n * @param options - Controller options\n * @param options.state - Initial controller state (defaults from getDefaultAnalyticsControllerState)\n * @param options.messenger - Messenger used to communicate with BaseController\n * @param options.platformAdapter - Platform adapter implementation for tracking\n */\n constructor({\n state = {},\n messenger,\n platformAdapter,\n }: AnalyticsControllerOptions) {\n const defaultState = getDefaultAnalyticsControllerState();\n // Use analyticsId from state if provided, otherwise use the one from defaultState\n const analyticsId: string =\n state.analyticsId !== undefined\n ? state.analyticsId\n : defaultState.analyticsId;\n const mergedState: AnalyticsControllerState = {\n ...defaultState,\n ...state,\n analyticsId,\n };\n\n super({\n name: controllerName,\n metadata: analyticsControllerMetadata,\n state: mergedState,\n messenger,\n });\n\n this.#platformAdapter = platformAdapter;\n\n this.messenger.registerMethodActionHandlers(\n this,\n MESSENGER_EXPOSED_METHODS,\n );\n\n projectLogger('AnalyticsController initialized and ready', {\n enabled: this.state.enabled,\n optedIn: this.state.optedIn,\n analyticsId: this.state.analyticsId,\n });\n }\n\n /**\n * Track an analytics event.\n *\n * Events are only tracked if analytics is enabled.\n *\n * @param eventName - The name of the event\n * @param properties - Event properties\n */\n trackEvent(\n eventName: string,\n properties: AnalyticsEventProperties = {},\n ): void {\n // Don't track if analytics is disabled\n if (!this.state.enabled) {\n return;\n }\n\n // Delegate to platform adapter\n this.#platformAdapter.trackEvent(eventName, properties);\n }\n\n /**\n * Identify a user for analytics.\n *\n * @param userId - The user identifier (e.g., metametrics ID)\n * @param traits - User traits/properties\n */\n identify(userId: string, traits?: AnalyticsEventProperties): void {\n if (!this.state.enabled) {\n return;\n }\n\n // Update state with analytics ID\n this.update((state) => {\n state.analyticsId = userId;\n });\n\n // Delegate to platform adapter if supported\n if (this.#platformAdapter.identify) {\n this.#platformAdapter.identify(userId, traits);\n }\n }\n\n /**\n * Track a page view.\n *\n * @param pageName - The name of the page\n * @param properties - Page properties\n */\n trackPage(pageName: string, properties?: AnalyticsEventProperties): void {\n if (!this.state.enabled) {\n return;\n }\n\n // Delegate to platform adapter if supported\n if (this.#platformAdapter.trackPage) {\n this.#platformAdapter.trackPage(pageName, properties);\n }\n }\n\n /**\n * Enable analytics tracking.\n */\n enable(): void {\n this.update((state) => {\n state.enabled = true;\n });\n }\n\n /**\n * Disable analytics tracking.\n */\n disable(): void {\n this.update((state) => {\n state.enabled = false;\n });\n }\n\n /**\n * Opt in to analytics.\n */\n optIn(): void {\n this.update((state) => {\n state.optedIn = true;\n });\n }\n\n /**\n * Opt out of analytics.\n */\n optOut(): void {\n this.update((state) => {\n state.optedIn = false;\n });\n }\n}\n"]}
@@ -0,0 +1,140 @@
1
+ import type { ControllerGetStateAction, ControllerStateChangeEvent } from "@metamask/base-controller";
2
+ import { BaseController } from "@metamask/base-controller";
3
+ import type { Messenger } from "@metamask/messenger";
4
+ import type { AnalyticsControllerMethodActions } from "./AnalyticsController-method-action-types.cjs";
5
+ import type { AnalyticsPlatformAdapter, AnalyticsEventProperties } from "./AnalyticsPlatformAdapter.types.cjs";
6
+ /**
7
+ * The name of the {@link AnalyticsController}, used to namespace the
8
+ * controller's actions and events and to namespace the controller's state data
9
+ * when composed with other controllers.
10
+ */
11
+ export declare const controllerName = "AnalyticsController";
12
+ /**
13
+ * Describes the shape of the state object for {@link AnalyticsController}.
14
+ */
15
+ export type AnalyticsControllerState = {
16
+ /**
17
+ * Whether analytics tracking is enabled
18
+ */
19
+ enabled: boolean;
20
+ /**
21
+ * Whether the user has opted in to analytics
22
+ */
23
+ optedIn: boolean;
24
+ /**
25
+ * User's UUIDv4 analytics identifier
26
+ */
27
+ analyticsId: string;
28
+ };
29
+ /**
30
+ * Constructs the default {@link AnalyticsController} state. This allows
31
+ * consumers to provide a partial state object when initializing the controller
32
+ * and also helps in constructing complete state objects for this controller in
33
+ * tests.
34
+ *
35
+ * @returns The default {@link AnalyticsController} state.
36
+ */
37
+ export declare function getDefaultAnalyticsControllerState(): AnalyticsControllerState;
38
+ /**
39
+ * Returns the state of the {@link AnalyticsController}.
40
+ */
41
+ export type AnalyticsControllerGetStateAction = ControllerGetStateAction<typeof controllerName, AnalyticsControllerState>;
42
+ /**
43
+ * Actions that {@link AnalyticsControllerMessenger} exposes to other consumers.
44
+ */
45
+ export type AnalyticsControllerActions = AnalyticsControllerGetStateAction | AnalyticsControllerMethodActions;
46
+ /**
47
+ * Actions from other messengers that {@link AnalyticsControllerMessenger} calls.
48
+ */
49
+ type AllowedActions = never;
50
+ /**
51
+ * Event emitted when the state of the {@link AnalyticsController} changes.
52
+ */
53
+ export type AnalyticsControllerStateChangeEvent = ControllerStateChangeEvent<typeof controllerName, AnalyticsControllerState>;
54
+ /**
55
+ * Events that {@link AnalyticsControllerMessenger} exposes to other consumers.
56
+ */
57
+ export type AnalyticsControllerEvents = AnalyticsControllerStateChangeEvent;
58
+ /**
59
+ * Events from other messengers that {@link AnalyticsControllerMessenger} subscribes to.
60
+ */
61
+ type AllowedEvents = never;
62
+ /**
63
+ * The messenger restricted to actions and events accessed by
64
+ * {@link AnalyticsController}.
65
+ */
66
+ export type AnalyticsControllerMessenger = Messenger<typeof controllerName, AnalyticsControllerActions | AllowedActions, AnalyticsControllerEvents | AllowedEvents>;
67
+ /**
68
+ * The options that AnalyticsController takes.
69
+ */
70
+ export type AnalyticsControllerOptions = {
71
+ state?: Partial<AnalyticsControllerState>;
72
+ messenger: AnalyticsControllerMessenger;
73
+ /**
74
+ * Platform adapter implementation for tracking events
75
+ */
76
+ platformAdapter: AnalyticsPlatformAdapter;
77
+ };
78
+ /**
79
+ * The AnalyticsController manages analytics tracking across platforms (Mobile/Extension).
80
+ * It provides a unified interface for tracking events, identifying users, and managing
81
+ * analytics preferences while delegating platform-specific implementation to an
82
+ * {@link AnalyticsPlatformAdapter}.
83
+ *
84
+ * This controller follows the MetaMask controller pattern and integrates with the
85
+ * messenger system to allow other controllers and components to track analytics events.
86
+ * It delegates platform-specific implementation to an {@link AnalyticsPlatformAdapter}.
87
+ */
88
+ export declare class AnalyticsController extends BaseController<'AnalyticsController', AnalyticsControllerState, AnalyticsControllerMessenger> {
89
+ #private;
90
+ /**
91
+ * Constructs an AnalyticsController instance.
92
+ *
93
+ * @param options - Controller options
94
+ * @param options.state - Initial controller state (defaults from getDefaultAnalyticsControllerState)
95
+ * @param options.messenger - Messenger used to communicate with BaseController
96
+ * @param options.platformAdapter - Platform adapter implementation for tracking
97
+ */
98
+ constructor({ state, messenger, platformAdapter, }: AnalyticsControllerOptions);
99
+ /**
100
+ * Track an analytics event.
101
+ *
102
+ * Events are only tracked if analytics is enabled.
103
+ *
104
+ * @param eventName - The name of the event
105
+ * @param properties - Event properties
106
+ */
107
+ trackEvent(eventName: string, properties?: AnalyticsEventProperties): void;
108
+ /**
109
+ * Identify a user for analytics.
110
+ *
111
+ * @param userId - The user identifier (e.g., metametrics ID)
112
+ * @param traits - User traits/properties
113
+ */
114
+ identify(userId: string, traits?: AnalyticsEventProperties): void;
115
+ /**
116
+ * Track a page view.
117
+ *
118
+ * @param pageName - The name of the page
119
+ * @param properties - Page properties
120
+ */
121
+ trackPage(pageName: string, properties?: AnalyticsEventProperties): void;
122
+ /**
123
+ * Enable analytics tracking.
124
+ */
125
+ enable(): void;
126
+ /**
127
+ * Disable analytics tracking.
128
+ */
129
+ disable(): void;
130
+ /**
131
+ * Opt in to analytics.
132
+ */
133
+ optIn(): void;
134
+ /**
135
+ * Opt out of analytics.
136
+ */
137
+ optOut(): void;
138
+ }
139
+ export {};
140
+ //# sourceMappingURL=AnalyticsController.d.cts.map
@@ -0,0 +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;AAGrD,OAAO,KAAK,EAAE,gCAAgC,EAAE,sDAAkD;AAElG,OAAO,KAAK,EACV,wBAAwB,EACxB,wBAAwB,EACzB,6CAAyC;AAI1C;;;;GAIG;AACH,eAAO,MAAM,cAAc,wBAAwB,CAAC;AAIpD;;GAEG;AACH,MAAM,MAAM,wBAAwB,GAAG;IACrC;;OAEG;IACH,OAAO,EAAE,OAAO,CAAC;IAEjB;;OAEG;IACH,OAAO,EAAE,OAAO,CAAC;IAEjB;;OAEG;IACH,WAAW,EAAE,MAAM,CAAC;CACrB,CAAC;AA0BF;;;;;;;GAOG;AACH,wBAAgB,kCAAkC,IAAI,wBAAwB,CAM7E;AAcD;;GAEG;AACH,MAAM,MAAM,iCAAiC,GAAG,wBAAwB,CACtE,OAAO,cAAc,EACrB,wBAAwB,CACzB,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,0BAA0B,GAClC,iCAAiC,GACjC,gCAAgC,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+1,140 @@
1
+ import type { ControllerGetStateAction, ControllerStateChangeEvent } from "@metamask/base-controller";
2
+ import { BaseController } from "@metamask/base-controller";
3
+ import type { Messenger } from "@metamask/messenger";
4
+ import type { AnalyticsControllerMethodActions } from "./AnalyticsController-method-action-types.mjs";
5
+ import type { AnalyticsPlatformAdapter, AnalyticsEventProperties } from "./AnalyticsPlatformAdapter.types.mjs";
6
+ /**
7
+ * The name of the {@link AnalyticsController}, used to namespace the
8
+ * controller's actions and events and to namespace the controller's state data
9
+ * when composed with other controllers.
10
+ */
11
+ export declare const controllerName = "AnalyticsController";
12
+ /**
13
+ * Describes the shape of the state object for {@link AnalyticsController}.
14
+ */
15
+ export type AnalyticsControllerState = {
16
+ /**
17
+ * Whether analytics tracking is enabled
18
+ */
19
+ enabled: boolean;
20
+ /**
21
+ * Whether the user has opted in to analytics
22
+ */
23
+ optedIn: boolean;
24
+ /**
25
+ * User's UUIDv4 analytics identifier
26
+ */
27
+ analyticsId: string;
28
+ };
29
+ /**
30
+ * Constructs the default {@link AnalyticsController} state. This allows
31
+ * consumers to provide a partial state object when initializing the controller
32
+ * and also helps in constructing complete state objects for this controller in
33
+ * tests.
34
+ *
35
+ * @returns The default {@link AnalyticsController} state.
36
+ */
37
+ export declare function getDefaultAnalyticsControllerState(): AnalyticsControllerState;
38
+ /**
39
+ * Returns the state of the {@link AnalyticsController}.
40
+ */
41
+ export type AnalyticsControllerGetStateAction = ControllerGetStateAction<typeof controllerName, AnalyticsControllerState>;
42
+ /**
43
+ * Actions that {@link AnalyticsControllerMessenger} exposes to other consumers.
44
+ */
45
+ export type AnalyticsControllerActions = AnalyticsControllerGetStateAction | AnalyticsControllerMethodActions;
46
+ /**
47
+ * Actions from other messengers that {@link AnalyticsControllerMessenger} calls.
48
+ */
49
+ type AllowedActions = never;
50
+ /**
51
+ * Event emitted when the state of the {@link AnalyticsController} changes.
52
+ */
53
+ export type AnalyticsControllerStateChangeEvent = ControllerStateChangeEvent<typeof controllerName, AnalyticsControllerState>;
54
+ /**
55
+ * Events that {@link AnalyticsControllerMessenger} exposes to other consumers.
56
+ */
57
+ export type AnalyticsControllerEvents = AnalyticsControllerStateChangeEvent;
58
+ /**
59
+ * Events from other messengers that {@link AnalyticsControllerMessenger} subscribes to.
60
+ */
61
+ type AllowedEvents = never;
62
+ /**
63
+ * The messenger restricted to actions and events accessed by
64
+ * {@link AnalyticsController}.
65
+ */
66
+ export type AnalyticsControllerMessenger = Messenger<typeof controllerName, AnalyticsControllerActions | AllowedActions, AnalyticsControllerEvents | AllowedEvents>;
67
+ /**
68
+ * The options that AnalyticsController takes.
69
+ */
70
+ export type AnalyticsControllerOptions = {
71
+ state?: Partial<AnalyticsControllerState>;
72
+ messenger: AnalyticsControllerMessenger;
73
+ /**
74
+ * Platform adapter implementation for tracking events
75
+ */
76
+ platformAdapter: AnalyticsPlatformAdapter;
77
+ };
78
+ /**
79
+ * The AnalyticsController manages analytics tracking across platforms (Mobile/Extension).
80
+ * It provides a unified interface for tracking events, identifying users, and managing
81
+ * analytics preferences while delegating platform-specific implementation to an
82
+ * {@link AnalyticsPlatformAdapter}.
83
+ *
84
+ * This controller follows the MetaMask controller pattern and integrates with the
85
+ * messenger system to allow other controllers and components to track analytics events.
86
+ * It delegates platform-specific implementation to an {@link AnalyticsPlatformAdapter}.
87
+ */
88
+ export declare class AnalyticsController extends BaseController<'AnalyticsController', AnalyticsControllerState, AnalyticsControllerMessenger> {
89
+ #private;
90
+ /**
91
+ * Constructs an AnalyticsController instance.
92
+ *
93
+ * @param options - Controller options
94
+ * @param options.state - Initial controller state (defaults from getDefaultAnalyticsControllerState)
95
+ * @param options.messenger - Messenger used to communicate with BaseController
96
+ * @param options.platformAdapter - Platform adapter implementation for tracking
97
+ */
98
+ constructor({ state, messenger, platformAdapter, }: AnalyticsControllerOptions);
99
+ /**
100
+ * Track an analytics event.
101
+ *
102
+ * Events are only tracked if analytics is enabled.
103
+ *
104
+ * @param eventName - The name of the event
105
+ * @param properties - Event properties
106
+ */
107
+ trackEvent(eventName: string, properties?: AnalyticsEventProperties): void;
108
+ /**
109
+ * Identify a user for analytics.
110
+ *
111
+ * @param userId - The user identifier (e.g., metametrics ID)
112
+ * @param traits - User traits/properties
113
+ */
114
+ identify(userId: string, traits?: AnalyticsEventProperties): void;
115
+ /**
116
+ * Track a page view.
117
+ *
118
+ * @param pageName - The name of the page
119
+ * @param properties - Page properties
120
+ */
121
+ trackPage(pageName: string, properties?: AnalyticsEventProperties): void;
122
+ /**
123
+ * Enable analytics tracking.
124
+ */
125
+ enable(): void;
126
+ /**
127
+ * Disable analytics tracking.
128
+ */
129
+ disable(): void;
130
+ /**
131
+ * Opt in to analytics.
132
+ */
133
+ optIn(): void;
134
+ /**
135
+ * Opt out of analytics.
136
+ */
137
+ optOut(): void;
138
+ }
139
+ export {};
140
+ //# sourceMappingURL=AnalyticsController.d.mts.map
@@ -0,0 +1 @@
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;AAGrD,OAAO,KAAK,EAAE,gCAAgC,EAAE,sDAAkD;AAElG,OAAO,KAAK,EACV,wBAAwB,EACxB,wBAAwB,EACzB,6CAAyC;AAI1C;;;;GAIG;AACH,eAAO,MAAM,cAAc,wBAAwB,CAAC;AAIpD;;GAEG;AACH,MAAM,MAAM,wBAAwB,GAAG;IACrC;;OAEG;IACH,OAAO,EAAE,OAAO,CAAC;IAEjB;;OAEG;IACH,OAAO,EAAE,OAAO,CAAC;IAEjB;;OAEG;IACH,WAAW,EAAE,MAAM,CAAC;CACrB,CAAC;AA0BF;;;;;;;GAOG;AACH,wBAAgB,kCAAkC,IAAI,wBAAwB,CAM7E;AAcD;;GAEG;AACH,MAAM,MAAM,iCAA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