-
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.
feat: Add metrics for alerts (transactions redesign) (#26121)
<!-- Please submit this PR as a draft initially. Do not mark it as "Ready for review" until the template has been completely filled out, and PR status checks have passed at least once. --> ## **Description** Recently the Alert System was introduced to redesigned confirmations with this the alerts for those confirmations were migrated to use the new Alert System. This PR introduces infrastructure to track metrics for alerts in the redesigned confirmation system. It ensures that alert metrics are captured and included in the necessary places for tracking. The use of a context to manage shared state for alert metrics within the confirmation flow. The context approach was chosen because it allows us to maintain a single instance of the shared state per confirmation. This is crucial for ensuring that clicks on different inline alerts and actions are persisted and aggregated into a unified set of properties. These aggregated properties are then added to the transaction event, providing a comprehensive view of user interactions within the confirmation process. <!-- Write a short description of the changes included in this pull request, also include relevant motivation and context. Have in mind the following questions: 1. What is the reason for the change? 2. What is the improvement/solution? --> [![Open in GitHub Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/MetaMask/metamask-extension/pull/26121?quickstart=1) ## **Related issues** Fixes: MetaMask/MetaMask-planning#2709 ## **Manual testing steps** 1. Go to the test dapp 2. in the session `PPOM - Malicious Transactions and Signatures` > mint ERC20 ## **Screenshots/Recordings** <!-- If applicable, add screenshots and/or recordings to visualize the before and after of your change. --> [confirm.webm](https://github.com/user-attachments/assets/43d88b5a-c10e-472f-8603-83a344ca71ca) [reject.webm](https://github.com/user-attachments/assets/ce13cb90-89f7-43d7-8336-281ab784f4eb) ### **Before** <!-- [screenshots/recordings] --> ### **After** <!-- [screenshots/recordings] --> ## **Pre-merge author checklist** - [x] I've followed [MetaMask Contributor Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask Extension Coding Standards](https://github.com/MetaMask/metamask-extension/blob/develop/.github/guidelines/CODING_GUIDELINES.md). - [x] I've completed the PR template to the best of my ability - [x] I’ve included tests if applicable - [x] I’ve documented my code using [JSDoc](https://jsdoc.app/) format if applicable - [x] I’ve applied the right labels on the PR (see [labeling guidelines](https://github.com/MetaMask/metamask-extension/blob/develop/.github/guidelines/LABELING_GUIDELINES.md)). Not required for external contributors. ## **Pre-merge reviewer checklist** - [ ] I've manually tested the PR (e.g. pull and build branch, run the app, test code being changed). - [ ] I confirm that this PR addresses all acceptance criteria described in the ticket it closes and includes the necessary testing evidence such as recordings and or screenshots. --------- Co-authored-by: Matthew Walsh <[email protected]>
- Loading branch information
1 parent
91dc6ea
commit bc6539b
Showing
26 changed files
with
629 additions
and
36 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
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
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
44 changes: 44 additions & 0 deletions
44
ui/components/app/alert-system/contexts/alertMetricsContext.test.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,44 @@ | ||
import React from 'react'; | ||
import { renderHookWithProvider } from '../../../../../test/lib/render-helpers'; | ||
import { useAlertMetrics } from './alertMetricsContext'; | ||
|
||
jest.mock('react', () => ({ | ||
...jest.requireActual('react'), | ||
useContext: jest.fn(), | ||
})); | ||
|
||
describe('useAlertMetrics', () => { | ||
beforeEach(() => { | ||
jest.resetAllMocks(); | ||
}); | ||
|
||
it('provides trackAlertActionClicked, trackAlertRender, and trackInlineAlertClicked functions from context', () => { | ||
(React.useContext as jest.Mock).mockReturnValue({ | ||
trackAlertActionClicked: jest.fn(), | ||
trackAlertRender: jest.fn(), | ||
trackInlineAlertClicked: jest.fn(), | ||
}); | ||
const ALERT_KEY_MOCK = 'testKey'; | ||
const { result } = renderHookWithProvider(useAlertMetrics); | ||
|
||
expect(result.current).toBeDefined(); | ||
expect(typeof result.current.trackAlertActionClicked).toBe('function'); | ||
expect(typeof result.current.trackAlertRender).toBe('function'); | ||
expect(typeof result.current.trackInlineAlertClicked).toBe('function'); | ||
|
||
expect(() => | ||
result.current.trackAlertActionClicked(ALERT_KEY_MOCK), | ||
).not.toThrow(); | ||
expect(() => result.current.trackAlertRender(ALERT_KEY_MOCK)).not.toThrow(); | ||
expect(() => | ||
result.current.trackInlineAlertClicked(ALERT_KEY_MOCK), | ||
).not.toThrow(); | ||
}); | ||
|
||
it('throws an error if used outside of AlertMetricsProvider', () => { | ||
const { result } = renderHookWithProvider(() => useAlertMetrics()); | ||
expect(result.error).toEqual( | ||
new Error('useAlertMetrics must be used within an AlertMetricsProvider'), | ||
); | ||
}); | ||
}); |
40 changes: 40 additions & 0 deletions
40
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,40 @@ | ||
import React, { createContext, useContext, ReactNode, useMemo } from 'react'; | ||
import { useConfirmationAlertMetrics } from '../../../../pages/confirmations/hooks/useConfirmationAlertMetrics'; | ||
|
||
const AlertMetricsContext = createContext<{ | ||
trackAlertActionClicked: (alertKey: string) => void; | ||
trackAlertRender: (alertKey: string) => void; | ||
trackInlineAlertClicked: (alertKey: string) => void; | ||
} | null>(null); | ||
|
||
export const AlertMetricsProvider: React.FC<{ children: ReactNode }> = ({ | ||
children, | ||
}) => { | ||
const { trackAlertActionClicked, trackAlertRender, trackInlineAlertClicked } = | ||
useConfirmationAlertMetrics(); | ||
|
||
const value = useMemo( | ||
() => ({ | ||
trackAlertActionClicked, | ||
trackAlertRender, | ||
trackInlineAlertClicked, | ||
}), | ||
[trackAlertActionClicked, trackAlertRender, trackInlineAlertClicked], | ||
); | ||
|
||
return ( | ||
<AlertMetricsContext.Provider value={value}> | ||
{children} | ||
</AlertMetricsContext.Provider> | ||
); | ||
}; | ||
|
||
export const useAlertMetrics = () => { | ||
const context = useContext(AlertMetricsContext); | ||
if (!context) { | ||
throw new Error( | ||
'useAlertMetrics must be used within an AlertMetricsProvider', | ||
); | ||
} | ||
return context; | ||
}; |
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
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.