From 2529b360bef7f9ac114167d2a29ef5c165c4a7e9 Mon Sep 17 00:00:00 2001 From: Frederik Bolding Date: Thu, 17 Aug 2023 16:13:40 +0200 Subject: [PATCH] Track usage of snap exports (#20503) * Track usage of snap exports * Fix fencing * Small change to event name * Use MetaMetricsEventName --- app/scripts/metamask-controller.js | 54 ++++++++++++++++++++++++------ shared/constants/metametrics.ts | 5 +++ 2 files changed, 48 insertions(+), 11 deletions(-) diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index 8daf37320..6bc0d9531 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -5,7 +5,12 @@ import { storeAsStream } from '@metamask/obs-store/dist/asStream'; import { JsonRpcEngine } from 'json-rpc-engine'; import { createEngineStream } from 'json-rpc-middleware-stream'; import { providerAsMiddleware } from '@metamask/eth-json-rpc-middleware'; -import { debounce } from 'lodash'; +import { + debounce, + ///: BEGIN:ONLY_INCLUDE_IN(snaps) + throttle, + ///: END:ONLY_INCLUDE_IN +} from 'lodash'; import { keyringBuilderFactory } from '@metamask/eth-keyring-controller'; import { KeyringController } from '@metamask/keyring-controller'; import createFilterMiddleware from 'eth-json-rpc-filters'; @@ -1848,6 +1853,39 @@ export default class MetamaskController extends EventEmitter { ///: BEGIN:ONLY_INCLUDE_IN(snaps) + /** + * Tracks snaps export usage. Note: This function is throttled to 1 call per 60 seconds. + * + * @param {string} handler - The handler to trigger on the snap for the request. + */ + _trackSnapExportUsage = throttle( + (handler) => + this.metaMetricsController.trackEvent({ + event: MetaMetricsEventName.SnapExportUsed, + category: MetaMetricsEventCategory.Snaps, + properties: { + export: handler, + }, + }), + SECOND * 60, + ); + + /** + * Passes a JSON-RPC request object to the SnapController for execution. + * + * @param {object} args - A bag of options. + * @param {string} args.snapId - The ID of the recipient snap. + * @param {string} args.origin - The origin of the RPC request. + * @param {string} args.handler - The handler to trigger on the snap for the request. + * @param {object} args.request - The JSON-RPC request object. + * @returns The result of the JSON-RPC request. + */ + handleSnapRequest(args) { + this._trackSnapExportUsage(args.handler); + + return this.controllerMessenger.call('SnapController:handleRequest', args); + } + /** * Constructor helper for getting Snap permission specifications. */ @@ -1869,10 +1907,7 @@ export default class MetamaskController extends EventEmitter { this.controllerMessenger, 'SnapController:get', ), - handleSnapRpcRequest: this.controllerMessenger.call.bind( - this.controllerMessenger, - 'SnapController:handleRequest', - ), + handleSnapRpcRequest: this.handleSnapRequest.bind(this), getSnapState: this.controllerMessenger.call.bind( this.controllerMessenger, 'SnapController:getSnapState', @@ -2024,7 +2059,7 @@ export default class MetamaskController extends EventEmitter { `${this.snapController.name}:snapInstalled`, (truncatedSnap) => { this.metaMetricsController.trackEvent({ - event: 'Snap Installed', + event: MetaMetricsEventName.SnapInstalled, category: MetaMetricsEventCategory.Snaps, properties: { snap_id: truncatedSnap.id, @@ -2038,7 +2073,7 @@ export default class MetamaskController extends EventEmitter { `${this.snapController.name}:snapUpdated`, (newSnap, oldVersion) => { this.metaMetricsController.trackEvent({ - event: 'Snap Updated', + event: MetaMetricsEventName.SnapUpdated, category: MetaMetricsEventCategory.Snaps, properties: { snap_id: newSnap.id, @@ -2607,10 +2642,7 @@ export default class MetamaskController extends EventEmitter { this.controllerMessenger, 'SnapController:remove', ), - handleSnapRequest: this.controllerMessenger.call.bind( - this.controllerMessenger, - 'SnapController:handleRequest', - ), + handleSnapRequest: this.handleSnapRequest.bind(this), revokeDynamicSnapPermissions: this.controllerMessenger.call.bind( this.controllerMessenger, 'SnapController:revokeDynamicPermissions', diff --git a/shared/constants/metametrics.ts b/shared/constants/metametrics.ts index cbbca92ca..3b2ed005b 100644 --- a/shared/constants/metametrics.ts +++ b/shared/constants/metametrics.ts @@ -625,6 +625,11 @@ export enum MetaMetricsEventName { TransactionFinalized = 'Transaction Finalized', ExitedSwaps = 'Exited Swaps', SwapError = 'Swap Error', + ///: BEGIN:ONLY_INCLUDE_IN(snaps) + SnapInstalled = 'Snap Installed', + SnapUpdated = 'Snap Updated', + SnapExportUsed = 'Snap Export Used', + ///: END:ONLY_INCLUDE_IN } export enum MetaMetricsEventAccountType {