Skip to content

Commit

Permalink
Merge pull request #21943 from MetaMask/cherry-pick-confirmations
Browse files Browse the repository at this point in the history
[cherry-pick Confirmations Metrics PRs] fix: After triggering a Transaction, a Signature request metrics even…
  • Loading branch information
danjm authored Nov 23, 2023
2 parents d16b3eb + 3ef4a2c commit 6d1de3e
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 151 deletions.
32 changes: 29 additions & 3 deletions app/scripts/lib/transaction/metrics.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ import {
} from '../../../../shared/constants/metametrics';
import { TRANSACTION_ENVELOPE_TYPE_NAMES } from '../../../../shared/lib/transactions-controller-utils';
///: BEGIN:ONLY_INCLUDE_IN(blockaid)
import { BlockaidReason } from '../../../../shared/constants/security-provider';
import {
BlockaidReason,
BlockaidResultType,
} from '../../../../shared/constants/security-provider';
///: END:ONLY_INCLUDE_IN(blockaid)
import {
handleTransactionAdded,
Expand Down Expand Up @@ -107,8 +110,9 @@ describe('Transaction metrics', () => {
// copy mockTransactionMeta and add blockaid data
mockTransactionMetaWithBlockaid = {
...JSON.parse(JSON.stringify(mockTransactionMeta)),
securityProviderResponse: {
flagAsDangerous: 1,
securityAlertResponse: {
result_type: BlockaidResultType.Malicious,
reason: BlockaidReason.maliciousDomain,
providerRequestsCount: {
eth_call: 5,
eth_getCode: 3,
Expand Down Expand Up @@ -230,6 +234,8 @@ describe('Transaction metrics', () => {
persist: true,
properties: {
...expectedProperties,
security_alert_reason: BlockaidReason.maliciousDomain,
security_alert_response: 'Malicious',
ui_customizations: ['flagged_as_malicious'],
ppom_eth_call_count: 5,
ppom_eth_getCode_count: 3,
Expand Down Expand Up @@ -315,6 +321,8 @@ describe('Transaction metrics', () => {
properties: {
...expectedProperties,
ui_customizations: ['flagged_as_malicious'],
security_alert_reason: BlockaidReason.maliciousDomain,
security_alert_response: 'Malicious',
ppom_eth_call_count: 5,
ppom_eth_getCode_count: 3,
},
Expand All @@ -330,6 +338,8 @@ describe('Transaction metrics', () => {
properties: {
...expectedProperties,
ui_customizations: ['flagged_as_malicious'],
security_alert_reason: BlockaidReason.maliciousDomain,
security_alert_response: 'Malicious',
ppom_eth_call_count: 5,
ppom_eth_getCode_count: 3,
},
Expand Down Expand Up @@ -442,6 +452,8 @@ describe('Transaction metrics', () => {
properties: {
...expectedProperties,
ui_customizations: ['flagged_as_malicious'],
security_alert_reason: BlockaidReason.maliciousDomain,
security_alert_response: 'Malicious',
ppom_eth_call_count: 5,
ppom_eth_getCode_count: 3,
},
Expand All @@ -460,6 +472,8 @@ describe('Transaction metrics', () => {
properties: {
...expectedProperties,
ui_customizations: ['flagged_as_malicious'],
security_alert_reason: BlockaidReason.maliciousDomain,
security_alert_response: 'Malicious',
ppom_eth_call_count: 5,
ppom_eth_getCode_count: 3,
},
Expand Down Expand Up @@ -627,6 +641,8 @@ describe('Transaction metrics', () => {
properties: {
...expectedProperties,
ui_customizations: ['flagged_as_malicious'],
security_alert_reason: BlockaidReason.maliciousDomain,
security_alert_response: 'Malicious',
ppom_eth_call_count: 5,
ppom_eth_getCode_count: 3,
},
Expand All @@ -647,6 +663,8 @@ describe('Transaction metrics', () => {
properties: {
...expectedProperties,
ui_customizations: ['flagged_as_malicious'],
security_alert_reason: BlockaidReason.maliciousDomain,
security_alert_response: 'Malicious',
ppom_eth_call_count: 5,
ppom_eth_getCode_count: 3,
},
Expand Down Expand Up @@ -750,6 +768,8 @@ describe('Transaction metrics', () => {
properties: {
...expectedProperties,
ui_customizations: ['flagged_as_malicious'],
security_alert_reason: BlockaidReason.maliciousDomain,
security_alert_response: 'Malicious',
ppom_eth_call_count: 5,
ppom_eth_getCode_count: 3,
},
Expand All @@ -769,6 +789,8 @@ describe('Transaction metrics', () => {
properties: {
...expectedProperties,
ui_customizations: ['flagged_as_malicious'],
security_alert_reason: BlockaidReason.maliciousDomain,
security_alert_response: 'Malicious',
ppom_eth_call_count: 5,
ppom_eth_getCode_count: 3,
},
Expand Down Expand Up @@ -867,6 +889,8 @@ describe('Transaction metrics', () => {
properties: {
...expectedProperties,
ui_customizations: ['flagged_as_malicious'],
security_alert_reason: BlockaidReason.maliciousDomain,
security_alert_response: 'Malicious',
ppom_eth_call_count: 5,
ppom_eth_getCode_count: 3,
},
Expand All @@ -882,6 +906,8 @@ describe('Transaction metrics', () => {
properties: {
...expectedProperties,
ui_customizations: ['flagged_as_malicious'],
security_alert_reason: BlockaidReason.maliciousDomain,
security_alert_response: 'Malicious',
ppom_eth_call_count: 5,
ppom_eth_getCode_count: 3,
},
Expand Down
40 changes: 15 additions & 25 deletions app/scripts/lib/transaction/metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import {
BlockaidReason,
BlockaidResultType,
} from '../../../../shared/constants/security-provider';
import { getBlockaidMetricsParams } from '../../../../ui/helpers/utils/metrics';
///: END:ONLY_INCLUDE_IN
import {
getSnapAndHardwareInfoForMetrics,
Expand Down Expand Up @@ -926,36 +927,25 @@ async function buildEventFragmentProperties({
}

let uiCustomizations;
let additionalBlockaidParams;

// eslint-disable-next-line no-lonely-if
if (securityProviderResponse?.flagAsDangerous === 1) {
uiCustomizations = ['flagged_as_malicious'];
} else if (securityProviderResponse?.flagAsDangerous === 2) {
uiCustomizations = ['flagged_as_safety_unknown'];
} else {
uiCustomizations = null;
}

///: BEGIN:ONLY_INCLUDE_IN(blockaid)
if (securityAlertResponse?.result_type === BlockaidResultType.Failed) {
uiCustomizations = ['security_alert_failed'];
} else {
///: END:ONLY_INCLUDE_IN
// eslint-disable-next-line no-lonely-if
if (securityProviderResponse?.flagAsDangerous === 1) {
uiCustomizations = ['flagged_as_malicious'];
} else if (securityProviderResponse?.flagAsDangerous === 2) {
uiCustomizations = ['flagged_as_safety_unknown'];
} else {
uiCustomizations = null;
}
///: BEGIN:ONLY_INCLUDE_IN(blockaid)
additionalBlockaidParams = getBlockaidMetricsParams(securityAlertResponse);
uiCustomizations = additionalBlockaidParams?.ui_customizations ?? null;
}
///: END:ONLY_INCLUDE_IN

///: BEGIN:ONLY_INCLUDE_IN(blockaid)
const additionalBlockaidParams = {} as Record<string, any>;

if (securityProviderResponse?.providerRequestsCount) {
Object.keys(securityProviderResponse.providerRequestsCount).forEach(
(key) => {
const metricKey = `ppom_${key}_count`;
additionalBlockaidParams[metricKey] =
securityProviderResponse.providerRequestsCount[key];
},
);
}
///: END:ONLY_INCLUDE_IN

if (simulationFails) {
Expand Down Expand Up @@ -985,13 +975,13 @@ async function buildEventFragmentProperties({
token_standard: tokenStandard,
transaction_type: transactionType,
transaction_speed_up: type === TransactionType.retry,
ui_customizations: uiCustomizations,
...additionalBlockaidParams,
ui_customizations: uiCustomizations?.length > 0 ? uiCustomizations : null,
///: BEGIN:ONLY_INCLUDE_IN(blockaid)
security_alert_response:
securityAlertResponse?.result_type ?? BlockaidResultType.NotApplicable,
security_alert_reason:
securityAlertResponse?.reason ?? BlockaidReason.notApplicable,
...additionalBlockaidParams,
///: END:ONLY_INCLUDE_IN
gas_estimation_failed: Boolean(simulationFails),
} as Record<string, any>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,6 @@ import {
///: END:ONLY_INCLUDE_IN
} from '../../component-library';
///: BEGIN:ONLY_INCLUDE_IN(blockaid)
import {
MetaMetricsEventCategory,
MetaMetricsEventName,
} from '../../../../shared/constants/metametrics';
import { getBlockaidMetricsParams } from '../../../helpers/utils/metrics';
import BlockaidBannerAlert from '../security-provider-banner-alert/blockaid-banner-alert/blockaid-banner-alert';
///: END:ONLY_INCLUDE_IN
import ConfirmPageContainerNavigation from '../confirm-page-container/confirm-page-container-navigation';
Expand Down Expand Up @@ -96,26 +91,6 @@ export default class SignatureRequestOriginal extends Component {
showSignatureRequestWarning: false,
};

///: BEGIN:ONLY_INCLUDE_IN(blockaid)
componentDidMount() {
const { txData } = this.props;
if (txData.securityAlertResponse) {
const blockaidMetricsParams = getBlockaidMetricsParams(
txData.securityAlertResponse,
);

this.context.trackEvent({
category: MetaMetricsEventCategory.Transactions,
event: MetaMetricsEventName.SignatureRequested,
properties: {
action: 'Sign Request',
...blockaidMetricsParams,
},
});
}
}
///: END:ONLY_INCLUDE_IN

msgHexToText = (hex) => {
try {
const stripped = stripHexPrefix(hex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export default function SignatureRequestSIWE({ txData }) {
},
});
}
}, [txData?.securityAlertResponse]);
}, []);
///: END:ONLY_INCLUDE_IN

const {
Expand Down
22 changes: 0 additions & 22 deletions ui/components/app/signature-request/signature-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ import { showCustodyConfirmLink } from '../../../store/institutional/institution
import { useMMICustodySignMessage } from '../../../hooks/useMMICustodySignMessage';
///: END:ONLY_INCLUDE_IN
///: BEGIN:ONLY_INCLUDE_IN(blockaid)
import { getBlockaidMetricsParams } from '../../../helpers/utils/metrics';
import BlockaidBannerAlert from '../security-provider-banner-alert/blockaid-banner-alert/blockaid-banner-alert';
///: END:ONLY_INCLUDE_IN

Expand Down Expand Up @@ -256,27 +255,6 @@ const SignatureRequest = ({ txData }) => {
]);
///: END:ONLY_INCLUDE_IN

///: BEGIN:ONLY_INCLUDE_IN(blockaid)
useEffect(() => {
if (txData.securityAlertResponse) {
const blockaidMetricsParams = getBlockaidMetricsParams(
txData.securityAlertResponse,
);

trackEvent({
category: MetaMetricsEventCategory.Transactions,
event: MetaMetricsEventName.SignatureRequested,
properties: {
action: 'Sign Request',
type,
version,
...blockaidMetricsParams,
},
});
}
}, []);
///: END:ONLY_INCLUDE_IN

return (
<div className="signature-request">
<ConfirmPageContainerNavigation />
Expand Down
21 changes: 0 additions & 21 deletions ui/components/app/transaction-alerts/transaction-alerts.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import React, {
///: BEGIN:ONLY_INCLUDE_IN(blockaid)
useCallback,
useContext,
useEffect,
///: END:ONLY_INCLUDE_IN
} from 'react';
import PropTypes from 'prop-types';
Expand Down Expand Up @@ -33,7 +32,6 @@ import {
MetaMetricsEventCategory,
MetaMetricsEventName,
} from '../../../../shared/constants/metametrics';
import { getBlockaidMetricsParams } from '../../../helpers/utils/metrics';
///: END:ONLY_INCLUDE_IN

const TransactionAlerts = ({
Expand Down Expand Up @@ -74,25 +72,6 @@ const TransactionAlerts = ({
const trackEvent = useContext(MetaMetricsContext);
///: END:ONLY_INCLUDE_IN

///: BEGIN:ONLY_INCLUDE_IN(blockaid)
useEffect(() => {
if (txData.securityAlertResponse) {
const blockaidMetricsParams = getBlockaidMetricsParams(
txData.securityAlertResponse,
);

trackEvent({
category: MetaMetricsEventCategory.Transactions,
event: MetaMetricsEventName.SignatureRequested,
properties: {
action: 'Sign Request',
...blockaidMetricsParams,
},
});
}
}, [txData?.securityAlertResponse]);
///: END:ONLY_INCLUDE_IN

///: BEGIN:ONLY_INCLUDE_IN(blockaid)
const onClickSupportLink = useCallback(() => {
trackEvent({
Expand Down
4 changes: 4 additions & 0 deletions ui/helpers/utils/metrics.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ export const getBlockaidMetricsParams = (securityAlertResponse = null) => {
additionalParams.ui_customizations = ['flagged_as_malicious'];
}

if (resultType === BlockaidResultType.Failed) {
additionalParams.ui_customizations = ['security_alert_failed'];
}

if (resultType !== BlockaidResultType.Benign) {
additionalParams.security_alert_reason = BlockaidReason.notApplicable;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,6 @@ import { ConfirmPageContainerWarning } from '../../../components/app/confirm-pag
import LedgerInstructionField from '../../../components/app/ledger-instruction-field';
///: BEGIN:ONLY_INCLUDE_IN(blockaid)
import BlockaidBannerAlert from '../../../components/app/security-provider-banner-alert/blockaid-banner-alert/blockaid-banner-alert';
import { getBlockaidMetricsParams } from '../../../helpers/utils/metrics';
import {
MetaMetricsEventCategory,
MetaMetricsEventName,
} from '../../../../shared/constants/metametrics';
///: END:ONLY_INCLUDE_IN
import { isSuspiciousResponse } from '../../../../shared/modules/security-provider.utils';

Expand Down Expand Up @@ -103,26 +98,6 @@ export default class ConfirmApproveContent extends Component {
setShowContractDetails: false,
};

///: BEGIN:ONLY_INCLUDE_IN(blockaid)
componentDidMount() {
const { txData } = this.props;
if (txData.securityAlertResponse) {
const blockaidMetricsParams = getBlockaidMetricsParams(
txData.securityAlertResponse,
);

this.context.trackEvent({
category: MetaMetricsEventCategory.Transactions,
event: MetaMetricsEventName.SignatureRequested,
properties: {
action: 'Sign Request',
...blockaidMetricsParams,
},
});
}
}
///: END:ONLY_INCLUDE_IN

renderApproveContentCard({
showHeader = true,
symbol,
Expand Down
Loading

0 comments on commit 6d1de3e

Please sign in to comment.