From 077ee16ec2018227875130ba43501a92b613db20 Mon Sep 17 00:00:00 2001 From: ryanml Date: Mon, 21 Jun 2021 12:02:43 -0700 Subject: [PATCH] Add 'Transaction Added' metric event to TransactionController (#11341) --- app/scripts/controllers/transactions/index.js | 24 +++++ .../controllers/transactions/index.test.js | 90 +++++++++++++++++++ 2 files changed, 114 insertions(+) diff --git a/app/scripts/controllers/transactions/index.js b/app/scripts/controllers/transactions/index.js index fa7c46a2b..a67a71e4a 100644 --- a/app/scripts/controllers/transactions/index.js +++ b/app/scripts/controllers/transactions/index.js @@ -151,8 +151,32 @@ export default class TransactionController extends EventEmitter { @emits ${txMeta.id}:unapproved */ addTransaction(txMeta) { + const { + type, + status, + chainId, + origin: referrer, + txParams: { gasPrice }, + metamaskNetworkId: network, + } = txMeta; + const source = referrer === 'metamask' ? 'user' : 'dapp'; + this.txStateManager.addTransaction(txMeta); this.emit(`${txMeta.id}:unapproved`, txMeta); + + this._trackMetaMetricsEvent({ + event: 'Transaction Added', + category: 'Transactions', + sensitiveProperties: { + type, + status, + gasPrice, + referrer, + source, + network, + chain_id: chainId, + }, + }); } /** diff --git a/app/scripts/controllers/transactions/index.test.js b/app/scripts/controllers/transactions/index.test.js index 3615c33d0..d5cfe8de0 100644 --- a/app/scripts/controllers/transactions/index.test.js +++ b/app/scripts/controllers/transactions/index.test.js @@ -56,6 +56,7 @@ describe('Transaction Controller', function () { getPermittedAccounts: () => undefined, getCurrentChainId: () => currentChainId, getParticipateInMetrics: () => false, + trackMetaMetricsEvent: () => undefined, }); txController.nonceTracker.getNonceLock = () => Promise.resolve({ nextNonce: 0, releaseLock: noop }); @@ -414,6 +415,19 @@ describe('Transaction Controller', function () { }); describe('#addTransaction', function () { + let trackMetaMetricsEventSpy; + + beforeEach(function () { + trackMetaMetricsEventSpy = sinon.spy( + txController, + '_trackMetaMetricsEvent', + ); + }); + + afterEach(function () { + trackMetaMetricsEventSpy.restore(); + }); + it('should emit updates', function (done) { const txMeta = { id: '1', @@ -451,6 +465,82 @@ describe('Transaction Controller', function () { .catch(done); txController.addTransaction(txMeta); }); + + it('should call _trackMetaMetricsEvent with the correct payload (one)', function () { + const txMeta = { + id: 1, + status: TRANSACTION_STATUSES.UNAPPROVED, + txParams: { + from: fromAccount.address, + to: '0x1678a085c290ebd122dc42cba69373b5953b831d', + gasPrice: '0x77359400', + gas: '0x7b0d', + nonce: '0x4b', + }, + type: 'sentEther', + origin: 'metamask', + chainId: currentChainId, + metamaskNetworkId: currentNetworkId, + }; + const expectedPayload = { + event: 'Transaction Added', + category: 'Transactions', + sensitiveProperties: { + chain_id: '0x2a', + gasPrice: '0x77359400', + network: '42', + referrer: 'metamask', + source: 'user', + status: 'unapproved', + type: 'sentEther', + }, + }; + + txController.addTransaction(txMeta); + assert.equal(trackMetaMetricsEventSpy.callCount, 1); + assert.deepEqual( + trackMetaMetricsEventSpy.getCall(0).args[0], + expectedPayload, + ); + }); + + it('should call _trackMetaMetricsEvent with the correct payload (two)', function () { + const txMeta = { + id: 1, + status: TRANSACTION_STATUSES.UNAPPROVED, + txParams: { + from: fromAccount.address, + to: '0x1678a085c290ebd122dc42cba69373b5953b831d', + gasPrice: '0x77359400', + gas: '0x7b0d', + nonce: '0x4b', + }, + type: 'sentEther', + origin: 'other', + chainId: '0x3', + metamaskNetworkId: '3', + }; + const expectedPayload = { + event: 'Transaction Added', + category: 'Transactions', + sensitiveProperties: { + chain_id: '0x3', + gasPrice: '0x77359400', + network: '3', + referrer: 'other', + source: 'dapp', + status: 'unapproved', + type: 'sentEther', + }, + }; + + txController.addTransaction(txMeta); + assert.equal(trackMetaMetricsEventSpy.callCount, 1); + assert.deepEqual( + trackMetaMetricsEventSpy.getCall(0).args[0], + expectedPayload, + ); + }); }); describe('#approveTransaction', function () {