Skip to content

Commit 51f981d

Browse files
committed
feat(4243): add doc for create an analystics event from extension
1 parent 24a4a27 commit 51f981d

File tree

2 files changed

+143
-0
lines changed

2 files changed

+143
-0
lines changed

docs/create-analystics-event.md

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
# Create Analytics Event
2+
3+
## Overview
4+
5+
This document outlines the process for creating a new analytics event in MetaMask (Extension and Mobile)
6+
7+
## Prerequisites
8+
9+
- Before you begin, please refer to this [Working with Event Metrics](https://www.notion.so/consensys/Working-with-Event-Metrics-3b0b5a4308e64649a54a2864886c0412) and make sure you understand the context of the analytics events.
10+
- Users must opt into MetaMetrics for events to be tracked, this can be configured when onboarding and in settings `Privacy & Security` => `Participate in MetaMetrics`
11+
12+
## Creating a New Analytics Event
13+
14+
### 1. Define the Event Schema
15+
16+
First, create a schema for your event following the [Segment Schema guidelines](https://github.com/Consensys/segment-schema/blob/main/CONTRIBUTING.md). Your event should:
17+
18+
- Have a clear, descriptive name (e.g., `wallet_connected`)
19+
- Include relevant properties and context
20+
- Follow the naming conventions
21+
22+
### 2. Implement a New Event in the Extension
23+
24+
#### 2.1. Add the Event Name to the Segment Schema
25+
26+
Add your new event name as a string literal type to the `MetaMetricsEventName` enum in `shared/constants/metametrics.ts`.
27+
And please check `MetaMetricsEventPayload` inside `shared/constants/metametrics.ts`for the details of the parameters.
28+
29+
#### 2.2. Implement the Event in the Extension
30+
31+
To implement an analytics event in the extension, you'll need to use the `MetaMetricsContext` and its `trackEvent` function. This context provides a standardized way to track events while handling opt-in status and data anonymization automatically. Below is an example showing how to implement event tracking in a React component:
32+
33+
```typescript
34+
// 1. Import the required types and context
35+
import { MetaMetricsContext } from '../../../shared/contexts/metametrics';
36+
import {
37+
MetaMetricsEventName,
38+
MetaMetricsEventCategory,
39+
MetaMetricsEventFragment,
40+
} from '../../../shared/constants/metametrics';
41+
42+
// 2. Use the context in your component
43+
function YourComponent() {
44+
const { trackEvent } = useContext(MetaMetricsContext);
45+
46+
const handleAction = () => {
47+
trackEvent({
48+
event: MetaMetricsEventName.YourNewEventName,
49+
category: MetaMetricsEventCategory.YourCategory,
50+
properties: {
51+
// Add properties that provide context about the event
52+
property_one: 'value_one',
53+
property_two: 'value_two',
54+
},
55+
sensitiveProperties: {
56+
// Add properties that might contain sensitive data
57+
// These will be automatically anonymized
58+
wallet_address: '0x...',
59+
},
60+
});
61+
};
62+
63+
return <button onClick={handleAction}>Perform Action</button>;
64+
}
65+
```
66+
67+
#### 2.3 E2E Testing
68+
69+
For implementing analytics event mocks in E2E tests using `testSpecificMock`, please refer to recording in notion of [Extension Codebase - E2E tests (mocking)](https://www.notion.so/metamask-consensys/f649ecf027cc41db858d3a3574fe3a99?v=da4af5e48f0e44d7b4536c4e2e911f36).
70+
71+
Example E2E test implementation:
72+
73+
```typescript
74+
// Mock the analytics endpoint
75+
async function mockAnalyticsEndpoint(mockServer) {
76+
return await mockServer
77+
.forPost('https://api.segment.io/v1/batch')
78+
.withJsonBodyIncluding({
79+
batch: [
80+
{
81+
type: 'track',
82+
event: MetaMetricsEventName.YourNewEventName,
83+
properties: {
84+
property_one: 'value_one',
85+
property_two: 'value_two',
86+
},
87+
},
88+
],
89+
})
90+
.thenCallback(() => ({ statusCode: 200 }));
91+
}
92+
93+
describe('YourNewEventName Analytics', () => {
94+
it('should track the event with correct properties', async () => {
95+
await withFixtures(
96+
{
97+
fixtures: new FixtureBuilder()
98+
.withMetaMetricsController({
99+
participateInMetaMetrics: true,
100+
})
101+
.build(),
102+
testSpecificMock: async (mockServer) => [
103+
await mockAnalyticsEndpoint(mockServer),
104+
],
105+
},
106+
async ({ driver, mockedEndpoints }) => {
107+
// Test implementation
108+
const events = await getEventPayloads(driver, mockedEndpoints);
109+
assert.equal(events[0].properties.property_one, 'value_one');
110+
assert.equal(events[0].properties.property_two, 'value_two');
111+
},
112+
);
113+
});
114+
});
115+
```
116+
117+
#### 2.4. Validate the Event
118+
119+
To validate your analytics event implementation:
120+
121+
1. Run the extension locally in development mode
122+
2. Open Chrome DevTools
123+
3. Navigate to the Network tab
124+
4. Perform the action that should trigger your analytics event
125+
5. Look for a `batch` request in the Network tab
126+
6. Inspect the request payload to verify:
127+
- The event name matches your implementation
128+
- All expected properties are present and correctly formatted
129+
- Property values are being captured as intended
130+
131+
![Analytics Event Request Process](./images/create-analystics-event-request.png)
132+
133+
#### 2.5. Opt-out Handling
134+
135+
The tracking system automatically respects user privacy settings:
136+
137+
- Events are only tracked if the user has opted into MetaMetrics
138+
- Sensitive data is automatically anonymized
139+
- You don't need to check opt-in status before calling trackEvent
140+
141+
### 3. Implement the Event in the Mobile App
142+
143+
TODO: Add instructions for the mobile app
Loading

0 commit comments

Comments
 (0)