@metamask-previews/analytics-controller 0.0.0-preview-40ec088b → 0.0.0-preview-21a5ddac
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +72 -11
- package/dist/AnalyticsController-method-action-types.cjs.map +1 -1
- package/dist/AnalyticsController-method-action-types.d.cts +28 -26
- package/dist/AnalyticsController-method-action-types.d.cts.map +1 -1
- package/dist/AnalyticsController-method-action-types.d.mts +28 -26
- package/dist/AnalyticsController-method-action-types.d.mts.map +1 -1
- package/dist/AnalyticsController-method-action-types.mjs.map +1 -1
- package/dist/AnalyticsController.cjs +81 -52
- package/dist/AnalyticsController.cjs.map +1 -1
- package/dist/AnalyticsController.d.cts +27 -24
- package/dist/AnalyticsController.d.cts.map +1 -1
- package/dist/AnalyticsController.d.mts +27 -24
- package/dist/AnalyticsController.d.mts.map +1 -1
- package/dist/AnalyticsController.mjs +81 -52
- package/dist/AnalyticsController.mjs.map +1 -1
- package/dist/AnalyticsPlatformAdapter.types.cjs.map +1 -1
- package/dist/AnalyticsPlatformAdapter.types.d.cts +58 -15
- package/dist/AnalyticsPlatformAdapter.types.d.cts.map +1 -1
- package/dist/AnalyticsPlatformAdapter.types.d.mts +58 -15
- package/dist/AnalyticsPlatformAdapter.types.d.mts.map +1 -1
- package/dist/AnalyticsPlatformAdapter.types.mjs.map +1 -1
- package/dist/AnalyticsPlatformAdapterSetupError.cjs +17 -0
- package/dist/AnalyticsPlatformAdapterSetupError.cjs.map +1 -0
- package/dist/AnalyticsPlatformAdapterSetupError.d.cts +8 -0
- package/dist/AnalyticsPlatformAdapterSetupError.d.cts.map +1 -0
- package/dist/AnalyticsPlatformAdapterSetupError.d.mts +8 -0
- package/dist/AnalyticsPlatformAdapterSetupError.d.mts.map +1 -0
- package/dist/AnalyticsPlatformAdapterSetupError.mjs +13 -0
- package/dist/AnalyticsPlatformAdapterSetupError.mjs.map +1 -0
- package/dist/analyticsStateComputer.cjs +46 -0
- package/dist/analyticsStateComputer.cjs.map +1 -0
- package/dist/analyticsStateComputer.d.cts +35 -0
- package/dist/analyticsStateComputer.d.cts.map +1 -0
- package/dist/analyticsStateComputer.d.mts +35 -0
- package/dist/analyticsStateComputer.d.mts.map +1 -0
- package/dist/analyticsStateComputer.mjs +42 -0
- package/dist/analyticsStateComputer.mjs.map +1 -0
- package/dist/analyticsStateValidator.cjs +19 -0
- package/dist/analyticsStateValidator.cjs.map +1 -0
- package/dist/analyticsStateValidator.d.cts +9 -0
- package/dist/analyticsStateValidator.d.cts.map +1 -0
- package/dist/analyticsStateValidator.d.mts +9 -0
- package/dist/analyticsStateValidator.d.mts.map +1 -0
- package/dist/analyticsStateValidator.mjs +15 -0
- package/dist/analyticsStateValidator.mjs.map +1 -0
- package/dist/index.cjs +7 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +4 -2
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +4 -2
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +4 -0
- package/dist/index.mjs.map +1 -1
- package/dist/selectors.cjs +44 -0
- package/dist/selectors.cjs.map +1 -0
- package/dist/selectors.d.cts +12 -0
- package/dist/selectors.d.cts.map +1 -0
- package/dist/selectors.d.mts +12 -0
- package/dist/selectors.d.mts.map +1 -0
- package/dist/selectors.mjs +41 -0
- package/dist/selectors.mjs.map +1 -0
- package/package.json +1 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AnalyticsController.cjs","sourceRoot":"","sources":["../src/AnalyticsController.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAKA,+DAA2D;AAE3D,+BAAoC;AAGpC,2DAAkD;AAMlD,kBAAkB;AAElB;;;;GAIG;AACU,QAAA,cAAc,GAAG,qBAAqB,CAAC;AAwBpD;;GAEG;AACH,MAAM,2BAA2B,GAAG;IAClC,OAAO,EAAE;QACP,kBAAkB,EAAE,IAAI;QACxB,OAAO,EAAE,IAAI;QACb,sBAAsB,EAAE,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CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE;YACpB,KAAK,CAAC,OAAO,GAAG,KAAK,CAAC;QACxB,CAAC,CAAC,CAAC;IACL,CAAC;IAED;;OAEG;IACH,KAAK;QACH,IAAI,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE;YACpB,KAAK,CAAC,OAAO,GAAG,IAAI,CAAC;QACvB,CAAC,CAAC,CAAC;IACL,CAAC;IAED;;OAEG;IACH,MAAM;QACJ,IAAI,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE;YACpB,KAAK,CAAC,OAAO,GAAG,KAAK,CAAC;QACxB,CAAC,CAAC,CAAC;IACL,CAAC;CACF;AA3ID,kDA2IC","sourcesContent":["import type {\n ControllerGetStateAction,\n ControllerStateChangeEvent,\n StateMetadata,\n} from '@metamask/base-controller';\nimport { BaseController } from '@metamask/base-controller';\nimport type { Messenger } from '@metamask/messenger';\nimport { v4 as uuidv4 } from 'uuid';\n\nimport type { AnalyticsControllerMethodActions } from './AnalyticsController-method-action-types';\nimport { projectLogger } from './AnalyticsLogger';\nimport type {\n AnalyticsPlatformAdapter,\n AnalyticsEventProperties,\n} from './AnalyticsPlatformAdapter.types';\n\n// === GENERAL ===\n\n/**\n * The name of the {@link AnalyticsController}, used to namespace the\n * controller's actions and events and to namespace the controller's state data\n * when composed with other controllers.\n */\nexport const controllerName = 'AnalyticsController';\n\n// === STATE ===\n\n/**\n * Describes the shape of the state object for {@link AnalyticsController}.\n */\nexport type AnalyticsControllerState = {\n /**\n * Whether analytics tracking is enabled\n */\n enabled: boolean;\n\n /**\n * Whether the user has opted in to analytics\n */\n optedIn: boolean;\n\n /**\n * User's UUIDv4 analytics identifier\n */\n analyticsId: string;\n};\n\n/**\n * The metadata for each property in {@link AnalyticsControllerState}.\n */\nconst analyticsControllerMetadata = {\n enabled: {\n includeInStateLogs: true,\n persist: true,\n includeInDebugSnapshot: true,\n usedInUi: true,\n },\n optedIn: {\n includeInStateLogs: true,\n persist: true,\n includeInDebugSnapshot: true,\n usedInUi: true,\n },\n analyticsId: {\n includeInStateLogs: true,\n persist: true,\n includeInDebugSnapshot: true,\n usedInUi: false,\n },\n} satisfies StateMetadata<AnalyticsControllerState>;\n\n/**\n * Constructs the default {@link AnalyticsController} state. This allows\n * consumers to provide a partial state object when initializing the controller\n * and also helps in constructing complete state objects for this controller in\n * tests.\n *\n * @returns The default {@link AnalyticsController} state.\n */\nexport function getDefaultAnalyticsControllerState(): AnalyticsControllerState {\n return {\n enabled: true,\n optedIn: false,\n analyticsId: uuidv4(),\n };\n}\n\n// === MESSENGER ===\n\nconst MESSENGER_EXPOSED_METHODS = [\n 'trackEvent',\n 'identify',\n 'trackPage',\n 'enable',\n 'disable',\n 'optIn',\n 'optOut',\n] as const;\n\n/**\n * Returns the state of the {@link AnalyticsController}.\n */\nexport type AnalyticsControllerGetStateAction = ControllerGetStateAction<\n typeof controllerName,\n AnalyticsControllerState\n>;\n\n/**\n * Actions that {@link AnalyticsControllerMessenger} exposes to other consumers.\n */\nexport type AnalyticsControllerActions =\n | AnalyticsControllerGetStateAction\n | AnalyticsControllerMethodActions;\n\n/**\n * Actions from other messengers that {@link AnalyticsControllerMessenger} calls.\n */\ntype AllowedActions = never;\n\n/**\n * Event emitted when the state of the {@link AnalyticsController} changes.\n */\nexport type AnalyticsControllerStateChangeEvent = ControllerStateChangeEvent<\n typeof controllerName,\n AnalyticsControllerState\n>;\n\n/**\n * Events that {@link AnalyticsControllerMessenger} exposes to other consumers.\n */\nexport type AnalyticsControllerEvents = AnalyticsControllerStateChangeEvent;\n\n/**\n * Events from other messengers that {@link AnalyticsControllerMessenger} subscribes to.\n */\ntype AllowedEvents = never;\n\n/**\n * The messenger restricted to actions and events accessed by\n * {@link AnalyticsController}.\n */\nexport type AnalyticsControllerMessenger = Messenger<\n typeof controllerName,\n AnalyticsControllerActions | AllowedActions,\n AnalyticsControllerEvents | AllowedEvents\n>;\n\n// === CONTROLLER DEFINITION ===\n\n/**\n * The options that AnalyticsController takes.\n */\nexport type AnalyticsControllerOptions = {\n state?: Partial<AnalyticsControllerState>;\n messenger: AnalyticsControllerMessenger;\n /**\n * Platform adapter implementation for tracking events\n */\n platformAdapter: AnalyticsPlatformAdapter;\n};\n\n/**\n * The AnalyticsController manages analytics tracking across platforms (Mobile/Extension).\n * It provides a unified interface for tracking events, identifying users, and managing\n * analytics preferences while delegating platform-specific implementation to an\n * {@link AnalyticsPlatformAdapter}.\n *\n * This controller follows the MetaMask controller pattern and integrates with the\n * messenger system to allow other controllers and components to track analytics events.\n * It delegates platform-specific implementation to an {@link AnalyticsPlatformAdapter}.\n */\nexport class AnalyticsController extends BaseController<\n 'AnalyticsController',\n AnalyticsControllerState,\n AnalyticsControllerMessenger\n> {\n readonly #platformAdapter: AnalyticsPlatformAdapter;\n\n /**\n * Constructs an AnalyticsController instance.\n *\n * @param options - Controller options\n * @param options.state - Initial controller state (defaults from getDefaultAnalyticsControllerState)\n * @param options.messenger - Messenger used to communicate with BaseController\n * @param options.platformAdapter - Platform adapter implementation for tracking\n */\n constructor({\n state = {},\n messenger,\n platformAdapter,\n }: AnalyticsControllerOptions) {\n super({\n name: controllerName,\n metadata: analyticsControllerMetadata,\n state: {\n ...getDefaultAnalyticsControllerState(),\n ...state,\n },\n messenger,\n });\n\n this.#platformAdapter = platformAdapter;\n\n this.messenger.registerMethodActionHandlers(\n this,\n MESSENGER_EXPOSED_METHODS,\n );\n\n projectLogger('AnalyticsController initialized and ready', {\n enabled: this.state.enabled,\n optedIn: this.state.optedIn,\n analyticsId: this.state.analyticsId,\n });\n }\n\n /**\n * Track an analytics event.\n *\n * Events are only tracked if analytics is enabled.\n *\n * @param eventName - The name of the event\n * @param properties - Event properties\n */\n trackEvent(\n eventName: string,\n properties: AnalyticsEventProperties = {},\n ): void {\n // Don't track if analytics is disabled\n if (!this.state.enabled) {\n return;\n }\n\n // Delegate to platform adapter\n this.#platformAdapter.trackEvent(eventName, properties);\n }\n\n /**\n * Identify a user for analytics.\n *\n * @param userId - The user identifier (e.g., metametrics ID)\n * @param traits - User traits/properties\n */\n identify(userId: string, traits?: AnalyticsEventProperties): void {\n if (!this.state.enabled) {\n return;\n }\n\n // Update state with analytics ID\n this.update((state) => {\n state.analyticsId = userId;\n });\n\n // Delegate to platform adapter if supported\n if (this.#platformAdapter.identify) {\n this.#platformAdapter.identify(userId, traits);\n }\n }\n\n /**\n * Track a page view.\n *\n * @param pageName - The name of the page\n * @param properties - Page properties\n */\n trackPage(pageName: string, properties?: AnalyticsEventProperties): void {\n if (!this.state.enabled) {\n return;\n }\n\n // Delegate to platform adapter if supported\n if (this.#platformAdapter.trackPage) {\n this.#platformAdapter.trackPage(pageName, properties);\n }\n }\n\n /**\n * Enable analytics tracking.\n */\n enable(): void {\n this.update((state) => {\n state.enabled = true;\n });\n }\n\n /**\n * Disable analytics tracking.\n */\n disable(): void {\n this.update((state) => {\n state.enabled = false;\n });\n }\n\n /**\n * Opt in to analytics.\n */\n optIn(): void {\n this.update((state) => {\n state.optedIn = true;\n });\n }\n\n /**\n * Opt out of analytics.\n */\n optOut(): void {\n this.update((state) => {\n state.optedIn = false;\n });\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"AnalyticsController.cjs","sourceRoot":"","sources":["../src/AnalyticsController.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAKA,+DAA2D;AAE3D,+BAAoC;AAGpC,2DAAkD;AAOlD,yEAA+D;AAC/D,2EAAmE;AAEnE,kBAAkB;AAElB;;;;GAIG;AACU,QAAA,cAAc,GAAG,qBAAqB,CAAC;AAwBpD;;GAEG;AACH,MAAM,2BAA2B,GAAG;IAClC,wBAAwB,EAAE;QACxB,kBAAkB,EAAE,IAAI;QACxB,OAAO,EAAE,IAAI;QACb,sBAAsB,EAAE,IAAI;QAC5B,QAAQ,EAAE,IAAI;KACf;IACD,uBAAuB,EAAE;QACvB,kBAAkB,EAAE,IAAI;QACxB,OAAO,EAAE,IAAI;QACb,sBAAsB,EAAE,I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,\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 AnalyticsUserTraits,\n AnalyticsTrackingEvent,\n} from './AnalyticsPlatformAdapter.types';\nimport { computeEnabledState } from './analyticsStateComputer';\nimport { validateAnalyticsState } from './analyticsStateValidator';\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 for regular account.\n */\n optedInForRegularAccount: boolean;\n\n /**\n * Whether the user has opted in to analytics for social account.\n */\n optedInForSocialAccount: 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 optedInForRegularAccount: {\n includeInStateLogs: true,\n persist: true,\n includeInDebugSnapshot: true,\n usedInUi: true,\n },\n optedInForSocialAccount: {\n includeInStateLogs: true,\n persist: true,\n includeInDebugSnapshot: true,\n usedInUi: true,\n },\n analyticsId: {\n includeInStateLogs: true,\n persist: true,\n includeInDebugSnapshot: true,\n usedInUi: false,\n },\n} satisfies StateMetadata<AnalyticsControllerState>;\n\n/**\n * Constructs the default {@link AnalyticsController} state. This allows\n * consumers to provide a partial state object when initializing the controller\n * and also helps in constructing complete state objects for this controller in\n * tests.\n *\n * @returns The default {@link AnalyticsController} state.\n */\nexport function getDefaultAnalyticsControllerState(): AnalyticsControllerState {\n return {\n optedInForRegularAccount: false,\n optedInForSocialAccount: false,\n analyticsId: uuidv4(),\n };\n}\n\n// === MESSENGER ===\n\nconst MESSENGER_EXPOSED_METHODS = [\n 'trackEvent',\n 'identify',\n 'trackView',\n 'optInForRegularAccount',\n 'optOutForRegularAccount',\n 'optInForSocialAccount',\n 'optOutForSocialAccount',\n] as const;\n\n/**\n * Returns the state of the {@link AnalyticsController}.\n */\nexport type AnalyticsControllerGetStateAction = ControllerGetStateAction<\n typeof controllerName,\n AnalyticsControllerState\n>;\n\n/**\n * Actions that {@link AnalyticsControllerMessenger} exposes to other consumers.\n */\nexport type AnalyticsControllerActions =\n | AnalyticsControllerGetStateAction\n | AnalyticsControllerMethodActions;\n\n/**\n * Actions from other messengers that {@link AnalyticsControllerMessenger} calls.\n */\ntype AllowedActions = never;\n\n/**\n * Event emitted when the state of the {@link AnalyticsController} changes.\n */\nexport type AnalyticsControllerStateChangeEvent = ControllerStateChangeEvent<\n typeof controllerName,\n AnalyticsControllerState\n>;\n\n/**\n * Events that {@link AnalyticsControllerMessenger} exposes to other consumers.\n */\nexport type AnalyticsControllerEvents = AnalyticsControllerStateChangeEvent;\n\n/**\n * Events from other messengers that {@link AnalyticsControllerMessenger} subscribes to.\n */\ntype AllowedEvents = never;\n\n/**\n * The messenger restricted to actions and events accessed by\n * {@link AnalyticsController}.\n */\nexport type AnalyticsControllerMessenger = Messenger<\n typeof controllerName,\n AnalyticsControllerActions | AllowedActions,\n AnalyticsControllerEvents | AllowedEvents\n>;\n\n// === CONTROLLER DEFINITION ===\n\n/**\n * The options that AnalyticsController takes.\n */\nexport type AnalyticsControllerOptions = {\n state?: Partial<AnalyticsControllerState>;\n messenger: AnalyticsControllerMessenger;\n /**\n * Platform adapter implementation for tracking events\n */\n platformAdapter: AnalyticsPlatformAdapter;\n};\n\n/**\n * The AnalyticsController manages analytics tracking across platforms (Mobile/Extension).\n * It provides a unified interface for tracking events, identifying users, and managing\n * analytics preferences while delegating platform-specific implementation to an\n * {@link AnalyticsPlatformAdapter}.\n *\n * This controller follows the MetaMask controller pattern and integrates with the\n * messenger system to allow other controllers and components to track analytics events.\n * It delegates platform-specific implementation to an {@link AnalyticsPlatformAdapter}.\n */\nexport class AnalyticsController extends BaseController<\n 'AnalyticsController',\n AnalyticsControllerState,\n AnalyticsControllerMessenger\n> {\n readonly #platformAdapter: AnalyticsPlatformAdapter;\n\n /**\n * Constructs an AnalyticsController instance.\n *\n * @param options - Controller options\n * @param options.state - Initial controller state (defaults from getDefaultAnalyticsControllerState).\n * For migration from a previous system, pass the existing analytics ID via state.analyticsId.\n * @param options.messenger - Messenger used to communicate with BaseController\n * @param options.platformAdapter - Platform adapter implementation for tracking\n */\n constructor({\n state = {},\n messenger,\n platformAdapter,\n }: AnalyticsControllerOptions) {\n const initialState = {\n ...getDefaultAnalyticsControllerState(),\n ...state,\n };\n\n validateAnalyticsState(initialState);\n\n super({\n name: controllerName,\n metadata: analyticsControllerMetadata,\n state: initialState,\n messenger,\n });\n\n this.#platformAdapter = platformAdapter;\n\n this.messenger.registerMethodActionHandlers(\n this,\n MESSENGER_EXPOSED_METHODS,\n );\n\n projectLogger('AnalyticsController initialized and ready', {\n enabled: computeEnabledState(this.state),\n optedIn: this.state.optedInForRegularAccount,\n socialOptedIn: this.state.optedInForSocialAccount,\n analyticsId: this.state.analyticsId,\n });\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 projectLogger('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 (!computeEnabledState(this.state)) {\n return;\n }\n\n // Derive sensitivity from presence of sensitiveProperties\n const hasSensitiveProperties =\n Object.keys(event.sensitiveProperties).length > 0;\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 (without isSensitive flag - it's the default)\n this.#platformAdapter.track(event.name, {\n ...event.properties,\n });\n\n // Track sensitive properties in a separate event with isSensitive flag\n if (hasSensitiveProperties) {\n this.#platformAdapter.track(event.name, {\n isSensitive: true,\n ...event.properties,\n ...event.sensitiveProperties,\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 (!computeEnabledState(this.state)) {\n return;\n }\n\n // Delegate to platform adapter if supported, using the current analytics ID\n if (this.#platformAdapter.identify) {\n this.#platformAdapter.identify(this.state.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 */\n trackView(name: string, properties?: AnalyticsEventProperties): void {\n if (!computeEnabledState(this.state)) {\n return;\n }\n\n // Delegate to platform adapter\n this.#platformAdapter.view(name, properties);\n }\n\n /**\n * Opt in to analytics for regular account.\n * This updates the user's opt-in status for regular account.\n */\n optInForRegularAccount(): void {\n this.update((state) => {\n state.optedInForRegularAccount = true;\n });\n }\n\n /**\n * Opt out of analytics for regular account.\n * This updates the user's opt-in status for regular account.\n */\n optOutForRegularAccount(): void {\n this.update((state) => {\n state.optedInForRegularAccount = false;\n });\n }\n\n /**\n * Opt in to analytics for social account.\n * This updates the user's opt-in status for social account.\n */\n optInForSocialAccount(): void {\n this.update((state) => {\n state.optedInForSocialAccount = true;\n });\n }\n\n /**\n * Opt out of analytics for social account.\n * This updates the user's opt-in status for social account.\n */\n optOutForSocialAccount(): void {\n this.update((state) => {\n state.optedInForSocialAccount = false;\n });\n }\n}\n"]}
|
|
@@ -2,7 +2,7 @@ import type { ControllerGetStateAction, ControllerStateChangeEvent } from "@meta
|
|
|
2
2
|
import { BaseController } from "@metamask/base-controller";
|
|
3
3
|
import type { Messenger } from "@metamask/messenger";
|
|
4
4
|
import type { AnalyticsControllerMethodActions } from "./AnalyticsController-method-action-types.cjs";
|
|
5
|
-
import type { AnalyticsPlatformAdapter, AnalyticsEventProperties } from "./AnalyticsPlatformAdapter.types.cjs";
|
|
5
|
+
import type { AnalyticsPlatformAdapter, AnalyticsEventProperties, AnalyticsUserTraits, AnalyticsTrackingEvent } from "./AnalyticsPlatformAdapter.types.cjs";
|
|
6
6
|
/**
|
|
7
7
|
* The name of the {@link AnalyticsController}, used to namespace the
|
|
8
8
|
* controller's actions and events and to namespace the controller's state data
|
|
@@ -14,15 +14,15 @@ export declare const controllerName = "AnalyticsController";
|
|
|
14
14
|
*/
|
|
15
15
|
export type AnalyticsControllerState = {
|
|
16
16
|
/**
|
|
17
|
-
* Whether analytics
|
|
17
|
+
* Whether the user has opted in to analytics for regular account.
|
|
18
18
|
*/
|
|
19
|
-
|
|
19
|
+
optedInForRegularAccount: boolean;
|
|
20
20
|
/**
|
|
21
|
-
* Whether the user has opted in to analytics
|
|
21
|
+
* Whether the user has opted in to analytics for social account.
|
|
22
22
|
*/
|
|
23
|
-
|
|
23
|
+
optedInForSocialAccount: boolean;
|
|
24
24
|
/**
|
|
25
|
-
* User's UUIDv4 analytics identifier
|
|
25
|
+
* User's UUIDv4 analytics identifier.
|
|
26
26
|
*/
|
|
27
27
|
analyticsId: string;
|
|
28
28
|
};
|
|
@@ -91,7 +91,8 @@ export declare class AnalyticsController extends BaseController<'AnalyticsContro
|
|
|
91
91
|
* Constructs an AnalyticsController instance.
|
|
92
92
|
*
|
|
93
93
|
* @param options - Controller options
|
|
94
|
-
* @param options.state - Initial controller state (defaults from getDefaultAnalyticsControllerState)
|
|
94
|
+
* @param options.state - Initial controller state (defaults from getDefaultAnalyticsControllerState).
|
|
95
|
+
* For migration from a previous system, pass the existing analytics ID via state.analyticsId.
|
|
95
96
|
* @param options.messenger - Messenger used to communicate with BaseController
|
|
96
97
|
* @param options.platformAdapter - Platform adapter implementation for tracking
|
|
97
98
|
*/
|
|
@@ -101,40 +102,42 @@ export declare class AnalyticsController extends BaseController<'AnalyticsContro
|
|
|
101
102
|
*
|
|
102
103
|
* Events are only tracked if analytics is enabled.
|
|
103
104
|
*
|
|
104
|
-
* @param
|
|
105
|
-
* @param properties - Event properties
|
|
105
|
+
* @param event - Analytics event with properties and sensitive properties
|
|
106
106
|
*/
|
|
107
|
-
trackEvent(
|
|
107
|
+
trackEvent(event: AnalyticsTrackingEvent): void;
|
|
108
108
|
/**
|
|
109
109
|
* Identify a user for analytics.
|
|
110
110
|
*
|
|
111
|
-
* @param userId - The user identifier (e.g., metametrics ID)
|
|
112
111
|
* @param traits - User traits/properties
|
|
113
112
|
*/
|
|
114
|
-
identify(
|
|
113
|
+
identify(traits?: AnalyticsUserTraits): void;
|
|
115
114
|
/**
|
|
116
|
-
* Track a page view.
|
|
115
|
+
* Track a page or screen view.
|
|
117
116
|
*
|
|
118
|
-
* @param
|
|
119
|
-
* @param properties -
|
|
117
|
+
* @param name - The identifier/name of the page or screen being viewed (e.g., "home", "settings", "wallet")
|
|
118
|
+
* @param properties - Optional properties associated with the view
|
|
120
119
|
*/
|
|
121
|
-
|
|
120
|
+
trackView(name: string, properties?: AnalyticsEventProperties): void;
|
|
122
121
|
/**
|
|
123
|
-
*
|
|
122
|
+
* Opt in to analytics for regular account.
|
|
123
|
+
* This updates the user's opt-in status for regular account.
|
|
124
124
|
*/
|
|
125
|
-
|
|
125
|
+
optInForRegularAccount(): void;
|
|
126
126
|
/**
|
|
127
|
-
*
|
|
127
|
+
* Opt out of analytics for regular account.
|
|
128
|
+
* This updates the user's opt-in status for regular account.
|
|
128
129
|
*/
|
|
129
|
-
|
|
130
|
+
optOutForRegularAccount(): void;
|
|
130
131
|
/**
|
|
131
|
-
* Opt in to analytics.
|
|
132
|
+
* Opt in to analytics for social account.
|
|
133
|
+
* This updates the user's opt-in status for social account.
|
|
132
134
|
*/
|
|
133
|
-
|
|
135
|
+
optInForSocialAccount(): void;
|
|
134
136
|
/**
|
|
135
|
-
* Opt out of analytics.
|
|
137
|
+
* Opt out of analytics for social account.
|
|
138
|
+
* This updates the user's opt-in status for social account.
|
|
136
139
|
*/
|
|
137
|
-
|
|
140
|
+
optOutForSocialAccount(): void;
|
|
138
141
|
}
|
|
139
142
|
export {};
|
|
140
143
|
//# sourceMappingURL=AnalyticsController.d.cts.map
|
|
@@ -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;AAGrD,OAAO,KAAK,EAAE,gCAAgC,EAAE,sDAAkD;AAElG,OAAO,KAAK,EACV,wBAAwB,EACxB,wBAAwB,
|
|
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,EACxB,mBAAmB,EACnB,sBAAsB,EACvB,6CAAyC;AAM1C;;;;GAIG;AACH,eAAO,MAAM,cAAc,wBAAwB,CAAC;AAIpD;;GAEG;AACH,MAAM,MAAM,wBAAwB,GAAG;IACrC;;OAEG;IACH,wBAAwB,EAAE,OAAO,CAAC;IAElC;;OAEG;IACH,uBAAuB,EAAE,OAAO,CAAC;IAEjC;;OAEG;IACH,WAAW,EAAE,MAAM,CAAC;CACrB,CAAC;AA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B,GAAG,IAAI;IASpE;;;OAGG;IACH,sBAAsB,IAAI,IAAI;IAM9B;;;OAGG;IACH,uBAAuB,IAAI,IAAI;IAM/B;;;OAGG;IACH,qBAAqB,IAAI,IAAI;IAM7B;;;OAGG;IACH,sBAAsB,IAAI,IAAI;CAK/B"}
|
|
@@ -2,7 +2,7 @@ import type { ControllerGetStateAction, ControllerStateChangeEvent } from "@meta
|
|
|
2
2
|
import { BaseController } from "@metamask/base-controller";
|
|
3
3
|
import type { Messenger } from "@metamask/messenger";
|
|
4
4
|
import type { AnalyticsControllerMethodActions } from "./AnalyticsController-method-action-types.mjs";
|
|
5
|
-
import type { AnalyticsPlatformAdapter, AnalyticsEventProperties } from "./AnalyticsPlatformAdapter.types.mjs";
|
|
5
|
+
import type { AnalyticsPlatformAdapter, AnalyticsEventProperties, AnalyticsUserTraits, AnalyticsTrackingEvent } from "./AnalyticsPlatformAdapter.types.mjs";
|
|
6
6
|
/**
|
|
7
7
|
* The name of the {@link AnalyticsController}, used to namespace the
|
|
8
8
|
* controller's actions and events and to namespace the controller's state data
|
|
@@ -14,15 +14,15 @@ export declare const controllerName = "AnalyticsController";
|
|
|
14
14
|
*/
|
|
15
15
|
export type AnalyticsControllerState = {
|
|
16
16
|
/**
|
|
17
|
-
* Whether analytics
|
|
17
|
+
* Whether the user has opted in to analytics for regular account.
|
|
18
18
|
*/
|
|
19
|
-
|
|
19
|
+
optedInForRegularAccount: boolean;
|
|
20
20
|
/**
|
|
21
|
-
* Whether the user has opted in to analytics
|
|
21
|
+
* Whether the user has opted in to analytics for social account.
|
|
22
22
|
*/
|
|
23
|
-
|
|
23
|
+
optedInForSocialAccount: boolean;
|
|
24
24
|
/**
|
|
25
|
-
* User's UUIDv4 analytics identifier
|
|
25
|
+
* User's UUIDv4 analytics identifier.
|
|
26
26
|
*/
|
|
27
27
|
analyticsId: string;
|
|
28
28
|
};
|
|
@@ -91,7 +91,8 @@ export declare class AnalyticsController extends BaseController<'AnalyticsContro
|
|
|
91
91
|
* Constructs an AnalyticsController instance.
|
|
92
92
|
*
|
|
93
93
|
* @param options - Controller options
|
|
94
|
-
* @param options.state - Initial controller state (defaults from getDefaultAnalyticsControllerState)
|
|
94
|
+
* @param options.state - Initial controller state (defaults from getDefaultAnalyticsControllerState).
|
|
95
|
+
* For migration from a previous system, pass the existing analytics ID via state.analyticsId.
|
|
95
96
|
* @param options.messenger - Messenger used to communicate with BaseController
|
|
96
97
|
* @param options.platformAdapter - Platform adapter implementation for tracking
|
|
97
98
|
*/
|
|
@@ -101,40 +102,42 @@ export declare class AnalyticsController extends BaseController<'AnalyticsContro
|
|
|
101
102
|
*
|
|
102
103
|
* Events are only tracked if analytics is enabled.
|
|
103
104
|
*
|
|
104
|
-
* @param
|
|
105
|
-
* @param properties - Event properties
|
|
105
|
+
* @param event - Analytics event with properties and sensitive properties
|
|
106
106
|
*/
|
|
107
|
-
trackEvent(
|
|
107
|
+
trackEvent(event: AnalyticsTrackingEvent): void;
|
|
108
108
|
/**
|
|
109
109
|
* Identify a user for analytics.
|
|
110
110
|
*
|
|
111
|
-
* @param userId - The user identifier (e.g., metametrics ID)
|
|
112
111
|
* @param traits - User traits/properties
|
|
113
112
|
*/
|
|
114
|
-
identify(
|
|
113
|
+
identify(traits?: AnalyticsUserTraits): void;
|
|
115
114
|
/**
|
|
116
|
-
* Track a page view.
|
|
115
|
+
* Track a page or screen view.
|
|
117
116
|
*
|
|
118
|
-
* @param
|
|
119
|
-
* @param properties -
|
|
117
|
+
* @param name - The identifier/name of the page or screen being viewed (e.g., "home", "settings", "wallet")
|
|
118
|
+
* @param properties - Optional properties associated with the view
|
|
120
119
|
*/
|
|
121
|
-
|
|
120
|
+
trackView(name: string, properties?: AnalyticsEventProperties): void;
|
|
122
121
|
/**
|
|
123
|
-
*
|
|
122
|
+
* Opt in to analytics for regular account.
|
|
123
|
+
* This updates the user's opt-in status for regular account.
|
|
124
124
|
*/
|
|
125
|
-
|
|
125
|
+
optInForRegularAccount(): void;
|
|
126
126
|
/**
|
|
127
|
-
*
|
|
127
|
+
* Opt out of analytics for regular account.
|
|
128
|
+
* This updates the user's opt-in status for regular account.
|
|
128
129
|
*/
|
|
129
|
-
|
|
130
|
+
optOutForRegularAccount(): void;
|
|
130
131
|
/**
|
|
131
|
-
* Opt in to analytics.
|
|
132
|
+
* Opt in to analytics for social account.
|
|
133
|
+
* This updates the user's opt-in status for social account.
|
|
132
134
|
*/
|
|
133
|
-
|
|
135
|
+
optInForSocialAccount(): void;
|
|
134
136
|
/**
|
|
135
|
-
* Opt out of analytics.
|
|
137
|
+
* Opt out of analytics for social account.
|
|
138
|
+
* This updates the user's opt-in status for social account.
|
|
136
139
|
*/
|
|
137
|
-
|
|
140
|
+
optOutForSocialAccount(): void;
|
|
138
141
|
}
|
|
139
142
|
export {};
|
|
140
143
|
//# sourceMappingURL=AnalyticsController.d.mts.map
|
|
@@ -1 +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,
|
|
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,EACxB,mBAAmB,EAC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AU,EACV,SAAS,EACT,eAAe,GAChB,EAAE,0BAA0B;IAuC7B;;;;;;OAMG;IACH,UAAU,CAAC,KAAK,EAAE,sBAAsB,GAAG,IAAI;IAgC/C;;;;OAIG;IACH,QAAQ,CAAC,MAAM,CAAC,EAAE,mBAAmB,GAAG,IAAI;IAW5C;;;;;OAKG;IACH,SAAS,CAAC,IAAI,EAAE,MAAM,EAAE,UAAU,CAAC,EAAE,wBAAwB,GAAG,IAAI;IASpE;;;OAGG;IACH,sBAAsB,IAAI,IAAI;IAM9B;;;OAGG;IACH,uBAAuB,IAAI,IAAI;IAM/B;;;OAGG;IACH,qBAAqB,IAAI,IAAI;IAM7B;;;OAGG;IACH,sBAAsB,IAAI,IAAI;CAK/B"}
|
|
@@ -13,6 +13,8 @@ var _AnalyticsController_platformAdapter;
|
|
|
13
13
|
import { BaseController } from "@metamask/base-controller";
|
|
14
14
|
import { v4 as uuidv4 } from "uuid";
|
|
15
15
|
import { projectLogger } from "./AnalyticsLogger.mjs";
|
|
16
|
+
import { computeEnabledState } from "./analyticsStateComputer.mjs";
|
|
17
|
+
import { validateAnalyticsState } from "./analyticsStateValidator.mjs";
|
|
16
18
|
// === GENERAL ===
|
|
17
19
|
/**
|
|
18
20
|
* The name of the {@link AnalyticsController}, used to namespace the
|
|
@@ -24,13 +26,13 @@ export const controllerName = 'AnalyticsController';
|
|
|
24
26
|
* The metadata for each property in {@link AnalyticsControllerState}.
|
|
25
27
|
*/
|
|
26
28
|
const analyticsControllerMetadata = {
|
|
27
|
-
|
|
29
|
+
optedInForRegularAccount: {
|
|
28
30
|
includeInStateLogs: true,
|
|
29
31
|
persist: true,
|
|
30
32
|
includeInDebugSnapshot: true,
|
|
31
33
|
usedInUi: true,
|
|
32
34
|
},
|
|
33
|
-
|
|
35
|
+
optedInForSocialAccount: {
|
|
34
36
|
includeInStateLogs: true,
|
|
35
37
|
persist: true,
|
|
36
38
|
includeInDebugSnapshot: true,
|
|
@@ -53,8 +55,8 @@ const analyticsControllerMetadata = {
|
|
|
53
55
|
*/
|
|
54
56
|
export function getDefaultAnalyticsControllerState() {
|
|
55
57
|
return {
|
|
56
|
-
|
|
57
|
-
|
|
58
|
+
optedInForRegularAccount: false,
|
|
59
|
+
optedInForSocialAccount: false,
|
|
58
60
|
analyticsId: uuidv4(),
|
|
59
61
|
};
|
|
60
62
|
}
|
|
@@ -62,11 +64,11 @@ export function getDefaultAnalyticsControllerState() {
|
|
|
62
64
|
const MESSENGER_EXPOSED_METHODS = [
|
|
63
65
|
'trackEvent',
|
|
64
66
|
'identify',
|
|
65
|
-
'
|
|
66
|
-
'
|
|
67
|
-
'
|
|
68
|
-
'
|
|
69
|
-
'
|
|
67
|
+
'trackView',
|
|
68
|
+
'optInForRegularAccount',
|
|
69
|
+
'optOutForRegularAccount',
|
|
70
|
+
'optInForSocialAccount',
|
|
71
|
+
'optOutForSocialAccount',
|
|
70
72
|
];
|
|
71
73
|
/**
|
|
72
74
|
* The AnalyticsController manages analytics tracking across platforms (Mobile/Extension).
|
|
@@ -83,109 +85,136 @@ export class AnalyticsController extends BaseController {
|
|
|
83
85
|
* Constructs an AnalyticsController instance.
|
|
84
86
|
*
|
|
85
87
|
* @param options - Controller options
|
|
86
|
-
* @param options.state - Initial controller state (defaults from getDefaultAnalyticsControllerState)
|
|
88
|
+
* @param options.state - Initial controller state (defaults from getDefaultAnalyticsControllerState).
|
|
89
|
+
* For migration from a previous system, pass the existing analytics ID via state.analyticsId.
|
|
87
90
|
* @param options.messenger - Messenger used to communicate with BaseController
|
|
88
91
|
* @param options.platformAdapter - Platform adapter implementation for tracking
|
|
89
92
|
*/
|
|
90
93
|
constructor({ state = {}, messenger, platformAdapter, }) {
|
|
94
|
+
const initialState = {
|
|
95
|
+
...getDefaultAnalyticsControllerState(),
|
|
96
|
+
...state,
|
|
97
|
+
};
|
|
98
|
+
validateAnalyticsState(initialState);
|
|
91
99
|
super({
|
|
92
100
|
name: controllerName,
|
|
93
101
|
metadata: analyticsControllerMetadata,
|
|
94
|
-
state:
|
|
95
|
-
...getDefaultAnalyticsControllerState(),
|
|
96
|
-
...state,
|
|
97
|
-
},
|
|
102
|
+
state: initialState,
|
|
98
103
|
messenger,
|
|
99
104
|
});
|
|
100
105
|
_AnalyticsController_platformAdapter.set(this, void 0);
|
|
101
106
|
__classPrivateFieldSet(this, _AnalyticsController_platformAdapter, platformAdapter, "f");
|
|
102
107
|
this.messenger.registerMethodActionHandlers(this, MESSENGER_EXPOSED_METHODS);
|
|
103
108
|
projectLogger('AnalyticsController initialized and ready', {
|
|
104
|
-
enabled: this.state
|
|
105
|
-
optedIn: this.state.
|
|
109
|
+
enabled: computeEnabledState(this.state),
|
|
110
|
+
optedIn: this.state.optedInForRegularAccount,
|
|
111
|
+
socialOptedIn: this.state.optedInForSocialAccount,
|
|
106
112
|
analyticsId: this.state.analyticsId,
|
|
107
113
|
});
|
|
114
|
+
// Call onSetupCompleted lifecycle hook after initialization
|
|
115
|
+
// State is already validated, so analyticsId is guaranteed to be a valid UUIDv4
|
|
116
|
+
try {
|
|
117
|
+
__classPrivateFieldGet(this, _AnalyticsController_platformAdapter, "f").onSetupCompleted(this.state.analyticsId);
|
|
118
|
+
}
|
|
119
|
+
catch (error) {
|
|
120
|
+
// Log error but don't throw - adapter setup failure shouldn't break controller
|
|
121
|
+
projectLogger('Error calling platformAdapter.onSetupCompleted', error);
|
|
122
|
+
}
|
|
108
123
|
}
|
|
109
124
|
/**
|
|
110
125
|
* Track an analytics event.
|
|
111
126
|
*
|
|
112
127
|
* Events are only tracked if analytics is enabled.
|
|
113
128
|
*
|
|
114
|
-
* @param
|
|
115
|
-
* @param properties - Event properties
|
|
129
|
+
* @param event - Analytics event with properties and sensitive properties
|
|
116
130
|
*/
|
|
117
|
-
trackEvent(
|
|
131
|
+
trackEvent(event) {
|
|
118
132
|
// Don't track if analytics is disabled
|
|
119
|
-
if (!this.state
|
|
133
|
+
if (!computeEnabledState(this.state)) {
|
|
120
134
|
return;
|
|
121
135
|
}
|
|
122
|
-
//
|
|
123
|
-
|
|
136
|
+
// Derive sensitivity from presence of sensitiveProperties
|
|
137
|
+
const hasSensitiveProperties = Object.keys(event.sensitiveProperties).length > 0;
|
|
138
|
+
// if event does not have properties, send event without properties
|
|
139
|
+
// and return to prevent any additional processing
|
|
140
|
+
if (!event.hasProperties) {
|
|
141
|
+
__classPrivateFieldGet(this, _AnalyticsController_platformAdapter, "f").track(event.name);
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
// Track regular properties (without isSensitive flag - it's the default)
|
|
145
|
+
__classPrivateFieldGet(this, _AnalyticsController_platformAdapter, "f").track(event.name, {
|
|
146
|
+
...event.properties,
|
|
147
|
+
});
|
|
148
|
+
// Track sensitive properties in a separate event with isSensitive flag
|
|
149
|
+
if (hasSensitiveProperties) {
|
|
150
|
+
__classPrivateFieldGet(this, _AnalyticsController_platformAdapter, "f").track(event.name, {
|
|
151
|
+
isSensitive: true,
|
|
152
|
+
...event.properties,
|
|
153
|
+
...event.sensitiveProperties,
|
|
154
|
+
});
|
|
155
|
+
}
|
|
124
156
|
}
|
|
125
157
|
/**
|
|
126
158
|
* Identify a user for analytics.
|
|
127
159
|
*
|
|
128
|
-
* @param userId - The user identifier (e.g., metametrics ID)
|
|
129
160
|
* @param traits - User traits/properties
|
|
130
161
|
*/
|
|
131
|
-
identify(
|
|
132
|
-
if (!this.state
|
|
162
|
+
identify(traits) {
|
|
163
|
+
if (!computeEnabledState(this.state)) {
|
|
133
164
|
return;
|
|
134
165
|
}
|
|
135
|
-
//
|
|
136
|
-
this.update((state) => {
|
|
137
|
-
state.analyticsId = userId;
|
|
138
|
-
});
|
|
139
|
-
// Delegate to platform adapter if supported
|
|
166
|
+
// Delegate to platform adapter if supported, using the current analytics ID
|
|
140
167
|
if (__classPrivateFieldGet(this, _AnalyticsController_platformAdapter, "f").identify) {
|
|
141
|
-
__classPrivateFieldGet(this, _AnalyticsController_platformAdapter, "f").identify(
|
|
168
|
+
__classPrivateFieldGet(this, _AnalyticsController_platformAdapter, "f").identify(this.state.analyticsId, traits);
|
|
142
169
|
}
|
|
143
170
|
}
|
|
144
171
|
/**
|
|
145
|
-
* Track a page view.
|
|
172
|
+
* Track a page or screen view.
|
|
146
173
|
*
|
|
147
|
-
* @param
|
|
148
|
-
* @param properties -
|
|
174
|
+
* @param name - The identifier/name of the page or screen being viewed (e.g., "home", "settings", "wallet")
|
|
175
|
+
* @param properties - Optional properties associated with the view
|
|
149
176
|
*/
|
|
150
|
-
|
|
151
|
-
if (!this.state
|
|
177
|
+
trackView(name, properties) {
|
|
178
|
+
if (!computeEnabledState(this.state)) {
|
|
152
179
|
return;
|
|
153
180
|
}
|
|
154
|
-
// Delegate to platform adapter
|
|
155
|
-
|
|
156
|
-
__classPrivateFieldGet(this, _AnalyticsController_platformAdapter, "f").trackPage(pageName, properties);
|
|
157
|
-
}
|
|
181
|
+
// Delegate to platform adapter
|
|
182
|
+
__classPrivateFieldGet(this, _AnalyticsController_platformAdapter, "f").view(name, properties);
|
|
158
183
|
}
|
|
159
184
|
/**
|
|
160
|
-
*
|
|
185
|
+
* Opt in to analytics for regular account.
|
|
186
|
+
* This updates the user's opt-in status for regular account.
|
|
161
187
|
*/
|
|
162
|
-
|
|
188
|
+
optInForRegularAccount() {
|
|
163
189
|
this.update((state) => {
|
|
164
|
-
state.
|
|
190
|
+
state.optedInForRegularAccount = true;
|
|
165
191
|
});
|
|
166
192
|
}
|
|
167
193
|
/**
|
|
168
|
-
*
|
|
194
|
+
* Opt out of analytics for regular account.
|
|
195
|
+
* This updates the user's opt-in status for regular account.
|
|
169
196
|
*/
|
|
170
|
-
|
|
197
|
+
optOutForRegularAccount() {
|
|
171
198
|
this.update((state) => {
|
|
172
|
-
state.
|
|
199
|
+
state.optedInForRegularAccount = false;
|
|
173
200
|
});
|
|
174
201
|
}
|
|
175
202
|
/**
|
|
176
|
-
* Opt in to analytics.
|
|
203
|
+
* Opt in to analytics for social account.
|
|
204
|
+
* This updates the user's opt-in status for social account.
|
|
177
205
|
*/
|
|
178
|
-
|
|
206
|
+
optInForSocialAccount() {
|
|
179
207
|
this.update((state) => {
|
|
180
|
-
state.
|
|
208
|
+
state.optedInForSocialAccount = true;
|
|
181
209
|
});
|
|
182
210
|
}
|
|
183
211
|
/**
|
|
184
|
-
* Opt out of analytics.
|
|
212
|
+
* Opt out of analytics for social account.
|
|
213
|
+
* This updates the user's opt-in status for social account.
|
|
185
214
|
*/
|
|
186
|
-
|
|
215
|
+
optOutForSocialAccount() {
|
|
187
216
|
this.update((state) => {
|
|
188
|
-
state.
|
|
217
|
+
state.optedInForSocialAccount = false;
|
|
189
218
|
});
|
|
190
219
|
}
|
|
191
220
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AnalyticsController.mjs","sourceRoot":"","sources":["../src/AnalyticsController.ts"],"names":[],"mappings":";;;;;;;;;;;;AAKA,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAE3D,OAAO,EAAE,EAAE,IAAI,MAAM,EAAE,aAAa;AAGpC,OAAO,EAAE,aAAa,EAAE,8BAA0B;AAMlD,kBAAkB;AAElB;;;;GAIG;AACH,MAAM,CAAC,MAAM,cAAc,GAAG,qBAAqB,CAAC;AAwBpD;;GAEG;AACH,MAAM,2BAA2B,GAAG;IAClC,OAAO,EAAE;QACP,kBAAkB,EAAE,IAAI;QACxB,OAAO,EAAE,IAAI;QACb,sBAAsB,EAAE,IAAI;QAC5B,QAAQ,EAAE,IAAI;KACf;IACD,OAAO,EAAE;QACP,kBAAkB,EAAE,IAAI;QACxB,OAAO,EAAE,IAAI;QACb,sBAAsB,EAAE,IAAI;QAC5B,QAAQ,EAAE,IAAI;KACf;IACD,WAAW,EAAE;QACX,kBAAkB,EAAE,IAAI;QACxB,OAAO,EAAE,IAAI;QACb,sBAAsB,EAAE,IAAI;QAC5B,QAAQ,EAAE,KAAK;KAChB;CACgD,CAAC;AAEpD;;;;;;;GAOG;AACH,MAAM,UAAU,kCAAkC;IAChD,O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,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';\nimport { v4 as uuidv4 } from 'uuid';\n\nimport type { AnalyticsControllerMethodActions } from './AnalyticsController-method-action-types';\nimport { projectLogger } from './AnalyticsLogger';\nimport type {\n AnalyticsPlatformAdapter,\n AnalyticsEventProperties,\n} from './AnalyticsPlatformAdapter.types';\n\n// === GENERAL ===\n\n/**\n * The name of the {@link AnalyticsController}, used to namespace the\n * controller's actions and events and to namespace the controller's state data\n * when composed with other controllers.\n */\nexport const controllerName = 'AnalyticsController';\n\n// === STATE ===\n\n/**\n * Describes the shape of the state object for {@link AnalyticsController}.\n */\nexport type AnalyticsControllerState = {\n /**\n * Whether analytics tracking is enabled\n */\n enabled: boolean;\n\n /**\n * Whether the user has opted in to analytics\n */\n optedIn: boolean;\n\n /**\n * User's UUIDv4 analytics identifier\n */\n analyticsId: string;\n};\n\n/**\n * The metadata for each property in {@link AnalyticsControllerState}.\n */\nconst analyticsControllerMetadata = {\n enabled: {\n includeInStateLogs: true,\n persist: true,\n includeInDebugSnapshot: true,\n usedInUi: true,\n },\n optedIn: {\n includeInStateLogs: true,\n persist: true,\n includeInDebugSnapshot: true,\n usedInUi: true,\n },\n analyticsId: {\n includeInStateLogs: true,\n persist: true,\n includeInDebugSnapshot: true,\n usedInUi: false,\n },\n} satisfies StateMetadata<AnalyticsControllerState>;\n\n/**\n * Constructs the default {@link AnalyticsController} state. This allows\n * consumers to provide a partial state object when initializing the controller\n * and also helps in constructing complete state objects for this controller in\n * tests.\n *\n * @returns The default {@link AnalyticsController} state.\n */\nexport function getDefaultAnalyticsControllerState(): AnalyticsControllerState {\n return {\n enabled: true,\n optedIn: false,\n analyticsId: uuidv4(),\n };\n}\n\n// === MESSENGER ===\n\nconst MESSENGER_EXPOSED_METHODS = [\n 'trackEvent',\n 'identify',\n 'trackPage',\n 'enable',\n 'disable',\n 'optIn',\n 'optOut',\n] as const;\n\n/**\n * Returns the state of the {@link AnalyticsController}.\n */\nexport type AnalyticsControllerGetStateAction = ControllerGetStateAction<\n typeof controllerName,\n AnalyticsControllerState\n>;\n\n/**\n * Actions that {@link AnalyticsControllerMessenger} exposes to other consumers.\n */\nexport type AnalyticsControllerActions =\n | AnalyticsControllerGetStateAction\n | AnalyticsControllerMethodActions;\n\n/**\n * Actions from other messengers that {@link AnalyticsControllerMessenger} calls.\n */\ntype AllowedActions = never;\n\n/**\n * Event emitted when the state of the {@link AnalyticsController} changes.\n */\nexport type AnalyticsControllerStateChangeEvent = ControllerStateChangeEvent<\n typeof controllerName,\n AnalyticsControllerState\n>;\n\n/**\n * Events that {@link AnalyticsControllerMessenger} exposes to other consumers.\n */\nexport type AnalyticsControllerEvents = AnalyticsControllerStateChangeEvent;\n\n/**\n * Events from other messengers that {@link AnalyticsControllerMessenger} subscribes to.\n */\ntype AllowedEvents = never;\n\n/**\n * The messenger restricted to actions and events accessed by\n * {@link AnalyticsController}.\n */\nexport type AnalyticsControllerMessenger = Messenger<\n typeof controllerName,\n AnalyticsControllerActions | AllowedActions,\n AnalyticsControllerEvents | AllowedEvents\n>;\n\n// === CONTROLLER DEFINITION ===\n\n/**\n * The options that AnalyticsController takes.\n */\nexport type AnalyticsControllerOptions = {\n state?: Partial<AnalyticsControllerState>;\n messenger: AnalyticsControllerMessenger;\n /**\n * Platform adapter implementation for tracking events\n */\n platformAdapter: AnalyticsPlatformAdapter;\n};\n\n/**\n * The AnalyticsController manages analytics tracking across platforms (Mobile/Extension).\n * It provides a unified interface for tracking events, identifying users, and managing\n * analytics preferences while delegating platform-specific implementation to an\n * {@link AnalyticsPlatformAdapter}.\n *\n * This controller follows the MetaMask controller pattern and integrates with the\n * messenger system to allow other controllers and components to track analytics events.\n * It delegates platform-specific implementation to an {@link AnalyticsPlatformAdapter}.\n */\nexport class AnalyticsController extends BaseController<\n 'AnalyticsController',\n AnalyticsControllerState,\n AnalyticsControllerMessenger\n> {\n readonly #platformAdapter: AnalyticsPlatformAdapter;\n\n /**\n * Constructs an AnalyticsController instance.\n *\n * @param options - Controller options\n * @param options.state - Initial controller state (defaults from getDefaultAnalyticsControllerState)\n * @param options.messenger - Messenger used to communicate with BaseController\n * @param options.platformAdapter - Platform adapter implementation for tracking\n */\n constructor({\n state = {},\n messenger,\n platformAdapter,\n }: AnalyticsControllerOptions) {\n super({\n name: controllerName,\n metadata: analyticsControllerMetadata,\n state: {\n ...getDefaultAnalyticsControllerState(),\n ...state,\n },\n messenger,\n });\n\n this.#platformAdapter = platformAdapter;\n\n this.messenger.registerMethodActionHandlers(\n this,\n MESSENGER_EXPOSED_METHODS,\n );\n\n projectLogger('AnalyticsController initialized and ready', {\n enabled: this.state.enabled,\n optedIn: this.state.optedIn,\n analyticsId: this.state.analyticsId,\n });\n }\n\n /**\n * Track an analytics event.\n *\n * Events are only tracked if analytics is enabled.\n *\n * @param eventName - The name of the event\n * @param properties - Event properties\n */\n trackEvent(\n eventName: string,\n properties: AnalyticsEventProperties = {},\n ): void {\n // Don't track if analytics is disabled\n if (!this.state.enabled) {\n return;\n }\n\n // Delegate to platform adapter\n this.#platformAdapter.trackEvent(eventName, properties);\n }\n\n /**\n * Identify a user for analytics.\n *\n * @param userId - The user identifier (e.g., metametrics ID)\n * @param traits - User traits/properties\n */\n identify(userId: string, traits?: AnalyticsEventProperties): void {\n if (!this.state.enabled) {\n return;\n }\n\n // Update state with analytics ID\n this.update((state) => {\n state.analyticsId = userId;\n });\n\n // Delegate to platform adapter if supported\n if (this.#platformAdapter.identify) {\n this.#platformAdapter.identify(userId, traits);\n }\n }\n\n /**\n * Track a page view.\n *\n * @param pageName - The name of the page\n * @param properties - Page properties\n */\n trackPage(pageName: string, properties?: AnalyticsEventProperties): void {\n if (!this.state.enabled) {\n return;\n }\n\n // Delegate to platform adapter if supported\n if (this.#platformAdapter.trackPage) {\n this.#platformAdapter.trackPage(pageName, properties);\n }\n }\n\n /**\n * Enable analytics tracking.\n */\n enable(): void {\n this.update((state) => {\n state.enabled = true;\n });\n }\n\n /**\n * Disable analytics tracking.\n */\n disable(): void {\n this.update((state) => {\n state.enabled = false;\n });\n }\n\n /**\n * Opt in to analytics.\n */\n optIn(): void {\n this.update((state) => {\n state.optedIn = true;\n });\n }\n\n /**\n * Opt out of analytics.\n */\n optOut(): void {\n this.update((state) => {\n state.optedIn = false;\n });\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"AnalyticsController.mjs","sourceRoot":"","sources":["../src/AnalyticsController.ts"],"names":[],"mappings":";;;;;;;;;;;;AAKA,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAE3D,OAAO,EAAE,EAAE,IAAI,MAAM,EAAE,aAAa;AAGpC,OAAO,EAAE,aAAa,EAAE,8BAA0B;AAOlD,OAAO,EAAE,mBAAmB,EAAE,qCAAiC;AAC/D,OAAO,EAAE,sBAAsB,EAAE,sCAAkC;AAEnE,kBAAkB;AAElB;;;;GAIG;AACH,MAAM,CAAC,MAAM,cAAc,GAAG,qBAAqB,CAAC;AAwBpD;;GAEG;AACH,MAAM,2BAA2B,GAAG;IAClC,wBAAwB,EAAE;QACxB,kBAAkB,EAAE,IAAI;QACxB,OAAO,EAAE,IAAI;QACb,sBAAsB,EAAE,IAAI;QAC5B,QAAQ,EAAE,IAAI;KACf;IACD,uBAAuB,EAAE;QACvB,kBAAkB,EAAE,IAAI;QACxB,OAAO,EAAE,IAAI;QACb,sBAAsB,EAAE,IAAI;QAC5B,QAAQ,EAAE,IAAI;KACf;IACD,WAAW,EAAE;QACX,kBAAkB,EAAE,IAAI;QACxB,OAAO,EAAE,IAAI;QACb,sBAAsB,EAAE,IAAI;QAC5B,QAAQ,EAAE,KAAK;KAChB;CACgD,CAAC;AAEpD;;;;;;;GAOG;AACH,MAAM,UAAU,kCAAkC;IAChD,OAAO;QACL,wBA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;IACH,SAAS,CAAC,IAAY,EAAE,UAAqC;QAC3D,IAAI,CAAC,mBAAmB,CAAC,IAAI,CAAC,KAAK,CAAC,EAAE,CAAC;YACrC,OAAO;QACT,CAAC;QAED,+BAA+B;QAC/B,uBAAA,IAAI,4CAAiB,CAAC,IAAI,CAAC,IAAI,EAAE,UAAU,CAAC,CAAC;IAC/C,CAAC;IAED;;;OAGG;IACH,sBAAsB;QACpB,IAAI,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE;YACpB,KAAK,CAAC,wBAAwB,GAAG,IAAI,CAAC;QACxC,CAAC,CAAC,CAAC;IACL,CAAC;IAED;;;OAGG;IACH,uBAAuB;QACrB,IAAI,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE;YACpB,KAAK,CAAC,wBAAwB,GAAG,KAAK,CAAC;QACzC,CAAC,CAAC,CAAC;IACL,CAAC;IAED;;;OAGG;IACH,qBAAqB;QACnB,IAAI,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE;YACpB,KAAK,CAAC,uBAAuB,GAAG,IAAI,CAAC;QACvC,CAAC,CAAC,CAAC;IACL,CAAC;IAED;;;OAGG;IACH,sBAAsB;QACpB,IAAI,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE;YACpB,KAAK,CAAC,uBAAuB,GAAG,KAAK,CAAC;QACxC,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';\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 AnalyticsUserTraits,\n AnalyticsTrackingEvent,\n} from './AnalyticsPlatformAdapter.types';\nimport { computeEnabledState } from './analyticsStateComputer';\nimport { validateAnalyticsState } from './analyticsStateValidator';\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 for regular account.\n */\n optedInForRegularAccount: boolean;\n\n /**\n * Whether the user has opted in to analytics for social account.\n */\n optedInForSocialAccount: 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 optedInForRegularAccount: {\n includeInStateLogs: true,\n persist: true,\n includeInDebugSnapshot: true,\n usedInUi: true,\n },\n optedInForSocialAccount: {\n includeInStateLogs: true,\n persist: true,\n includeInDebugSnapshot: true,\n usedInUi: true,\n },\n analyticsId: {\n includeInStateLogs: true,\n persist: true,\n includeInDebugSnapshot: true,\n usedInUi: false,\n },\n} satisfies StateMetadata<AnalyticsControllerState>;\n\n/**\n * Constructs the default {@link AnalyticsController} state. This allows\n * consumers to provide a partial state object when initializing the controller\n * and also helps in constructing complete state objects for this controller in\n * tests.\n *\n * @returns The default {@link AnalyticsController} state.\n */\nexport function getDefaultAnalyticsControllerState(): AnalyticsControllerState {\n return {\n optedInForRegularAccount: false,\n optedInForSocialAccount: false,\n analyticsId: uuidv4(),\n };\n}\n\n// === MESSENGER ===\n\nconst MESSENGER_EXPOSED_METHODS = [\n 'trackEvent',\n 'identify',\n 'trackView',\n 'optInForRegularAccount',\n 'optOutForRegularAccount',\n 'optInForSocialAccount',\n 'optOutForSocialAccount',\n] as const;\n\n/**\n * Returns the state of the {@link AnalyticsController}.\n */\nexport type AnalyticsControllerGetStateAction = ControllerGetStateAction<\n typeof controllerName,\n AnalyticsControllerState\n>;\n\n/**\n * Actions that {@link AnalyticsControllerMessenger} exposes to other consumers.\n */\nexport type AnalyticsControllerActions =\n | AnalyticsControllerGetStateAction\n | AnalyticsControllerMethodActions;\n\n/**\n * Actions from other messengers that {@link AnalyticsControllerMessenger} calls.\n */\ntype AllowedActions = never;\n\n/**\n * Event emitted when the state of the {@link AnalyticsController} changes.\n */\nexport type AnalyticsControllerStateChangeEvent = ControllerStateChangeEvent<\n typeof controllerName,\n AnalyticsControllerState\n>;\n\n/**\n * Events that {@link AnalyticsControllerMessenger} exposes to other consumers.\n */\nexport type AnalyticsControllerEvents = AnalyticsControllerStateChangeEvent;\n\n/**\n * Events from other messengers that {@link AnalyticsControllerMessenger} subscribes to.\n */\ntype AllowedEvents = never;\n\n/**\n * The messenger restricted to actions and events accessed by\n * {@link AnalyticsController}.\n */\nexport type AnalyticsControllerMessenger = Messenger<\n typeof controllerName,\n AnalyticsControllerActions | AllowedActions,\n AnalyticsControllerEvents | AllowedEvents\n>;\n\n// === CONTROLLER DEFINITION ===\n\n/**\n * The options that AnalyticsController takes.\n */\nexport type AnalyticsControllerOptions = {\n state?: Partial<AnalyticsControllerState>;\n messenger: AnalyticsControllerMessenger;\n /**\n * Platform adapter implementation for tracking events\n */\n platformAdapter: AnalyticsPlatformAdapter;\n};\n\n/**\n * The AnalyticsController manages analytics tracking across platforms (Mobile/Extension).\n * It provides a unified interface for tracking events, identifying users, and managing\n * analytics preferences while delegating platform-specific implementation to an\n * {@link AnalyticsPlatformAdapter}.\n *\n * This controller follows the MetaMask controller pattern and integrates with the\n * messenger system to allow other controllers and components to track analytics events.\n * It delegates platform-specific implementation to an {@link AnalyticsPlatformAdapter}.\n */\nexport class AnalyticsController extends BaseController<\n 'AnalyticsController',\n AnalyticsControllerState,\n AnalyticsControllerMessenger\n> {\n readonly #platformAdapter: AnalyticsPlatformAdapter;\n\n /**\n * Constructs an AnalyticsController instance.\n *\n * @param options - Controller options\n * @param options.state - Initial controller state (defaults from getDefaultAnalyticsControllerState).\n * For migration from a previous system, pass the existing analytics ID via state.analyticsId.\n * @param options.messenger - Messenger used to communicate with BaseController\n * @param options.platformAdapter - Platform adapter implementation for tracking\n */\n constructor({\n state = {},\n messenger,\n platformAdapter,\n }: AnalyticsControllerOptions) {\n const initialState = {\n ...getDefaultAnalyticsControllerState(),\n ...state,\n };\n\n validateAnalyticsState(initialState);\n\n super({\n name: controllerName,\n metadata: analyticsControllerMetadata,\n state: initialState,\n messenger,\n });\n\n this.#platformAdapter = platformAdapter;\n\n this.messenger.registerMethodActionHandlers(\n this,\n MESSENGER_EXPOSED_METHODS,\n );\n\n projectLogger('AnalyticsController initialized and ready', {\n enabled: computeEnabledState(this.state),\n optedIn: this.state.optedInForRegularAccount,\n socialOptedIn: this.state.optedInForSocialAccount,\n analyticsId: this.state.analyticsId,\n });\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 projectLogger('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 (!computeEnabledState(this.state)) {\n return;\n }\n\n // Derive sensitivity from presence of sensitiveProperties\n const hasSensitiveProperties =\n Object.keys(event.sensitiveProperties).length > 0;\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 (without isSensitive flag - it's the default)\n this.#platformAdapter.track(event.name, {\n ...event.properties,\n });\n\n // Track sensitive properties in a separate event with isSensitive flag\n if (hasSensitiveProperties) {\n this.#platformAdapter.track(event.name, {\n isSensitive: true,\n ...event.properties,\n ...event.sensitiveProperties,\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 (!computeEnabledState(this.state)) {\n return;\n }\n\n // Delegate to platform adapter if supported, using the current analytics ID\n if (this.#platformAdapter.identify) {\n this.#platformAdapter.identify(this.state.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 */\n trackView(name: string, properties?: AnalyticsEventProperties): void {\n if (!computeEnabledState(this.state)) {\n return;\n }\n\n // Delegate to platform adapter\n this.#platformAdapter.view(name, properties);\n }\n\n /**\n * Opt in to analytics for regular account.\n * This updates the user's opt-in status for regular account.\n */\n optInForRegularAccount(): void {\n this.update((state) => {\n state.optedInForRegularAccount = true;\n });\n }\n\n /**\n * Opt out of analytics for regular account.\n * This updates the user's opt-in status for regular account.\n */\n optOutForRegularAccount(): void {\n this.update((state) => {\n state.optedInForRegularAccount = false;\n });\n }\n\n /**\n * Opt in to analytics for social account.\n * This updates the user's opt-in status for social account.\n */\n optInForSocialAccount(): void {\n this.update((state) => {\n state.optedInForSocialAccount = true;\n });\n }\n\n /**\n * Opt out of analytics for social account.\n * This updates the user's opt-in status for social account.\n */\n optOutForSocialAccount(): void {\n this.update((state) => {\n state.optedInForSocialAccount = 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 *
|
|
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 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 * 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, properties?: AnalyticsEventProperties): void;\n\n /**\n * Identify a user with traits.\n *\n * @param userId - The user identifier (e.g., metametrics ID)\n * @param traits - User traits/properties\n */\n identify(userId: string, traits?: AnalyticsUserTraits): void;\n\n /**\n * Track a UI unit (page or screen) view depending on the platform\n *\n * This 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 */\n view(name: string, properties?: AnalyticsEventProperties): void;\n\n /**\n * Lifecycle hook called after the AnalyticsController is fully initialized.\n *\n * This hook allows platform-specific adapters to perform setup that requires\n * access to the controller's state (e.g., analyticsId).\n *\n * The controller calls this method once after initialization, passing the\n * analyticsId from controller state. The analyticsId is guaranteed to be set\n * when this method is called - this is the definition of \"completed\" setup.\n *\n * @param analyticsId - The analytics ID from controller state. Always set (never empty).\n * @throws {AnalyticsPlatformAdapterSetupError} May throw errors during setup (e.g., configuration errors, network failures).\n * Errors thrown by this method are caught and logged by the controller, but do not prevent\n * controller initialization from completing successfully.\n *\n * @example\n * ```typescript\n * onSetupCompleted(analyticsId: string): void {\n * // Add platform-specific plugins that require analyticsId\n * client.add({\n * plugin: new PrivacyPlugin(analyticsId),\n * });\n * }\n * ```\n */\n onSetupCompleted(analyticsId: string): void;\n};\n"]}
|