Skip to content

Commit 2673a3b

Browse files
committed
Add #trackMetaMetricsEvent tracker property and call
1 parent 0ded3cb commit 2673a3b

1 file changed

Lines changed: 35 additions & 1 deletion

File tree

packages/assets-controllers/src/TokenDetectionController.ts

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,16 @@ export class TokenDetectionController extends StaticIntervalPollingController<
111111

112112
readonly #getTokensState: () => TokensState;
113113

114+
readonly #trackMetaMetricsEvent: (options: {
115+
event: string;
116+
category: string;
117+
properties: {
118+
tokens: string[];
119+
token_standard: string;
120+
asset_type: string;
121+
};
122+
}) => void;
123+
114124
/**
115125
* Creates a TokenDetectionController instance.
116126
*
@@ -125,6 +135,7 @@ export class TokenDetectionController extends StaticIntervalPollingController<
125135
* @param options.getBalancesInSingleCall - Gets the balances of a list of tokens for the given address.
126136
* @param options.getTokensState - Gets the current state of the Tokens controller.
127137
* @param options.getPreferencesState - Gets the state of the preferences controller.
138+
* @param options.trackMetaMetricsEvent - Sets options for MetaMetrics event tracking.
128139
*/
129140
constructor({
130141
networkClientId,
@@ -136,6 +147,7 @@ export class TokenDetectionController extends StaticIntervalPollingController<
136147
addDetectedTokens,
137148
getPreferencesState,
138149
getTokensState,
150+
trackMetaMetricsEvent,
139151
messenger,
140152
}: {
141153
networkClientId: NetworkClientId;
@@ -149,6 +161,15 @@ export class TokenDetectionController extends StaticIntervalPollingController<
149161
getBalancesInSingleCall: AssetsContractController['getBalancesInSingleCall'];
150162
getTokensState: () => TokensState;
151163
getPreferencesState: () => PreferencesState;
164+
trackMetaMetricsEvent: (options: {
165+
event: string;
166+
category: string;
167+
properties: {
168+
tokens: string[];
169+
token_standard: string;
170+
asset_type: string;
171+
};
172+
}) => void;
152173
messenger: TokenDetectionControllerMessenger;
153174
}) {
154175
const { useTokenDetection: defaultUseTokenDetection } =
@@ -177,6 +198,8 @@ export class TokenDetectionController extends StaticIntervalPollingController<
177198
this.#getBalancesInSingleCall = getBalancesInSingleCall;
178199
this.#getTokensState = getTokensState;
179200

201+
this.#trackMetaMetricsEvent = trackMetaMetricsEvent;
202+
180203
this.messagingSystem.subscribe(
181204
'TokenListController:stateChange',
182205
async ({ tokenList }) => {
@@ -388,7 +411,7 @@ export class TokenDetectionController extends StaticIntervalPollingController<
388411
return;
389412
}
390413
const { tokens } = this.#getTokensState();
391-
const selectedAddress = accountAddress || this.#selectedAddress;
414+
const selectedAddress = accountAddress ?? this.#selectedAddress;
392415
const chainId = this.#getCorrectChainId(networkClientId);
393416

394417
const tokensAddresses = tokens.map(
@@ -426,6 +449,7 @@ export class TokenDetectionController extends StaticIntervalPollingController<
426449
tokensSlice,
427450
);
428451
const tokensToAdd: Token[] = [];
452+
const eventTokensDetails = [];
429453
for (const tokenAddress of Object.keys(balances)) {
430454
let ignored;
431455
/* istanbul ignore else */
@@ -444,6 +468,7 @@ export class TokenDetectionController extends StaticIntervalPollingController<
444468
if (ignored === undefined) {
445469
const { decimals, symbol, aggregators, iconUrl, name } =
446470
tokenList[caseInsensitiveTokenKey];
471+
eventTokensDetails.push(`${symbol} - ${tokenAddress}`);
447472
tokensToAdd.push({
448473
address: tokenAddress,
449474
decimals,
@@ -457,6 +482,15 @@ export class TokenDetectionController extends StaticIntervalPollingController<
457482
}
458483

459484
if (tokensToAdd.length) {
485+
this.#trackMetaMetricsEvent({
486+
event: 'Token Detected',
487+
category: 'Wallet',
488+
properties: {
489+
tokens: eventTokensDetails,
490+
token_standard: 'ERC20',
491+
asset_type: 'TOKEN',
492+
},
493+
});
460494
await this.#addDetectedTokens(tokensToAdd, {
461495
selectedAddress,
462496
chainId,

0 commit comments

Comments
 (0)