-
-
Notifications
You must be signed in to change notification settings - Fork 239
feat(identity): create metametrics event library for profile-sync-controller #6044
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
@metamaskbot publish-preview |
Preview builds have been published. See these instructions for more information about preview builds. Expand for full list of packages and versions.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While I agree with the intent, and the logic of everything is sound, I don't think it translates well into following the current segment schema definition, and we can probably deliver a better DX from this.
Some ideas, let me know what you think!
BackupAndSyncFeatureNames
makes little sense to me. This is not a dictionary that needs to exist in my mind. Also, you're listing things that don't exist yet in the segment schema definition. But I see the intent, so I think we should probably go in a direction like this instead:
export const BackupAndSyncEvents = {
ACCOUNT_SYNCING: {
ACCOUNTS_SYNC_ADDED: 'Accounts Sync Added',
...
},
...
}
But more in 3.
-
createBackupAndSyncEventProperties
is too specific, as most currently used events for example don't have afeature_name
andaction
field. Example event definition here (only hasprofile_id
as property) -
Considering the difference in how MetaMetrics events are handled in both clients (extension example, mobile example), we should probably change our approach. In the end, we only need the event name and the corresponding properties.
category
is extension specific and doesn't matter in the end.
Below is only a suggestion!! Feel free to explore more on your own 😄
This gives type safety and intellisense directly in the properties
argument, so that you're sure you're building the properties correctly + that you have the correct event name.
This is a bit convoluted maybe though lol. But in the end, you only need to import MetaMetrics.IDENTITY_EVENTS
and MetaMetrics.buildIdentityEvent
LMK what you think!
export interface IdentityEvent {
name: string;
properties: {
[key: string]: {
required: boolean;
type: 'string' | 'number' | 'boolean';
fromObject?: Record<string, string>;
};
};
}
type PropertyType<T, FromObject> = FromObject extends Record<string, string>
? FromObject[keyof FromObject]
: T extends 'string'
? string
: T extends 'number'
? number
: T extends 'boolean'
? boolean
: never;
type EventProperties<T extends IdentityEvent> = {
[K in keyof T['properties'] as T['properties'][K]['required'] extends true
? K
: never]: PropertyType<
T['properties'][K]['type'],
T['properties'][K]['fromObject']
>;
} & {
[K in keyof T['properties'] as T['properties'][K]['required'] extends false
? K
: never]?: PropertyType<
T['properties'][K]['type'],
T['properties'][K]['fromObject']
>;
};
export const IDENTITY_EVENTS = {
ACCOUNT_SYNCING: {
ACCOUNT_ADDED: {
name: 'Accounts Sync Account Added',
properties: {
profile_id: {
required: true,
type: 'string',
},
},
},
},
PROFILE: {
ACTIVITY_UPDATED: {
name: 'Profile Activity Updated',
properties: {
profile_id: {
required: false,
type: 'string',
},
feature_name: {
required: true,
type: 'string',
fromObject: {
BACKUP_AND_SYNC: 'Backup and Sync',
AUTHENTICATION: 'Authentication',
},
},
action: {
required: true,
type: 'string',
fromObject: {
SETTINGS_TOGGLE_ENABLED: 'settings_toggle_enabled',
SETTINGS_TOGGLE_DISABLED: 'settings_toggle_disabled',
},
},
additional_description: {
required: false,
type: 'string',
},
},
},
},
} as const satisfies Record<string, Record<string, IdentityEvent>>;
export const buildIdentityEvent = <T extends IdentityEvent>(
event: T,
properties: EventProperties<T>,
): { name: (typeof event)['name']; properties: typeof properties } => ({
name: event.name,
properties,
});
const accountSyncingAccountAddedEvent = buildIdentityEvent(
IDENTITY_EVENTS.ACCOUNT_SYNCING.ACCOUNT_ADDED,
{
profile_id: 'profile_123',
},
);
console.log(accountSyncingAccountAddedEvent.name); // Outputs: "Accounts Sync Account Added"
console.log(accountSyncingAccountAddedEvent.properties); // Outputs: { profile_id: 'profile_123' }
const profileActivityUpdatedEvent = buildIdentityEvent(
IDENTITY_EVENTS.PROFILE.ACTIVITY_UPDATED,
{
feature_name:
IDENTITY_EVENTS.PROFILE.ACTIVITY_UPDATED.properties.feature_name
.fromObject.BACKUP_AND_SYNC,
action:
IDENTITY_EVENTS.PROFILE.ACTIVITY_UPDATED.properties.action.fromObject
.SETTINGS_TOGGLE_ENABLED,
},
);
console.log(profileActivityUpdatedEvent.name); // Outputs: "Profile Activity Updated"
console.log(profileActivityUpdatedEvent.properties); // Outputs: { feature_name: 'Backup and Sync', action: 'settings_toggle_enabled' }
@mathieuartu About point #2: I considered a future in which we (=identity) only rely on: https://github.com/Consensys/segment-schema/blob/main/libraries/events/metamask-identity/identity-event.yaml |
@mathieuartu despite metamask-mobile likely blocking this effort, I addressed all of your remarks. Please check it out 😉 |
@metamaskbot publish-preview |
Preview builds have been published. See these instructions for more information about preview builds. Expand for full list of packages and versions.
|
Explanation
This PR addresses the issue of magic strings being used inconsistently across MetaMask clients when tracking MetaMetrics events related to profile-sync-controller functionality. Currently, both extension and mobile clients use hardcoded string literals for feature names and actions, leading to inconsistencies (e.g., extension uses 'Backup And Sync' while mobile uses 'Contacts Sync' for the same feature).
The solution creates a centralized MetaMetrics event library within the profile-sync-controller package that exports standardized constants and helper functions. This library provides:
BackupAndSyncFeatureNames
- Constants for all feature names used in profile-sync eventsBackupAndSyncActions
- Constants for all action names used in profile-sync eventsBackupAndSyncEventProperties
- Pre-built property sets for common eventscreateBackupAndSyncEventProperties()
- Helper function for creating custom event propertiesClients can now import these type-safe constants instead of maintaining their own string definitions.
References
This PR is part of the effort to standardize MetaMetrics event tracking across MetaMask clients and improve maintainability of analytics code.
Changelog
Checklist