-
Notifications
You must be signed in to change notification settings - Fork 5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
552754a
commit 051fd66
Showing
8 changed files
with
175 additions
and
162 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
ui/components/app/alert-system/contexts/alertMetricsContext.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import React, { createContext, useContext, ReactNode } from 'react'; | ||
import { | ||
ALERTS_NAME_METRICS, | ||
UseAlertSystemMetricsProps, | ||
useConfirmAlertMetrics, | ||
} from '../useConfirmAlertMetrics'; | ||
import { Alert } from '../../../../ducks/confirm-alerts/confirm-alerts'; | ||
|
||
const AlertMetricsContext = createContext<{ | ||
trackAlertMetrics: (props?: UseAlertSystemMetricsProps) => void; | ||
} | null>(null); | ||
|
||
export const AlertMetricsProvider: React.FC<{ children: ReactNode }> = ({ | ||
children, | ||
}) => { | ||
const { trackAlertMetrics } = useConfirmAlertMetrics(); | ||
|
||
return ( | ||
<AlertMetricsContext.Provider value={{ trackAlertMetrics }}> | ||
{children} | ||
</AlertMetricsContext.Provider> | ||
); | ||
}; | ||
|
||
export const useAlertMetrics = () => { | ||
const context = useContext(AlertMetricsContext); | ||
if (!context) { | ||
throw new Error( | ||
'useAlertMetrics must be used within an AlertMetricsProvider', | ||
); | ||
} | ||
return context; | ||
}; |
143 changes: 0 additions & 143 deletions
143
ui/components/app/alert-system/useAlertSystemMetrics.ts
This file was deleted.
Oops, something went wrong.
123 changes: 123 additions & 0 deletions
123
ui/components/app/alert-system/useConfirmAlertMetrics.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
import { TransactionType } from '@metamask/transaction-controller'; | ||
import { useCallback, useEffect, useState } from 'react'; | ||
import { useSelector } from 'react-redux'; | ||
import useAlerts from '../../../hooks/useAlerts'; | ||
import { useTransactionEventFragment } from '../../../pages/confirmations/hooks/useTransactionEventFragment'; | ||
import { REDESIGN_TRANSACTION_TYPES } from '../../../pages/confirmations/utils'; | ||
import { Alert } from '../../../ducks/confirm-alerts/confirm-alerts'; | ||
import { AlertsName } from '../../../pages/confirmations/hooks/alerts/constants'; | ||
import { confirmSelector } from '../../../selectors'; | ||
|
||
export type UseAlertSystemMetricsProps = { | ||
alertKey?: string; | ||
action?: AlertsActionMetrics; | ||
}; | ||
|
||
export type AlertMetricsProperties = { | ||
alert_visualized: string[]; | ||
alert_visualized_count: number; | ||
alert_key_clicked: string[]; | ||
alert_action_clicked: string[]; | ||
}; | ||
|
||
export const ALERTS_NAME_METRICS: Record<AlertsName | string, string> = { | ||
[AlertsName.GasEstimateFailed]: 'gas_estimate_failed', | ||
[AlertsName.GasFeeLow]: 'gas_fee_low', | ||
[AlertsName.GasTooLow]: 'gas_too_low', | ||
[AlertsName.InsufficientBalance]: 'insufficient_balance', | ||
[AlertsName.NetworkBusy]: 'network_busy', | ||
[AlertsName.NoGasPrice]: 'no_gas_price', | ||
[AlertsName.PendingTransaction]: 'pending_transaction', | ||
[AlertsName.SigningOrSubmitting]: 'signing_or_submitting', | ||
Blockaid: 'blockaid', | ||
}; | ||
|
||
export enum AlertsActionMetrics { | ||
AlertVisualized = 'AlertVisualized', | ||
InlineAlertClicked = 'InlineAlertClicked', | ||
AlertActionClicked = 'AlertActionClicked', | ||
} | ||
|
||
export function useConfirmAlertMetrics() { | ||
const { currentConfirmation } = useSelector(confirmSelector); | ||
const ownerId = currentConfirmation?.id ?? ''; | ||
const { alerts, isAlertConfirmed } = useAlerts(ownerId); | ||
const { updateTransactionEventFragment } = useTransactionEventFragment(); | ||
|
||
const [metricsProperties, setMetricsProperties] = | ||
useState<AlertMetricsProperties>({ | ||
alert_visualized: [], | ||
alert_visualized_count: 0, | ||
alert_key_clicked: [], | ||
alert_action_clicked: [], | ||
}); | ||
|
||
const isValidType = REDESIGN_TRANSACTION_TYPES.includes( | ||
currentConfirmation?.type as TransactionType, | ||
); | ||
|
||
const trackAlertMetrics = useCallback( | ||
(props?: UseAlertSystemMetricsProps) => { | ||
const { alertKey, action } = props ?? {}; | ||
if (!isValidType || !alertKey || !action) { | ||
return; | ||
} | ||
|
||
setMetricsProperties((prevState) => { | ||
const newState = { ...prevState }; | ||
const alertName = ALERTS_NAME_METRICS[alertKey] ?? alertKey; | ||
|
||
switch (action) { | ||
case AlertsActionMetrics.AlertVisualized: | ||
newState.alert_visualized = [ | ||
...new Set([...prevState.alert_visualized, alertName]), | ||
]; | ||
newState.alert_visualized_count = newState.alert_visualized.length; | ||
break; | ||
case AlertsActionMetrics.InlineAlertClicked: | ||
newState.alert_key_clicked = [ | ||
...new Set([...prevState.alert_key_clicked, alertName]), | ||
]; | ||
break; | ||
case AlertsActionMetrics.AlertActionClicked: | ||
newState.alert_action_clicked = [ | ||
...new Set([...prevState.alert_action_clicked, alertName]), | ||
]; | ||
break; | ||
default: | ||
} | ||
return newState; | ||
}); | ||
}, | ||
[isValidType], | ||
); | ||
|
||
useEffect(() => { | ||
if (isValidType && alerts.length > 0) { | ||
const properties = { | ||
alert_triggered_count: alerts.length, | ||
alert_triggered: getAlertsName(alerts), | ||
alert_resolved_count: alerts.filter((alert) => | ||
isAlertConfirmed(alert.key), | ||
).length, | ||
alert_resolved: getAlertsName( | ||
alerts.filter((alert) => isAlertConfirmed(alert.key)), | ||
), | ||
...metricsProperties, | ||
}; | ||
updateTransactionEventFragment({ properties }, ownerId); | ||
} | ||
}, [ | ||
metricsProperties, | ||
alerts, | ||
isValidType, | ||
ownerId, | ||
updateTransactionEventFragment, | ||
]); | ||
|
||
return { trackAlertMetrics }; | ||
} | ||
|
||
function getAlertsName(alerts: Alert[]) { | ||
return alerts.map((alert) => ALERTS_NAME_METRICS[alert.key]); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.