Skip to content

feat(Snaps): Change Snap export tracking approach #31553

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ describe('SnapControllerInit', () => {
getFeatureFlags: expect.any(Function),
getMnemonicSeed: expect.any(Function),
preinstalledSnaps: expect.any(Array),
trackEvent: expect.any(Function),
});
});
});
3 changes: 3 additions & 0 deletions app/scripts/controller-init/snaps/snap-controller-init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { getBooleanFlag } from '../../lib/util';
* @param request.removeAllConnections - Function to remove all connections for
* a given origin.
* @param request.preinstalledSnaps - The list of preinstalled Snaps.
* @param request.trackEvent - Event tracking hook.
* @returns The initialized controller.
*/
export const SnapControllerInit: ControllerInitFunction<
Expand All @@ -38,6 +39,7 @@ export const SnapControllerInit: ControllerInitFunction<
persistedState,
removeAllConnections,
preinstalledSnaps,
trackEvent,
}) => {
const allowLocalSnaps = getBooleanFlag(process.env.ALLOW_LOCAL_SNAPS);
const requireAllowlist = getBooleanFlag(process.env.REQUIRE_SNAPS_ALLOWLIST);
Expand Down Expand Up @@ -109,6 +111,7 @@ export const SnapControllerInit: ControllerInitFunction<

preinstalledSnaps,
getFeatureFlags,
trackEvent,
});

return {
Expand Down
49 changes: 5 additions & 44 deletions app/scripts/metamask-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { createEngineStream } from '@metamask/json-rpc-middleware-stream';
import { ObservableStore } from '@metamask/obs-store';
import { storeAsStream } from '@metamask/obs-store/dist/asStream';
import { providerAsMiddleware } from '@metamask/eth-json-rpc-middleware';
import { debounce, throttle, memoize, wrap, pick, uniq } from 'lodash';
import { debounce, pick, uniq } from 'lodash';
import {
KeyringController,
KeyringTypes,
Expand Down Expand Up @@ -2412,38 +2412,6 @@ export default class MetamaskController extends EventEmitter {
return this.snapsRegistry.state.database?.verifiedSnaps?.[snapId]?.metadata;
}

/**
* Tracks snaps export usage.
* Note: This function is throttled to 1 call per 60 seconds per snap id + handler combination.
*
* @param {string} snapId - The ID of the snap the handler is being triggered on.
* @param {string} handler - The handler to trigger on the snap for the request.
* @param {boolean} success - Whether the invocation was successful or not.
* @param {string} origin - The origin of the request.
*/
_trackSnapExportUsage = wrap(
memoize(
() =>
throttle(
(snapId, handler, success, origin) =>
this.metaMetricsController.trackEvent({
event: MetaMetricsEventName.SnapExportUsed,
category: MetaMetricsEventCategory.Snaps,
properties: {
snap_id: snapId,
export: handler,
snap_category: this._getSnapMetadata(snapId)?.category,
success,
origin,
},
}),
SECOND * 60,
),
(snapId, handler, _, origin) => `${snapId}${handler}${origin}`,
),
(getFunc, ...args) => getFunc(...args)(...args),
);

/**
* Passes a JSON-RPC request object to the SnapController for execution.
*
Expand All @@ -2455,17 +2423,10 @@ export default class MetamaskController extends EventEmitter {
* @returns The result of the JSON-RPC request.
*/
async handleSnapRequest(args) {
try {
const response = await this.controllerMessenger.call(
'SnapController:handleRequest',
args,
);
this._trackSnapExportUsage(args.snapId, args.handler, true, args.origin);
return response;
} catch (error) {
this._trackSnapExportUsage(args.snapId, args.handler, false, args.origin);
throw error;
}
return await this.controllerMessenger.call(
'SnapController:handleRequest',
args,
);
}

/**
Expand Down
Loading