1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-23 09:52:26 +01:00

Add 'Transaction Added' metric event to TransactionController (#11341)

This commit is contained in:
ryanml 2021-06-21 12:02:43 -07:00
parent fed9c86abb
commit 077ee16ec2
2 changed files with 114 additions and 0 deletions

View File

@ -151,8 +151,32 @@ export default class TransactionController extends EventEmitter {
@emits ${txMeta.id}:unapproved @emits ${txMeta.id}:unapproved
*/ */
addTransaction(txMeta) { addTransaction(txMeta) {
const {
type,
status,
chainId,
origin: referrer,
txParams: { gasPrice },
metamaskNetworkId: network,
} = txMeta;
const source = referrer === 'metamask' ? 'user' : 'dapp';
this.txStateManager.addTransaction(txMeta); this.txStateManager.addTransaction(txMeta);
this.emit(`${txMeta.id}:unapproved`, txMeta); this.emit(`${txMeta.id}:unapproved`, txMeta);
this._trackMetaMetricsEvent({
event: 'Transaction Added',
category: 'Transactions',
sensitiveProperties: {
type,
status,
gasPrice,
referrer,
source,
network,
chain_id: chainId,
},
});
} }
/** /**

View File

@ -56,6 +56,7 @@ describe('Transaction Controller', function () {
getPermittedAccounts: () => undefined, getPermittedAccounts: () => undefined,
getCurrentChainId: () => currentChainId, getCurrentChainId: () => currentChainId,
getParticipateInMetrics: () => false, getParticipateInMetrics: () => false,
trackMetaMetricsEvent: () => undefined,
}); });
txController.nonceTracker.getNonceLock = () => txController.nonceTracker.getNonceLock = () =>
Promise.resolve({ nextNonce: 0, releaseLock: noop }); Promise.resolve({ nextNonce: 0, releaseLock: noop });
@ -414,6 +415,19 @@ describe('Transaction Controller', function () {
}); });
describe('#addTransaction', function () { describe('#addTransaction', function () {
let trackMetaMetricsEventSpy;
beforeEach(function () {
trackMetaMetricsEventSpy = sinon.spy(
txController,
'_trackMetaMetricsEvent',
);
});
afterEach(function () {
trackMetaMetricsEventSpy.restore();
});
it('should emit updates', function (done) { it('should emit updates', function (done) {
const txMeta = { const txMeta = {
id: '1', id: '1',
@ -451,6 +465,82 @@ describe('Transaction Controller', function () {
.catch(done); .catch(done);
txController.addTransaction(txMeta); 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 () { describe('#approveTransaction', function () {