@metamask-previews/analytics-controller 0.0.0-preview-21a5ddac → 0.0.0-preview-4ed214fa
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +1 -1
- package/README.md +11 -72
- package/dist/AnalyticsController-method-action-types.cjs.map +1 -1
- package/dist/AnalyticsController-method-action-types.d.cts +26 -28
- package/dist/AnalyticsController-method-action-types.d.cts.map +1 -1
- package/dist/AnalyticsController-method-action-types.d.mts +26 -28
- 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 +52 -81
- package/dist/AnalyticsController.cjs.map +1 -1
- package/dist/AnalyticsController.d.cts +24 -27
- package/dist/AnalyticsController.d.cts.map +1 -1
- package/dist/AnalyticsController.d.mts +24 -27
- package/dist/AnalyticsController.d.mts.map +1 -1
- package/dist/AnalyticsController.mjs +52 -81
- package/dist/AnalyticsController.mjs.map +1 -1
- package/dist/AnalyticsPlatformAdapter.types.cjs.map +1 -1
- package/dist/AnalyticsPlatformAdapter.types.d.cts +15 -58
- package/dist/AnalyticsPlatformAdapter.types.d.cts.map +1 -1
- package/dist/AnalyticsPlatformAdapter.types.d.mts +15 -58
- package/dist/AnalyticsPlatformAdapter.types.d.mts.map +1 -1
- package/dist/AnalyticsPlatformAdapter.types.mjs.map +1 -1
- package/dist/index.cjs +1 -7
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +2 -4
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +2 -4
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +0 -4
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
- package/dist/AnalyticsPlatformAdapterSetupError.cjs +0 -17
- package/dist/AnalyticsPlatformAdapterSetupError.cjs.map +0 -1
- package/dist/AnalyticsPlatformAdapterSetupError.d.cts +0 -8
- package/dist/AnalyticsPlatformAdapterSetupError.d.cts.map +0 -1
- package/dist/AnalyticsPlatformAdapterSetupError.d.mts +0 -8
- package/dist/AnalyticsPlatformAdapterSetupError.d.mts.map +0 -1
- package/dist/AnalyticsPlatformAdapterSetupError.mjs +0 -13
- package/dist/AnalyticsPlatformAdapterSetupError.mjs.map +0 -1
- package/dist/analyticsStateComputer.cjs +0 -46
- package/dist/analyticsStateComputer.cjs.map +0 -1
- package/dist/analyticsStateComputer.d.cts +0 -35
- package/dist/analyticsStateComputer.d.cts.map +0 -1
- package/dist/analyticsStateComputer.d.mts +0 -35
- package/dist/analyticsStateComputer.d.mts.map +0 -1
- package/dist/analyticsStateComputer.mjs +0 -42
- package/dist/analyticsStateComputer.mjs.map +0 -1
- package/dist/analyticsStateValidator.cjs +0 -19
- package/dist/analyticsStateValidator.cjs.map +0 -1
- package/dist/analyticsStateValidator.d.cts +0 -9
- package/dist/analyticsStateValidator.d.cts.map +0 -1
- package/dist/analyticsStateValidator.d.mts +0 -9
- package/dist/analyticsStateValidator.d.mts.map +0 -1
- package/dist/analyticsStateValidator.mjs +0 -15
- package/dist/analyticsStateValidator.mjs.map +0 -1
- package/dist/selectors.cjs +0 -44
- package/dist/selectors.cjs.map +0 -1
- package/dist/selectors.d.cts +0 -12
- package/dist/selectors.d.cts.map +0 -1
- package/dist/selectors.d.mts +0 -12
- package/dist/selectors.d.mts.map +0 -1
- package/dist/selectors.mjs +0 -41
- package/dist/selectors.mjs.map +0 -1
|
@@ -1 +1 @@
|
|
|
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,kBAAk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AC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE;YACpB,KAAK,CAAC,uBAAuB,GAAG,KAAK,CAAC;QACxC,CAAC,CAAC,CAAC;IACL,CAAC;CACF;AAxKD,kDAwKC","sourcesContent":["import type {\n ControllerGetStateAction,\n ControllerStateChangeEvent,\n StateMetadata,\n} from '@metamask/base-controller';\nimport { BaseController } from '@metamask/base-controller';\nimport type { Messenger } from '@metamask/messenger';\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
|
+
{"version":3,"file":"AnalyticsController.cjs","sourceRoot":"","sources":["../src/AnalyticsController.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAKA,+DAA2D;AAE3D,+BAAoC;AAGpC,2DAAkD;AAMlD,kBAAkB;AAElB;;;;GAIG;AACU,QAAA,cAAc,GAAG,qBAAqB,CAAC;AAwBpD;;GAEG;AACH,MAAM,2BAA2B,GAAG;IAClC,OAAO,EAAE;QACP,kBAAkB,EAAE,IAAI;QACxB,OAAO,EAAE,IAAI;QACb,sBAAsB,EAAE,IAAI;QAC5B,QAAQ,EAAE,IAAI;KACf;IACD,OAAO,EAAE;QACP,kBAAkB,EAAE,IAAI;QACxB,OAAO,EAAE,IAAI;QACb,sBAAsB,EAAE,IAAI;QAC5B,QAAQ,EAAE,IAAI;KACf;IACD,WAAW,EAAE;QACX,kBAAkB,EAAE,IAAI;QACxB,OAAO,EAAE,IAAI;QACb,sBAAsB,EAAE,IAAI;QAC5B,QAAQ,EAAE,KAAK;KAChB;CACgD,CAAC;AAEpD;;;;;;;GAOG;AACH,SAAgB,kCAAkC;IAChD,OAAO;QACL,OAAO,EAAE,IAAI;QACb,OAAO,EAAE,KAAK;QACd,WAAW,EAAE,IAAA,SAAM,GAAE;KACtB,CAAC;AACJ,CAAC;AAND,gFAMC;AAED,oBAAoB;AAEpB,MAAM,yBAAyB,GAAG;IAChC,YAAY;IACZ,UAAU;IACV,WAAW;IACX,QAAQ;IACR,SAAS;IACT,OAAO;IACP,QAAQ;CACA,CAAC;AAgEX;;;;;;;;;GASG;AACH,MAAa,mBAAoB,SAAQ,gCAIxC;IAGC;;;;;;;OAOG;IACH,YAAY,EACV,KAAK,GAAG,EAAE,EACV,SAAS,EACT,eAAe,GACY;QAC3B,KAAK,CAAC;YACJ,IAAI,EAAE,sBAAc;YACpB,QAAQ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AC/D,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,OAAO,EAAE,CAAC;YACxB,OAAO;QACT,CAAC;QAED,4CAA4C;QAC5C,IAAI,uBAAA,IAAI,4CAAiB,CAAC,SAAS,EAAE,CAAC;YACpC,uBAAA,IAAI,4CAAiB,CAAC,SAAS,CAAC,QAAQ,EAAE,UAAU,CAAC,CAAC;QACxD,CAAC;IACH,CAAC;IAED;;OAEG;IACH,MAAM;QACJ,IAAI,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE;YACpB,KAAK,CAAC,OAAO,GAAG,IAAI,CAAC;QACvB,CAAC,CAAC,CAAC;IACL,CAAC;IAED;;OAEG;IACH,OAAO;QACL,IAAI,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE;YACpB,KAAK,CAAC,OAAO,GAAG,KAAK,CAAC;QACxB,CAAC,CAAC,CAAC;IACL,CAAC;IAED;;OAEG;IACH,KAAK;QACH,IAAI,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE;YACpB,KAAK,CAAC,OAAO,GAAG,IAAI,CAAC;QACvB,CAAC,CAAC,CAAC;IACL,CAAC;IAED;;OAEG;IACH,MAAM;QACJ,IAAI,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE;YACpB,KAAK,CAAC,OAAO,GAAG,KAAK,CAAC;QACxB,CAAC,CAAC,CAAC;IACL,CAAC;CACF;AA3ID,kDA2IC","sourcesContent":["import type {\n ControllerGetStateAction,\n ControllerStateChangeEvent,\n StateMetadata,\n} from '@metamask/base-controller';\nimport { BaseController } from '@metamask/base-controller';\nimport type { Messenger } from '@metamask/messenger';\nimport { v4 as uuidv4 } from 'uuid';\n\nimport type { AnalyticsControllerMethodActions } from './AnalyticsController-method-action-types';\nimport { projectLogger } from './AnalyticsLogger';\nimport type {\n AnalyticsPlatformAdapter,\n AnalyticsEventProperties,\n} from './AnalyticsPlatformAdapter.types';\n\n// === GENERAL ===\n\n/**\n * The name of the {@link AnalyticsController}, used to namespace the\n * controller's actions and events and to namespace the controller's state data\n * when composed with other controllers.\n */\nexport const controllerName = 'AnalyticsController';\n\n// === STATE ===\n\n/**\n * Describes the shape of the state object for {@link AnalyticsController}.\n */\nexport type AnalyticsControllerState = {\n /**\n * Whether analytics tracking is enabled\n */\n enabled: boolean;\n\n /**\n * Whether the user has opted in to analytics\n */\n optedIn: boolean;\n\n /**\n * User's UUIDv4 analytics identifier\n */\n analyticsId: string;\n};\n\n/**\n * The metadata for each property in {@link AnalyticsControllerState}.\n */\nconst analyticsControllerMetadata = {\n enabled: {\n includeInStateLogs: true,\n persist: true,\n includeInDebugSnapshot: true,\n usedInUi: true,\n },\n optedIn: {\n includeInStateLogs: true,\n persist: true,\n includeInDebugSnapshot: true,\n usedInUi: true,\n },\n analyticsId: {\n includeInStateLogs: true,\n persist: true,\n includeInDebugSnapshot: true,\n usedInUi: false,\n },\n} satisfies StateMetadata<AnalyticsControllerState>;\n\n/**\n * Constructs the default {@link AnalyticsController} state. This allows\n * consumers to provide a partial state object when initializing the controller\n * and also helps in constructing complete state objects for this controller in\n * tests.\n *\n * @returns The default {@link AnalyticsController} state.\n */\nexport function getDefaultAnalyticsControllerState(): AnalyticsControllerState {\n return {\n enabled: true,\n optedIn: false,\n analyticsId: uuidv4(),\n };\n}\n\n// === MESSENGER ===\n\nconst MESSENGER_EXPOSED_METHODS = [\n 'trackEvent',\n 'identify',\n 'trackPage',\n 'enable',\n 'disable',\n 'optIn',\n 'optOut',\n] as const;\n\n/**\n * Returns the state of the {@link AnalyticsController}.\n */\nexport type AnalyticsControllerGetStateAction = ControllerGetStateAction<\n typeof controllerName,\n AnalyticsControllerState\n>;\n\n/**\n * Actions that {@link AnalyticsControllerMessenger} exposes to other consumers.\n */\nexport type AnalyticsControllerActions =\n | AnalyticsControllerGetStateAction\n | AnalyticsControllerMethodActions;\n\n/**\n * Actions from other messengers that {@link AnalyticsControllerMessenger} calls.\n */\ntype AllowedActions = never;\n\n/**\n * Event emitted when the state of the {@link AnalyticsController} changes.\n */\nexport type AnalyticsControllerStateChangeEvent = ControllerStateChangeEvent<\n typeof controllerName,\n AnalyticsControllerState\n>;\n\n/**\n * Events that {@link AnalyticsControllerMessenger} exposes to other consumers.\n */\nexport type AnalyticsControllerEvents = AnalyticsControllerStateChangeEvent;\n\n/**\n * Events from other messengers that {@link AnalyticsControllerMessenger} subscribes to.\n */\ntype AllowedEvents = never;\n\n/**\n * The messenger restricted to actions and events accessed by\n * {@link AnalyticsController}.\n */\nexport type AnalyticsControllerMessenger = Messenger<\n typeof controllerName,\n AnalyticsControllerActions | AllowedActions,\n AnalyticsControllerEvents | AllowedEvents\n>;\n\n// === CONTROLLER DEFINITION ===\n\n/**\n * The options that AnalyticsController takes.\n */\nexport type AnalyticsControllerOptions = {\n state?: Partial<AnalyticsControllerState>;\n messenger: AnalyticsControllerMessenger;\n /**\n * Platform adapter implementation for tracking events\n */\n platformAdapter: AnalyticsPlatformAdapter;\n};\n\n/**\n * The AnalyticsController manages analytics tracking across platforms (Mobile/Extension).\n * It provides a unified interface for tracking events, identifying users, and managing\n * analytics preferences while delegating platform-specific implementation to an\n * {@link AnalyticsPlatformAdapter}.\n *\n * This controller follows the MetaMask controller pattern and integrates with the\n * messenger system to allow other controllers and components to track analytics events.\n * It delegates platform-specific implementation to an {@link AnalyticsPlatformAdapter}.\n */\nexport class AnalyticsController extends BaseController<\n 'AnalyticsController',\n AnalyticsControllerState,\n AnalyticsControllerMessenger\n> {\n readonly #platformAdapter: AnalyticsPlatformAdapter;\n\n /**\n * Constructs an AnalyticsController instance.\n *\n * @param options - Controller options\n * @param options.state - Initial controller state (defaults from getDefaultAnalyticsControllerState)\n * @param options.messenger - Messenger used to communicate with BaseController\n * @param options.platformAdapter - Platform adapter implementation for tracking\n */\n constructor({\n state = {},\n messenger,\n platformAdapter,\n }: AnalyticsControllerOptions) {\n super({\n name: controllerName,\n metadata: analyticsControllerMetadata,\n state: {\n ...getDefaultAnalyticsControllerState(),\n ...state,\n },\n messenger,\n });\n\n this.#platformAdapter = platformAdapter;\n\n this.messenger.registerMethodActionHandlers(\n this,\n MESSENGER_EXPOSED_METHODS,\n );\n\n projectLogger('AnalyticsController initialized and ready', {\n enabled: this.state.enabled,\n optedIn: this.state.optedIn,\n analyticsId: this.state.analyticsId,\n });\n }\n\n /**\n * Track an analytics event.\n *\n * Events are only tracked if analytics is enabled.\n *\n * @param eventName - The name of the event\n * @param properties - Event properties\n */\n trackEvent(\n eventName: string,\n properties: AnalyticsEventProperties = {},\n ): void {\n // Don't track if analytics is disabled\n if (!this.state.enabled) {\n return;\n }\n\n // Delegate to platform adapter\n this.#platformAdapter.trackEvent(eventName, properties);\n }\n\n /**\n * Identify a user for analytics.\n *\n * @param userId - The user identifier (e.g., metametrics ID)\n * @param traits - User traits/properties\n */\n identify(userId: string, traits?: AnalyticsEventProperties): void {\n if (!this.state.enabled) {\n return;\n }\n\n // Update state with analytics ID\n this.update((state) => {\n state.analyticsId = userId;\n });\n\n // Delegate to platform adapter if supported\n if (this.#platformAdapter.identify) {\n this.#platformAdapter.identify(userId, traits);\n }\n }\n\n /**\n * Track a page view.\n *\n * @param pageName - The name of the page\n * @param properties - Page properties\n */\n trackPage(pageName: string, properties?: AnalyticsEventProperties): void {\n if (!this.state.enabled) {\n return;\n }\n\n // Delegate to platform adapter if supported\n if (this.#platformAdapter.trackPage) {\n this.#platformAdapter.trackPage(pageName, properties);\n }\n }\n\n /**\n * Enable analytics tracking.\n */\n enable(): void {\n this.update((state) => {\n state.enabled = true;\n });\n }\n\n /**\n * Disable analytics tracking.\n */\n disable(): void {\n this.update((state) => {\n state.enabled = false;\n });\n }\n\n /**\n * Opt in to analytics.\n */\n optIn(): void {\n this.update((state) => {\n state.optedIn = true;\n });\n }\n\n /**\n * Opt out of analytics.\n */\n optOut(): void {\n this.update((state) => {\n state.optedIn = false;\n });\n }\n}\n"]}
|
|
@@ -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
|
|
5
|
+
import type { AnalyticsPlatformAdapter, AnalyticsEventProperties } 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
|
|
17
|
+
* Whether analytics tracking is enabled
|
|
18
18
|
*/
|
|
19
|
-
|
|
19
|
+
enabled: boolean;
|
|
20
20
|
/**
|
|
21
|
-
* Whether the user has opted in to analytics
|
|
21
|
+
* Whether the user has opted in to analytics
|
|
22
22
|
*/
|
|
23
|
-
|
|
23
|
+
optedIn: boolean;
|
|
24
24
|
/**
|
|
25
|
-
* User's UUIDv4 analytics identifier
|
|
25
|
+
* User's UUIDv4 analytics identifier
|
|
26
26
|
*/
|
|
27
27
|
analyticsId: string;
|
|
28
28
|
};
|
|
@@ -91,8 +91,7 @@ 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)
|
|
95
|
-
* For migration from a previous system, pass the existing analytics ID via state.analyticsId.
|
|
94
|
+
* @param options.state - Initial controller state (defaults from getDefaultAnalyticsControllerState)
|
|
96
95
|
* @param options.messenger - Messenger used to communicate with BaseController
|
|
97
96
|
* @param options.platformAdapter - Platform adapter implementation for tracking
|
|
98
97
|
*/
|
|
@@ -102,42 +101,40 @@ export declare class AnalyticsController extends BaseController<'AnalyticsContro
|
|
|
102
101
|
*
|
|
103
102
|
* Events are only tracked if analytics is enabled.
|
|
104
103
|
*
|
|
105
|
-
* @param
|
|
104
|
+
* @param eventName - The name of the event
|
|
105
|
+
* @param properties - Event properties
|
|
106
106
|
*/
|
|
107
|
-
trackEvent(
|
|
107
|
+
trackEvent(eventName: string, properties?: AnalyticsEventProperties): void;
|
|
108
108
|
/**
|
|
109
109
|
* Identify a user for analytics.
|
|
110
110
|
*
|
|
111
|
+
* @param userId - The user identifier (e.g., metametrics ID)
|
|
111
112
|
* @param traits - User traits/properties
|
|
112
113
|
*/
|
|
113
|
-
identify(traits?:
|
|
114
|
+
identify(userId: string, traits?: AnalyticsEventProperties): void;
|
|
114
115
|
/**
|
|
115
|
-
* Track a page
|
|
116
|
+
* Track a page view.
|
|
116
117
|
*
|
|
117
|
-
* @param
|
|
118
|
-
* @param properties -
|
|
118
|
+
* @param pageName - The name of the page
|
|
119
|
+
* @param properties - Page properties
|
|
119
120
|
*/
|
|
120
|
-
|
|
121
|
+
trackPage(pageName: string, properties?: AnalyticsEventProperties): void;
|
|
121
122
|
/**
|
|
122
|
-
*
|
|
123
|
-
* This updates the user's opt-in status for regular account.
|
|
123
|
+
* Enable analytics tracking.
|
|
124
124
|
*/
|
|
125
|
-
|
|
125
|
+
enable(): void;
|
|
126
126
|
/**
|
|
127
|
-
*
|
|
128
|
-
* This updates the user's opt-in status for regular account.
|
|
127
|
+
* Disable analytics tracking.
|
|
129
128
|
*/
|
|
130
|
-
|
|
129
|
+
disable(): void;
|
|
131
130
|
/**
|
|
132
|
-
* Opt in to analytics
|
|
133
|
-
* This updates the user's opt-in status for social account.
|
|
131
|
+
* Opt in to analytics.
|
|
134
132
|
*/
|
|
135
|
-
|
|
133
|
+
optIn(): void;
|
|
136
134
|
/**
|
|
137
|
-
* Opt out of analytics
|
|
138
|
-
* This updates the user's opt-in status for social account.
|
|
135
|
+
* Opt out of analytics.
|
|
139
136
|
*/
|
|
140
|
-
|
|
137
|
+
optOut(): void;
|
|
141
138
|
}
|
|
142
139
|
export {};
|
|
143
140
|
//# 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,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,MA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IACH,MAAM,IAAI,IAAI;CAKf"}
|
|
@@ -2,7 +2,7 @@ import type { ControllerGetStateAction, ControllerStateChangeEvent } from "@meta
|
|
|
2
2
|
import { BaseController } from "@metamask/base-controller";
|
|
3
3
|
import type { Messenger } from "@metamask/messenger";
|
|
4
4
|
import type { AnalyticsControllerMethodActions } from "./AnalyticsController-method-action-types.mjs";
|
|
5
|
-
import type { AnalyticsPlatformAdapter, AnalyticsEventProperties
|
|
5
|
+
import type { AnalyticsPlatformAdapter, AnalyticsEventProperties } 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
|
|
17
|
+
* Whether analytics tracking is enabled
|
|
18
18
|
*/
|
|
19
|
-
|
|
19
|
+
enabled: boolean;
|
|
20
20
|
/**
|
|
21
|
-
* Whether the user has opted in to analytics
|
|
21
|
+
* Whether the user has opted in to analytics
|
|
22
22
|
*/
|
|
23
|
-
|
|
23
|
+
optedIn: boolean;
|
|
24
24
|
/**
|
|
25
|
-
* User's UUIDv4 analytics identifier
|
|
25
|
+
* User's UUIDv4 analytics identifier
|
|
26
26
|
*/
|
|
27
27
|
analyticsId: string;
|
|
28
28
|
};
|
|
@@ -91,8 +91,7 @@ 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)
|
|
95
|
-
* For migration from a previous system, pass the existing analytics ID via state.analyticsId.
|
|
94
|
+
* @param options.state - Initial controller state (defaults from getDefaultAnalyticsControllerState)
|
|
96
95
|
* @param options.messenger - Messenger used to communicate with BaseController
|
|
97
96
|
* @param options.platformAdapter - Platform adapter implementation for tracking
|
|
98
97
|
*/
|
|
@@ -102,42 +101,40 @@ export declare class AnalyticsController extends BaseController<'AnalyticsContro
|
|
|
102
101
|
*
|
|
103
102
|
* Events are only tracked if analytics is enabled.
|
|
104
103
|
*
|
|
105
|
-
* @param
|
|
104
|
+
* @param eventName - The name of the event
|
|
105
|
+
* @param properties - Event properties
|
|
106
106
|
*/
|
|
107
|
-
trackEvent(
|
|
107
|
+
trackEvent(eventName: string, properties?: AnalyticsEventProperties): void;
|
|
108
108
|
/**
|
|
109
109
|
* Identify a user for analytics.
|
|
110
110
|
*
|
|
111
|
+
* @param userId - The user identifier (e.g., metametrics ID)
|
|
111
112
|
* @param traits - User traits/properties
|
|
112
113
|
*/
|
|
113
|
-
identify(traits?:
|
|
114
|
+
identify(userId: string, traits?: AnalyticsEventProperties): void;
|
|
114
115
|
/**
|
|
115
|
-
* Track a page
|
|
116
|
+
* Track a page view.
|
|
116
117
|
*
|
|
117
|
-
* @param
|
|
118
|
-
* @param properties -
|
|
118
|
+
* @param pageName - The name of the page
|
|
119
|
+
* @param properties - Page properties
|
|
119
120
|
*/
|
|
120
|
-
|
|
121
|
+
trackPage(pageName: string, properties?: AnalyticsEventProperties): void;
|
|
121
122
|
/**
|
|
122
|
-
*
|
|
123
|
-
* This updates the user's opt-in status for regular account.
|
|
123
|
+
* Enable analytics tracking.
|
|
124
124
|
*/
|
|
125
|
-
|
|
125
|
+
enable(): void;
|
|
126
126
|
/**
|
|
127
|
-
*
|
|
128
|
-
* This updates the user's opt-in status for regular account.
|
|
127
|
+
* Disable analytics tracking.
|
|
129
128
|
*/
|
|
130
|
-
|
|
129
|
+
disable(): void;
|
|
131
130
|
/**
|
|
132
|
-
* Opt in to analytics
|
|
133
|
-
* This updates the user's opt-in status for social account.
|
|
131
|
+
* Opt in to analytics.
|
|
134
132
|
*/
|
|
135
|
-
|
|
133
|
+
optIn(): void;
|
|
136
134
|
/**
|
|
137
|
-
* Opt out of analytics
|
|
138
|
-
* This updates the user's opt-in status for social account.
|
|
135
|
+
* Opt out of analytics.
|
|
139
136
|
*/
|
|
140
|
-
|
|
137
|
+
optOut(): void;
|
|
141
138
|
}
|
|
142
139
|
export {};
|
|
143
140
|
//# 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,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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AI,IAAI;IAMd;;OAEG;IACH,OAAO,IAAI,IAAI;IAMf;;OAEG;IACH,KAAK,IAAI,IAAI;IAMb;;OAEG;IACH,MAAM,IAAI,IAAI;CAKf"}
|
|
@@ -13,8 +13,6 @@ 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";
|
|
18
16
|
// === GENERAL ===
|
|
19
17
|
/**
|
|
20
18
|
* The name of the {@link AnalyticsController}, used to namespace the
|
|
@@ -26,13 +24,13 @@ export const controllerName = 'AnalyticsController';
|
|
|
26
24
|
* The metadata for each property in {@link AnalyticsControllerState}.
|
|
27
25
|
*/
|
|
28
26
|
const analyticsControllerMetadata = {
|
|
29
|
-
|
|
27
|
+
enabled: {
|
|
30
28
|
includeInStateLogs: true,
|
|
31
29
|
persist: true,
|
|
32
30
|
includeInDebugSnapshot: true,
|
|
33
31
|
usedInUi: true,
|
|
34
32
|
},
|
|
35
|
-
|
|
33
|
+
optedIn: {
|
|
36
34
|
includeInStateLogs: true,
|
|
37
35
|
persist: true,
|
|
38
36
|
includeInDebugSnapshot: true,
|
|
@@ -55,8 +53,8 @@ const analyticsControllerMetadata = {
|
|
|
55
53
|
*/
|
|
56
54
|
export function getDefaultAnalyticsControllerState() {
|
|
57
55
|
return {
|
|
58
|
-
|
|
59
|
-
|
|
56
|
+
enabled: true,
|
|
57
|
+
optedIn: false,
|
|
60
58
|
analyticsId: uuidv4(),
|
|
61
59
|
};
|
|
62
60
|
}
|
|
@@ -64,11 +62,11 @@ export function getDefaultAnalyticsControllerState() {
|
|
|
64
62
|
const MESSENGER_EXPOSED_METHODS = [
|
|
65
63
|
'trackEvent',
|
|
66
64
|
'identify',
|
|
67
|
-
'
|
|
68
|
-
'
|
|
69
|
-
'
|
|
70
|
-
'
|
|
71
|
-
'
|
|
65
|
+
'trackPage',
|
|
66
|
+
'enable',
|
|
67
|
+
'disable',
|
|
68
|
+
'optIn',
|
|
69
|
+
'optOut',
|
|
72
70
|
];
|
|
73
71
|
/**
|
|
74
72
|
* The AnalyticsController manages analytics tracking across platforms (Mobile/Extension).
|
|
@@ -85,136 +83,109 @@ export class AnalyticsController extends BaseController {
|
|
|
85
83
|
* Constructs an AnalyticsController instance.
|
|
86
84
|
*
|
|
87
85
|
* @param options - Controller options
|
|
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.
|
|
86
|
+
* @param options.state - Initial controller state (defaults from getDefaultAnalyticsControllerState)
|
|
90
87
|
* @param options.messenger - Messenger used to communicate with BaseController
|
|
91
88
|
* @param options.platformAdapter - Platform adapter implementation for tracking
|
|
92
89
|
*/
|
|
93
90
|
constructor({ state = {}, messenger, platformAdapter, }) {
|
|
94
|
-
const initialState = {
|
|
95
|
-
...getDefaultAnalyticsControllerState(),
|
|
96
|
-
...state,
|
|
97
|
-
};
|
|
98
|
-
validateAnalyticsState(initialState);
|
|
99
91
|
super({
|
|
100
92
|
name: controllerName,
|
|
101
93
|
metadata: analyticsControllerMetadata,
|
|
102
|
-
state:
|
|
94
|
+
state: {
|
|
95
|
+
...getDefaultAnalyticsControllerState(),
|
|
96
|
+
...state,
|
|
97
|
+
},
|
|
103
98
|
messenger,
|
|
104
99
|
});
|
|
105
100
|
_AnalyticsController_platformAdapter.set(this, void 0);
|
|
106
101
|
__classPrivateFieldSet(this, _AnalyticsController_platformAdapter, platformAdapter, "f");
|
|
107
102
|
this.messenger.registerMethodActionHandlers(this, MESSENGER_EXPOSED_METHODS);
|
|
108
103
|
projectLogger('AnalyticsController initialized and ready', {
|
|
109
|
-
enabled:
|
|
110
|
-
optedIn: this.state.
|
|
111
|
-
socialOptedIn: this.state.optedInForSocialAccount,
|
|
104
|
+
enabled: this.state.enabled,
|
|
105
|
+
optedIn: this.state.optedIn,
|
|
112
106
|
analyticsId: this.state.analyticsId,
|
|
113
107
|
});
|
|
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
|
-
}
|
|
123
108
|
}
|
|
124
109
|
/**
|
|
125
110
|
* Track an analytics event.
|
|
126
111
|
*
|
|
127
112
|
* Events are only tracked if analytics is enabled.
|
|
128
113
|
*
|
|
129
|
-
* @param
|
|
114
|
+
* @param eventName - The name of the event
|
|
115
|
+
* @param properties - Event properties
|
|
130
116
|
*/
|
|
131
|
-
trackEvent(
|
|
117
|
+
trackEvent(eventName, properties = {}) {
|
|
132
118
|
// Don't track if analytics is disabled
|
|
133
|
-
if (!
|
|
134
|
-
return;
|
|
135
|
-
}
|
|
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);
|
|
119
|
+
if (!this.state.enabled) {
|
|
142
120
|
return;
|
|
143
121
|
}
|
|
144
|
-
//
|
|
145
|
-
__classPrivateFieldGet(this, _AnalyticsController_platformAdapter, "f").
|
|
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
|
-
}
|
|
122
|
+
// Delegate to platform adapter
|
|
123
|
+
__classPrivateFieldGet(this, _AnalyticsController_platformAdapter, "f").trackEvent(eventName, properties);
|
|
156
124
|
}
|
|
157
125
|
/**
|
|
158
126
|
* Identify a user for analytics.
|
|
159
127
|
*
|
|
128
|
+
* @param userId - The user identifier (e.g., metametrics ID)
|
|
160
129
|
* @param traits - User traits/properties
|
|
161
130
|
*/
|
|
162
|
-
identify(traits) {
|
|
163
|
-
if (!
|
|
131
|
+
identify(userId, traits) {
|
|
132
|
+
if (!this.state.enabled) {
|
|
164
133
|
return;
|
|
165
134
|
}
|
|
166
|
-
//
|
|
135
|
+
// Update state with analytics ID
|
|
136
|
+
this.update((state) => {
|
|
137
|
+
state.analyticsId = userId;
|
|
138
|
+
});
|
|
139
|
+
// Delegate to platform adapter if supported
|
|
167
140
|
if (__classPrivateFieldGet(this, _AnalyticsController_platformAdapter, "f").identify) {
|
|
168
|
-
__classPrivateFieldGet(this, _AnalyticsController_platformAdapter, "f").identify(
|
|
141
|
+
__classPrivateFieldGet(this, _AnalyticsController_platformAdapter, "f").identify(userId, traits);
|
|
169
142
|
}
|
|
170
143
|
}
|
|
171
144
|
/**
|
|
172
|
-
* Track a page
|
|
145
|
+
* Track a page view.
|
|
173
146
|
*
|
|
174
|
-
* @param
|
|
175
|
-
* @param properties -
|
|
147
|
+
* @param pageName - The name of the page
|
|
148
|
+
* @param properties - Page properties
|
|
176
149
|
*/
|
|
177
|
-
|
|
178
|
-
if (!
|
|
150
|
+
trackPage(pageName, properties) {
|
|
151
|
+
if (!this.state.enabled) {
|
|
179
152
|
return;
|
|
180
153
|
}
|
|
181
|
-
// Delegate to platform adapter
|
|
182
|
-
__classPrivateFieldGet(this, _AnalyticsController_platformAdapter, "f").
|
|
154
|
+
// Delegate to platform adapter if supported
|
|
155
|
+
if (__classPrivateFieldGet(this, _AnalyticsController_platformAdapter, "f").trackPage) {
|
|
156
|
+
__classPrivateFieldGet(this, _AnalyticsController_platformAdapter, "f").trackPage(pageName, properties);
|
|
157
|
+
}
|
|
183
158
|
}
|
|
184
159
|
/**
|
|
185
|
-
*
|
|
186
|
-
* This updates the user's opt-in status for regular account.
|
|
160
|
+
* Enable analytics tracking.
|
|
187
161
|
*/
|
|
188
|
-
|
|
162
|
+
enable() {
|
|
189
163
|
this.update((state) => {
|
|
190
|
-
state.
|
|
164
|
+
state.enabled = true;
|
|
191
165
|
});
|
|
192
166
|
}
|
|
193
167
|
/**
|
|
194
|
-
*
|
|
195
|
-
* This updates the user's opt-in status for regular account.
|
|
168
|
+
* Disable analytics tracking.
|
|
196
169
|
*/
|
|
197
|
-
|
|
170
|
+
disable() {
|
|
198
171
|
this.update((state) => {
|
|
199
|
-
state.
|
|
172
|
+
state.enabled = false;
|
|
200
173
|
});
|
|
201
174
|
}
|
|
202
175
|
/**
|
|
203
|
-
* Opt in to analytics
|
|
204
|
-
* This updates the user's opt-in status for social account.
|
|
176
|
+
* Opt in to analytics.
|
|
205
177
|
*/
|
|
206
|
-
|
|
178
|
+
optIn() {
|
|
207
179
|
this.update((state) => {
|
|
208
|
-
state.
|
|
180
|
+
state.optedIn = true;
|
|
209
181
|
});
|
|
210
182
|
}
|
|
211
183
|
/**
|
|
212
|
-
* Opt out of analytics
|
|
213
|
-
* This updates the user's opt-in status for social account.
|
|
184
|
+
* Opt out of analytics.
|
|
214
185
|
*/
|
|
215
|
-
|
|
186
|
+
optOut() {
|
|
216
187
|
this.update((state) => {
|
|
217
|
-
state.
|
|
188
|
+
state.optedIn = false;
|
|
218
189
|
});
|
|
219
190
|
}
|
|
220
191
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AnalyticsController.mjs","sourceRoot":"","sources":["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;;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
|
+
{"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,OAAO;QACL,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AG,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 +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 * Platform adapter interface for analytics tracking\n * Implementations should handle platform-specific details (Segment SDK, etc.)\n *\n * @todo This type is work in progress and will be updated as we\n * integrate with the new analytics system on mobile.\n * We have this draft type to help us iterate on the implementation.\n * It will be updated with proper types as we create the mobile adapter\n * And the controller package will be released only when this is completed.\n */\nexport type AnalyticsPlatformAdapter = {\n /**\n * Track an analytics event\n *\n * @param eventName - The name of the event\n * @param properties - Event properties\n */\n trackEvent(eventName: string, properties: AnalyticsEventProperties): void;\n\n /**\n * Identify a user\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\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};\n"]}
|