Skip to content

Commit c85832f

Browse files
david0xdMrtenz
authored andcommitted
feat(Snaps): Change Snap export tracking approach (#31553)
## **Description** This PR is the next step in moving Snap export tracking away from the extension and transferring its functionality and responsibility to Snap Controller. ## **Notes** - Requires Snaps release. - Snaps PR: MetaMask/snaps#3281 - Targets Snaps release when ready. [![Open in GitHub Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/MetaMask/metamask-extension/pull/31553?quickstart=1) ## **Related issues** Fixes: MetaMask/snaps#3259 ## **Manual testing steps** 1. Find a way to verify that events are tracked (test locally, add console log, etc.) ## **Screenshots/Recordings** ### **Before** n/a ### **After** n/a ## **Pre-merge author checklist** - [ ] I've followed [MetaMask Contributor Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask Extension Coding Standards](https://github.com/MetaMask/metamask-extension/blob/main/.github/guidelines/CODING_GUIDELINES.md). - [ ] I've completed the PR template to the best of my ability - [ ] I’ve included tests if applicable - [ ] I’ve documented my code using [JSDoc](https://jsdoc.app/) format if applicable - [ ] I’ve applied the right labels on the PR (see [labeling guidelines](https://github.com/MetaMask/metamask-extension/blob/main/.github/guidelines/LABELING_GUIDELINES.md)). Not required for external contributors. ## **Pre-merge reviewer checklist** - [ ] I've manually tested the PR (e.g. pull and build branch, run the app, test code being changed). - [ ] I confirm that this PR addresses all acceptance criteria described in the ticket it closes and includes the necessary testing evidence such as recordings and or screenshots.
1 parent 7b1455f commit c85832f

File tree

3 files changed

+9
-44
lines changed

3 files changed

+9
-44
lines changed

app/scripts/controller-init/snaps/snap-controller-init.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ describe('SnapControllerInit', () => {
5353
getFeatureFlags: expect.any(Function),
5454
getMnemonicSeed: expect.any(Function),
5555
preinstalledSnaps: expect.any(Array),
56+
trackEvent: expect.any(Function),
5657
});
5758
});
5859
});

app/scripts/controller-init/snaps/snap-controller-init.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import { getBooleanFlag } from '../../lib/util';
2626
* @param request.removeAllConnections - Function to remove all connections for
2727
* a given origin.
2828
* @param request.preinstalledSnaps - The list of preinstalled Snaps.
29+
* @param request.trackEvent - Event tracking hook.
2930
* @returns The initialized controller.
3031
*/
3132
export const SnapControllerInit: ControllerInitFunction<
@@ -38,6 +39,7 @@ export const SnapControllerInit: ControllerInitFunction<
3839
persistedState,
3940
removeAllConnections,
4041
preinstalledSnaps,
42+
trackEvent,
4143
}) => {
4244
const allowLocalSnaps = getBooleanFlag(process.env.ALLOW_LOCAL_SNAPS);
4345
const requireAllowlist = getBooleanFlag(process.env.REQUIRE_SNAPS_ALLOWLIST);
@@ -109,6 +111,7 @@ export const SnapControllerInit: ControllerInitFunction<
109111

110112
preinstalledSnaps,
111113
getFeatureFlags,
114+
trackEvent,
112115
});
113116

114117
return {

app/scripts/metamask-controller.js

Lines changed: 5 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { createEngineStream } from '@metamask/json-rpc-middleware-stream';
1414
import { ObservableStore } from '@metamask/obs-store';
1515
import { storeAsStream } from '@metamask/obs-store/dist/asStream';
1616
import { providerAsMiddleware } from '@metamask/eth-json-rpc-middleware';
17-
import { debounce, throttle, memoize, wrap, pick, uniq } from 'lodash';
17+
import { debounce, pick, uniq } from 'lodash';
1818
import {
1919
KeyringController,
2020
KeyringTypes,
@@ -2509,38 +2509,6 @@ export default class MetamaskController extends EventEmitter {
25092509
return this.snapsRegistry.state.database?.verifiedSnaps?.[snapId]?.metadata;
25102510
}
25112511

2512-
/**
2513-
* Tracks snaps export usage.
2514-
* Note: This function is throttled to 1 call per 60 seconds per snap id + handler combination.
2515-
*
2516-
* @param {string} snapId - The ID of the snap the handler is being triggered on.
2517-
* @param {string} handler - The handler to trigger on the snap for the request.
2518-
* @param {boolean} success - Whether the invocation was successful or not.
2519-
* @param {string} origin - The origin of the request.
2520-
*/
2521-
_trackSnapExportUsage = wrap(
2522-
memoize(
2523-
() =>
2524-
throttle(
2525-
(snapId, handler, success, origin) =>
2526-
this.metaMetricsController.trackEvent({
2527-
event: MetaMetricsEventName.SnapExportUsed,
2528-
category: MetaMetricsEventCategory.Snaps,
2529-
properties: {
2530-
snap_id: snapId,
2531-
export: handler,
2532-
snap_category: this._getSnapMetadata(snapId)?.category,
2533-
success,
2534-
origin,
2535-
},
2536-
}),
2537-
SECOND * 60,
2538-
),
2539-
(snapId, handler, _, origin) => `${snapId}${handler}${origin}`,
2540-
),
2541-
(getFunc, ...args) => getFunc(...args)(...args),
2542-
);
2543-
25442512
/**
25452513
* Passes a JSON-RPC request object to the SnapController for execution.
25462514
*
@@ -2552,17 +2520,10 @@ export default class MetamaskController extends EventEmitter {
25522520
* @returns The result of the JSON-RPC request.
25532521
*/
25542522
async handleSnapRequest(args) {
2555-
try {
2556-
const response = await this.controllerMessenger.call(
2557-
'SnapController:handleRequest',
2558-
args,
2559-
);
2560-
this._trackSnapExportUsage(args.snapId, args.handler, true, args.origin);
2561-
return response;
2562-
} catch (error) {
2563-
this._trackSnapExportUsage(args.snapId, args.handler, false, args.origin);
2564-
throw error;
2565-
}
2523+
return await this.controllerMessenger.call(
2524+
'SnapController:handleRequest',
2525+
args,
2526+
);
25662527
}
25672528

25682529
/**

0 commit comments

Comments
 (0)