Skip to content

Commit 46b6652

Browse files
committed
feat(metametrics): add events during snap account removal
1 parent 0fed5fd commit 46b6652

File tree

3 files changed

+73
-7
lines changed

3 files changed

+73
-7
lines changed

app/scripts/lib/snap-keyring/snap-keyring.ts

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,15 +234,30 @@ export const snapKeyringBuilder = (
234234
snapId: string,
235235
handleUserInput: (accepted: boolean) => Promise<void>,
236236
) => {
237+
const snapName = getSnapName(controllerMessenger, snapId);
237238
const { id: removeAccountApprovalId } = controllerMessenger.call(
238239
'ApprovalController:startFlow',
239240
);
240241

241242
const learnMoreLink =
242243
'https://support.metamask.io/hc/en-us/articles/360057435092-How-to-remove-an-account-from-your-MetaMask-wallet';
243244

245+
const trackSnapAccountEvent = (event: MetaMetricsEventName) => {
246+
trackEvent({
247+
event,
248+
category: MetaMetricsEventCategory.Accounts,
249+
properties: {
250+
account_type: MetaMetricsEventAccountType.Snap,
251+
snap_id: snapId,
252+
snap_name: snapName,
253+
},
254+
});
255+
};
256+
257+
// Since we use this in the finally, better to give it a default value if the controller call fails
258+
let confirmationResult = false;
244259
try {
245-
const confirmationResult = Boolean(
260+
confirmationResult = Boolean(
246261
await controllerMessenger.call(
247262
'ApprovalController:addRequest',
248263
{
@@ -260,6 +275,10 @@ export const snapKeyringBuilder = (
260275
await handleUserInput(confirmationResult);
261276
await persistKeyringHelper();
262277

278+
// User should now see the "Successfuly removed account" page
279+
trackSnapAccountEvent(
280+
MetaMetricsEventName.RemoveSnapAccountSuccessViewed,
281+
);
263282
// This isn't actually an error, but we show it as one for styling reasons
264283
await showError(
265284
controllerMessenger,
@@ -273,10 +292,14 @@ export const snapKeyringBuilder = (
273292
learnMoreLink,
274293
},
275294
);
295+
296+
// User has clicked on "OK"
297+
trackSnapAccountEvent(
298+
MetaMetricsEventName.RemoveSnapAccountSuccessClicked,
299+
);
276300
} catch (e) {
277301
const error = (e as Error).message;
278302

279-
const snapName = getSnapName(controllerMessenger, snapId);
280303
await showError(
281304
controllerMessenger,
282305
snapId,
@@ -300,9 +323,19 @@ export const snapKeyringBuilder = (
300323
}
301324
} else {
302325
await handleUserInput(confirmationResult);
326+
trackSnapAccountEvent(
327+
MetaMetricsEventName.RemoveSnapAccountCancelled,
328+
);
329+
303330
throw new Error('User denied account removal');
304331
}
305332
} finally {
333+
trackSnapAccountEvent(
334+
confirmationResult
335+
? MetaMetricsEventName.AccountRemoved
336+
: MetaMetricsEventName.AccountRemoveFailed,
337+
);
338+
306339
controllerMessenger.call('ApprovalController:endFlow', {
307340
id: removeAccountApprovalId,
308341
});

shared/constants/metametrics.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -638,6 +638,7 @@ export enum MetaMetricsEventName {
638638
AccountDetailMenuOpened = 'Account Details Menu Opened',
639639
BlockExplorerLinkClicked = 'Block Explorer Clicked',
640640
AccountRemoved = 'Account Removed',
641+
AccountRemoveFailed = 'Account Remove Failed',
641642
TestNetworksDisplayed = 'Test Networks Displayed',
642643
AddNetworkButtonClick = 'Add Network Button Clicked',
643644
CustomNetworkAdded = 'Custom Network Added',
@@ -682,6 +683,11 @@ export enum MetaMetricsEventName {
682683
AddSnapAccountCancelled = 'Add Snap Account Cancelled',
683684
AddSnapAccountSuccessViewed = 'Add Snap Account Success Viewed',
684685
AddSnapAccountSuccessClicked = 'Add Snap Account Success Clicked',
686+
RemoveSnapAccountViewed = 'Remove Snap Account Viewed',
687+
RemoveSnapAccountConfirmed = 'Remove Snap Account Confirmed',
688+
RemoveSnapAccountCancelled = 'Remove Snap Account Cancelled',
689+
RemoveSnapAccountSuccessViewed = 'Remove Snap Account Success Viewed',
690+
RemoveSnapAccountSuccessClicked = 'Remove Snap Account Success Clicked',
685691
SnapAccountTransactionLoadingViewed = 'Snap Account Transaction Loading Viewed',
686692
SnapAccountTransactionFinalizeViewed = 'Snap Account Transaction Finalize Viewed',
687693
SnapAccountTransactionFinalizeRedirectGoToSiteClicked = 'Snap Account Transaction Finalize Redirect "Go To Site" Clicked',

ui/pages/confirmations/confirmation/templates/remove-snap-account.js

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,25 @@
1-
function getValues(pendingApproval, t, actions) {
1+
import {
2+
MetaMetricsEventCategory,
3+
MetaMetricsEventName,
4+
MetaMetricsEventAccountType,
5+
} from '../../../../../shared/constants/metametrics';
6+
7+
function getValues(pendingApproval, t, actions, _history, _data, contexts) {
28
const { origin: snapId, snapName } = pendingApproval;
39
const { publicAddress } = pendingApproval.requestData;
10+
const { trackEvent } = contexts;
11+
12+
const trackSnapAccountEvent = (event) => {
13+
trackEvent({
14+
event,
15+
category: MetaMetricsEventCategory.Accounts,
16+
properties: {
17+
account_type: MetaMetricsEventAccountType.Snap,
18+
snap_id: snapId,
19+
snap_name: snapName,
20+
},
21+
});
22+
};
423

524
return {
625
content: [
@@ -16,13 +35,21 @@ function getValues(pendingApproval, t, actions) {
1635
],
1736
cancelText: t('cancel'),
1837
submitText: t('remove'),
19-
onSubmit: () => actions.resolvePendingApproval(pendingApproval.id, true),
20-
onCancel: () => actions.resolvePendingApproval(pendingApproval.id, false),
38+
onLoad: () =>
39+
trackSnapAccountEvent(MetaMetricsEventName.RemoveSnapAccountViewed),
40+
onSubmit: () => {
41+
trackSnapAccountEvent(MetaMetricsEventName.RemoveSnapAccountConfirmed);
42+
actions.resolvePendingApproval(pendingApproval.id, true);
43+
},
44+
onCancel: () => {
45+
trackSnapAccountEvent(MetaMetricsEventName.RemoveSnapAccountCancelled);
46+
actions.resolvePendingApproval(pendingApproval.id, false);
47+
},
2148
};
2249
}
2350

24-
const createSnapAccount = {
51+
const removeSnapAccount = {
2552
getValues,
2653
};
2754

28-
export default createSnapAccount;
55+
export default removeSnapAccount;

0 commit comments

Comments
 (0)