2022-04-21 23:46:12 +02:00
|
|
|
import {
|
|
|
|
isEqual,
|
|
|
|
memoize,
|
|
|
|
merge,
|
|
|
|
omit,
|
|
|
|
omitBy,
|
|
|
|
pickBy,
|
|
|
|
size,
|
|
|
|
sum,
|
|
|
|
} from 'lodash';
|
2021-02-04 19:15:23 +01:00
|
|
|
import { ObservableStore } from '@metamask/obs-store';
|
2021-04-16 17:05:13 +02:00
|
|
|
import { bufferToHex, keccak } from 'ethereumjs-util';
|
2022-09-06 16:13:04 +02:00
|
|
|
import { v4 as uuidv4 } from 'uuid';
|
2021-02-04 19:15:23 +01:00
|
|
|
import { ENVIRONMENT_TYPE_BACKGROUND } from '../../../shared/constants/app';
|
2020-12-02 22:41:30 +01:00
|
|
|
import {
|
|
|
|
METAMETRICS_ANONYMOUS_ID,
|
|
|
|
METAMETRICS_BACKGROUND_PAGE_OBJECT,
|
2023-04-03 17:31:04 +02:00
|
|
|
MetaMetricsUserTrait,
|
2021-02-04 19:15:23 +01:00
|
|
|
} from '../../../shared/constants/metametrics';
|
2022-01-12 20:31:54 +01:00
|
|
|
import { SECOND } from '../../../shared/constants/time';
|
2022-10-04 19:03:50 +02:00
|
|
|
import { isManifestV3 } from '../../../shared/modules/mv3.utils';
|
|
|
|
import { METAMETRICS_FINALIZE_EVENT_FRAGMENT_ALARM } from '../../../shared/constants/alarms';
|
2022-11-08 00:03:03 +01:00
|
|
|
import { checkAlarmExists, generateRandomId, isValidDate } from '../lib/util';
|
2020-12-02 22:41:30 +01:00
|
|
|
|
2022-08-11 19:33:33 +02:00
|
|
|
const EXTENSION_UNINSTALL_URL = 'https://metamask.io/uninstalled';
|
|
|
|
|
2021-11-11 04:27:04 +01:00
|
|
|
const defaultCaptureException = (err) => {
|
|
|
|
// throw error on clean stack so its captured by platform integrations (eg sentry)
|
2022-01-28 16:09:07 +01:00
|
|
|
// but does not interrupt the call stack
|
2021-11-11 04:27:04 +01:00
|
|
|
setTimeout(() => {
|
|
|
|
throw err;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2022-11-08 19:08:08 +01:00
|
|
|
// The function is used to build a unique messageId for segment messages
|
|
|
|
// It uses actionId and uniqueIdentifier from event if present
|
|
|
|
const buildUniqueMessageId = (args) => {
|
2023-01-05 15:49:55 +01:00
|
|
|
const messageIdParts = [];
|
2022-11-08 19:08:08 +01:00
|
|
|
if (args.uniqueIdentifier) {
|
2023-01-05 15:49:55 +01:00
|
|
|
messageIdParts.push(args.uniqueIdentifier);
|
2022-11-08 19:08:08 +01:00
|
|
|
}
|
|
|
|
if (args.actionId) {
|
2023-01-05 15:49:55 +01:00
|
|
|
messageIdParts.push(args.actionId);
|
2022-11-08 19:08:08 +01:00
|
|
|
}
|
2023-01-05 15:49:55 +01:00
|
|
|
if (messageIdParts.length && args.isDuplicateAnonymizedEvent) {
|
|
|
|
messageIdParts.push('0x000');
|
|
|
|
}
|
|
|
|
if (messageIdParts.length) {
|
|
|
|
return messageIdParts.join('-');
|
2022-11-08 19:08:08 +01:00
|
|
|
}
|
|
|
|
return generateRandomId();
|
|
|
|
};
|
|
|
|
|
2021-11-19 18:37:50 +01:00
|
|
|
const exceptionsToFilter = {
|
|
|
|
[`You must pass either an "anonymousId" or a "userId".`]: true,
|
|
|
|
};
|
|
|
|
|
2020-12-02 22:41:30 +01:00
|
|
|
/**
|
|
|
|
* @typedef {import('../../../shared/constants/metametrics').MetaMetricsContext} MetaMetricsContext
|
|
|
|
* @typedef {import('../../../shared/constants/metametrics').MetaMetricsEventPayload} MetaMetricsEventPayload
|
|
|
|
* @typedef {import('../../../shared/constants/metametrics').MetaMetricsEventOptions} MetaMetricsEventOptions
|
|
|
|
* @typedef {import('../../../shared/constants/metametrics').SegmentEventPayload} SegmentEventPayload
|
|
|
|
* @typedef {import('../../../shared/constants/metametrics').SegmentInterface} SegmentInterface
|
|
|
|
* @typedef {import('../../../shared/constants/metametrics').MetaMetricsPagePayload} MetaMetricsPagePayload
|
|
|
|
* @typedef {import('../../../shared/constants/metametrics').MetaMetricsPageOptions} MetaMetricsPageOptions
|
2022-01-12 20:31:54 +01:00
|
|
|
* @typedef {import('../../../shared/constants/metametrics').MetaMetricsEventFragment} MetaMetricsEventFragment
|
2022-04-13 18:46:49 +02:00
|
|
|
* @typedef {import('../../../shared/constants/metametrics').MetaMetricsTraits} MetaMetricsTraits
|
2020-12-02 22:41:30 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2022-07-27 15:28:05 +02:00
|
|
|
* @typedef {object} MetaMetricsControllerState
|
2022-01-12 20:31:54 +01:00
|
|
|
* @property {string} [metaMetricsId] - The user's metaMetricsId that will be
|
2020-12-02 22:41:30 +01:00
|
|
|
* attached to all non-anonymized event payloads
|
2022-01-12 20:31:54 +01:00
|
|
|
* @property {boolean} [participateInMetaMetrics] - The user's preference for
|
2020-12-02 22:41:30 +01:00
|
|
|
* participating in the MetaMetrics analytics program. This setting controls
|
|
|
|
* whether or not events are tracked
|
2022-01-12 20:31:54 +01:00
|
|
|
* @property {{[string]: MetaMetricsEventFragment}} [fragments] - Object keyed
|
|
|
|
* by UUID with stored fragments as values.
|
2022-08-11 19:33:33 +02:00
|
|
|
* @property {Array} [eventsBeforeMetricsOptIn] - Array of queued events added before
|
|
|
|
* a user opts into metrics.
|
|
|
|
* @property {object} [traits] - Traits that are not derived from other state keys.
|
2022-11-16 15:17:55 +01:00
|
|
|
* @property {Record<string any>} [previousUserTraits] - The user traits the last
|
|
|
|
* time they were computed.
|
2020-12-02 22:41:30 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
export default class MetaMetricsController {
|
|
|
|
/**
|
2022-01-07 16:57:33 +01:00
|
|
|
* @param {object} options
|
2022-10-11 17:04:32 +02:00
|
|
|
* @param {object} options.segment - an instance of analytics for tracking
|
2020-12-02 22:41:30 +01:00
|
|
|
* events that conform to the new MetaMetrics tracking plan.
|
2022-07-27 15:28:05 +02:00
|
|
|
* @param {object} options.preferencesStore - The preferences controller store, used
|
2020-12-02 22:41:30 +01:00
|
|
|
* to access and subscribe to preferences that will be attached to events
|
2022-01-07 16:57:33 +01:00
|
|
|
* @param {Function} options.onNetworkDidChange - Used to attach a listener to the
|
2020-12-02 22:41:30 +01:00
|
|
|
* networkDidChange event emitted by the networkController
|
2022-01-07 16:57:33 +01:00
|
|
|
* @param {Function} options.getCurrentChainId - Gets the current chain id from the
|
2020-12-02 22:41:30 +01:00
|
|
|
* network controller
|
2022-01-07 16:57:33 +01:00
|
|
|
* @param {string} options.version - The version of the extension
|
|
|
|
* @param {string} options.environment - The environment the extension is running in
|
2022-08-11 19:33:33 +02:00
|
|
|
* @param {string} options.extension - webextension-polyfill
|
2022-01-07 16:57:33 +01:00
|
|
|
* @param {MetaMetricsControllerState} options.initState - State to initialized with
|
|
|
|
* @param options.captureException
|
2020-12-02 22:41:30 +01:00
|
|
|
*/
|
|
|
|
constructor({
|
|
|
|
segment,
|
|
|
|
preferencesStore,
|
|
|
|
onNetworkDidChange,
|
|
|
|
getCurrentChainId,
|
|
|
|
version,
|
|
|
|
environment,
|
|
|
|
initState,
|
2022-08-11 19:33:33 +02:00
|
|
|
extension,
|
2021-11-11 04:27:04 +01:00
|
|
|
captureException = defaultCaptureException,
|
2020-12-02 22:41:30 +01:00
|
|
|
}) {
|
2021-11-19 18:37:50 +01:00
|
|
|
this._captureException = (err) => {
|
|
|
|
// This is a temporary measure. Currently there are errors flooding sentry due to a problem in how we are tracking anonymousId
|
|
|
|
// We intend on removing this as soon as we understand how to correctly solve that problem.
|
|
|
|
if (!exceptionsToFilter[err.message]) {
|
|
|
|
captureException(err);
|
|
|
|
}
|
|
|
|
};
|
2021-02-04 19:15:23 +01:00
|
|
|
const prefState = preferencesStore.getState();
|
|
|
|
this.chainId = getCurrentChainId();
|
|
|
|
this.locale = prefState.currentLocale.replace('_', '-');
|
2020-12-02 22:41:30 +01:00
|
|
|
this.version =
|
2021-02-04 19:15:23 +01:00
|
|
|
environment === 'production' ? version : `${version}-${environment}`;
|
2022-08-11 19:33:33 +02:00
|
|
|
this.extension = extension;
|
|
|
|
this.environment = environment;
|
2020-12-02 22:41:30 +01:00
|
|
|
|
2022-01-12 20:31:54 +01:00
|
|
|
const abandonedFragments = omitBy(initState?.fragments, 'persist');
|
2022-11-08 00:03:03 +01:00
|
|
|
const segmentApiCalls = initState?.segmentApiCalls || {};
|
2022-01-12 20:31:54 +01:00
|
|
|
|
2020-12-02 22:41:30 +01:00
|
|
|
this.store = new ObservableStore({
|
|
|
|
participateInMetaMetrics: null,
|
|
|
|
metaMetricsId: null,
|
2022-08-11 19:33:33 +02:00
|
|
|
eventsBeforeMetricsOptIn: [],
|
|
|
|
traits: {},
|
2020-12-02 22:41:30 +01:00
|
|
|
...initState,
|
2022-01-12 20:31:54 +01:00
|
|
|
fragments: {
|
|
|
|
...initState?.fragments,
|
|
|
|
},
|
2022-11-08 00:03:03 +01:00
|
|
|
segmentApiCalls: {
|
|
|
|
...segmentApiCalls,
|
|
|
|
},
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2020-12-02 22:41:30 +01:00
|
|
|
|
|
|
|
preferencesStore.subscribe(({ currentLocale }) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
this.locale = currentLocale.replace('_', '-');
|
|
|
|
});
|
2020-12-02 22:41:30 +01:00
|
|
|
|
|
|
|
onNetworkDidChange(() => {
|
2021-02-04 19:15:23 +01:00
|
|
|
this.chainId = getCurrentChainId();
|
|
|
|
});
|
|
|
|
this.segment = segment;
|
2022-01-12 20:31:54 +01:00
|
|
|
|
|
|
|
// Track abandoned fragments that weren't properly cleaned up.
|
|
|
|
// Abandoned fragments are those that were stored in persistent memory
|
|
|
|
// and are available at controller instance creation, but do not have the
|
|
|
|
// 'persist' flag set. This means anytime the extension is unlocked, any
|
|
|
|
// fragments that are not marked as persistent will be purged and the
|
|
|
|
// failure event will be emitted.
|
|
|
|
Object.values(abandonedFragments).forEach((fragment) => {
|
|
|
|
this.finalizeEventFragment(fragment.id, { abandoned: true });
|
|
|
|
});
|
|
|
|
|
2022-11-08 00:03:03 +01:00
|
|
|
// Code below submits any pending segmentApiCalls to Segment if/when the controller is re-instantiated
|
|
|
|
if (isManifestV3) {
|
2022-11-23 19:00:05 +01:00
|
|
|
Object.values(segmentApiCalls).forEach(({ eventType, payload }) => {
|
|
|
|
this._submitSegmentAPICall(eventType, payload);
|
|
|
|
});
|
2022-11-08 00:03:03 +01:00
|
|
|
}
|
|
|
|
|
2022-01-12 20:31:54 +01:00
|
|
|
// Close out event fragments that were created but not progressed. An
|
|
|
|
// interval is used to routinely check if a fragment has not been updated
|
|
|
|
// within the fragment's timeout window. When creating a new event fragment
|
|
|
|
// a timeout can be specified that will cause an abandoned event to be
|
|
|
|
// tracked if the event isn't progressed within that amount of time.
|
2022-10-04 19:03:50 +02:00
|
|
|
if (isManifestV3) {
|
|
|
|
/* eslint-disable no-undef */
|
|
|
|
chrome.alarms.getAll((alarms) => {
|
|
|
|
const hasAlarm = checkAlarmExists(
|
|
|
|
alarms,
|
|
|
|
METAMETRICS_FINALIZE_EVENT_FRAGMENT_ALARM,
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!hasAlarm) {
|
|
|
|
chrome.alarms.create(METAMETRICS_FINALIZE_EVENT_FRAGMENT_ALARM, {
|
|
|
|
delayInMinutes: 1,
|
|
|
|
periodInMinutes: 1,
|
|
|
|
});
|
2022-01-12 20:31:54 +01:00
|
|
|
}
|
|
|
|
});
|
2022-11-04 16:23:56 +01:00
|
|
|
chrome.alarms.onAlarm.addListener((alarmInfo) => {
|
|
|
|
if (alarmInfo.name === METAMETRICS_FINALIZE_EVENT_FRAGMENT_ALARM) {
|
|
|
|
this.finalizeAbandonedFragments();
|
|
|
|
}
|
2022-10-04 19:03:50 +02:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
setInterval(() => {
|
|
|
|
this.finalizeAbandonedFragments();
|
|
|
|
}, SECOND * 30);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
finalizeAbandonedFragments() {
|
|
|
|
Object.values(this.store.getState().fragments).forEach((fragment) => {
|
|
|
|
if (
|
|
|
|
fragment.timeout &&
|
|
|
|
Date.now() - fragment.lastUpdated / 1000 > fragment.timeout
|
|
|
|
) {
|
|
|
|
this.finalizeEventFragment(fragment.id, { abandoned: true });
|
|
|
|
}
|
|
|
|
});
|
2020-12-02 22:41:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
generateMetaMetricsId() {
|
|
|
|
return bufferToHex(
|
2021-04-16 17:05:13 +02:00
|
|
|
keccak(
|
|
|
|
Buffer.from(
|
|
|
|
String(Date.now()) +
|
|
|
|
String(Math.round(Math.random() * Number.MAX_SAFE_INTEGER)),
|
|
|
|
),
|
2020-12-02 22:41:30 +01:00
|
|
|
),
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2020-12-02 22:41:30 +01:00
|
|
|
}
|
|
|
|
|
2022-01-12 20:31:54 +01:00
|
|
|
/**
|
|
|
|
* Create an event fragment in state and returns the event fragment object.
|
|
|
|
*
|
2022-01-20 17:26:39 +01:00
|
|
|
* @param {MetaMetricsEventFragment} options - Fragment settings and properties
|
2022-01-12 20:31:54 +01:00
|
|
|
* to initiate the fragment with.
|
2022-01-20 17:26:39 +01:00
|
|
|
* @returns {MetaMetricsEventFragment}
|
2022-01-12 20:31:54 +01:00
|
|
|
*/
|
|
|
|
createEventFragment(options) {
|
|
|
|
if (!options.successEvent || !options.category) {
|
|
|
|
throw new Error(
|
|
|
|
`Must specify success event and category. Success event was: ${
|
|
|
|
options.event
|
|
|
|
}. Category was: ${options.category}. Payload keys were: ${Object.keys(
|
|
|
|
options,
|
|
|
|
)}. ${
|
|
|
|
typeof options.properties === 'object'
|
|
|
|
? `Payload property keys were: ${Object.keys(options.properties)}`
|
|
|
|
: ''
|
|
|
|
}`,
|
|
|
|
);
|
|
|
|
}
|
2022-09-16 19:04:14 +02:00
|
|
|
|
2022-01-12 20:31:54 +01:00
|
|
|
const { fragments } = this.store.getState();
|
|
|
|
|
2022-09-06 16:13:04 +02:00
|
|
|
const id = options.uniqueIdentifier ?? uuidv4();
|
2022-01-12 20:31:54 +01:00
|
|
|
const fragment = {
|
|
|
|
id,
|
|
|
|
...options,
|
|
|
|
lastUpdated: Date.now(),
|
|
|
|
};
|
|
|
|
this.store.updateState({
|
|
|
|
fragments: {
|
|
|
|
...fragments,
|
|
|
|
[id]: fragment,
|
|
|
|
},
|
|
|
|
});
|
2022-01-20 17:26:39 +01:00
|
|
|
|
|
|
|
if (options.initialEvent) {
|
|
|
|
this.trackEvent({
|
|
|
|
event: fragment.initialEvent,
|
|
|
|
category: fragment.category,
|
|
|
|
properties: fragment.properties,
|
|
|
|
sensitiveProperties: fragment.sensitiveProperties,
|
|
|
|
page: fragment.page,
|
|
|
|
referrer: fragment.referrer,
|
|
|
|
revenue: fragment.revenue,
|
|
|
|
value: fragment.value,
|
|
|
|
currency: fragment.currency,
|
|
|
|
environmentType: fragment.environmentType,
|
2022-11-08 19:08:08 +01:00
|
|
|
actionId: options.actionId,
|
|
|
|
uniqueIdentifier: options.uniqueIdentifier,
|
2022-01-20 17:26:39 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return fragment;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the fragment stored in memory with provided id or undefined if it
|
|
|
|
* does not exist.
|
|
|
|
*
|
|
|
|
* @param {string} id - id of fragment to retrieve
|
|
|
|
* @returns {[MetaMetricsEventFragment]}
|
|
|
|
*/
|
|
|
|
getEventFragmentById(id) {
|
|
|
|
const { fragments } = this.store.getState();
|
|
|
|
|
|
|
|
const fragment = fragments[id];
|
|
|
|
|
2022-01-12 20:31:54 +01:00
|
|
|
return fragment;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates an event fragment in state
|
|
|
|
*
|
|
|
|
* @param {string} id - The fragment id to update
|
|
|
|
* @param {MetaMetricsEventFragment} payload - Fragment settings and
|
|
|
|
* properties to initiate the fragment with.
|
|
|
|
*/
|
|
|
|
updateEventFragment(id, payload) {
|
|
|
|
const { fragments } = this.store.getState();
|
|
|
|
|
|
|
|
const fragment = fragments[id];
|
|
|
|
|
|
|
|
if (!fragment) {
|
|
|
|
throw new Error(`Event fragment with id ${id} does not exist.`);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.store.updateState({
|
|
|
|
fragments: {
|
|
|
|
...fragments,
|
|
|
|
[id]: merge(fragments[id], {
|
|
|
|
...payload,
|
|
|
|
lastUpdated: Date.now(),
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Finalizes a fragment, tracking either a success event or failure Event
|
|
|
|
* and then removes the fragment from state.
|
|
|
|
*
|
|
|
|
* @param {string} id - UUID of the event fragment to be closed
|
|
|
|
* @param {object} options
|
|
|
|
* @param {boolean} [options.abandoned] - if true track the failure
|
|
|
|
* event instead of the success event
|
|
|
|
* @param {MetaMetricsContext.page} [options.page] - page the final event
|
|
|
|
* occurred on. This will override whatever is set on the fragment
|
|
|
|
* @param {MetaMetricsContext.referrer} [options.referrer] - Dapp that
|
|
|
|
* originated the fragment. This is for fallback only, the fragment referrer
|
|
|
|
* property will take precedence.
|
|
|
|
*/
|
2022-01-20 17:26:39 +01:00
|
|
|
finalizeEventFragment(id, { abandoned = false, page, referrer } = {}) {
|
2022-01-12 20:31:54 +01:00
|
|
|
const fragment = this.store.getState().fragments[id];
|
|
|
|
if (!fragment) {
|
|
|
|
throw new Error(`Funnel with id ${id} does not exist.`);
|
|
|
|
}
|
|
|
|
|
|
|
|
const eventName = abandoned ? fragment.failureEvent : fragment.successEvent;
|
|
|
|
|
|
|
|
this.trackEvent({
|
|
|
|
event: eventName,
|
|
|
|
category: fragment.category,
|
|
|
|
properties: fragment.properties,
|
|
|
|
sensitiveProperties: fragment.sensitiveProperties,
|
|
|
|
page: page ?? fragment.page,
|
|
|
|
referrer: fragment.referrer ?? referrer,
|
|
|
|
revenue: fragment.revenue,
|
|
|
|
value: fragment.value,
|
|
|
|
currency: fragment.currency,
|
|
|
|
environmentType: fragment.environmentType,
|
2022-11-08 19:08:08 +01:00
|
|
|
actionId: fragment.actionId,
|
2023-03-07 16:46:41 +01:00
|
|
|
// We append success or failure to the unique-identifier so that the
|
|
|
|
// messageId can still be idempotent, but so that it differs from the
|
|
|
|
// initial event fired. The initial event was preventing new events from
|
|
|
|
// making it to mixpanel because they were using the same unique ID as
|
|
|
|
// the events processed in other parts of the fragment lifecycle.
|
|
|
|
uniqueIdentifier: fragment.uniqueIdentifier
|
|
|
|
? `${fragment.uniqueIdentifier}-${abandoned ? 'failure' : 'success'}`
|
|
|
|
: undefined,
|
2022-01-12 20:31:54 +01:00
|
|
|
});
|
|
|
|
const { fragments } = this.store.getState();
|
|
|
|
delete fragments[id];
|
|
|
|
this.store.updateState({ fragments });
|
|
|
|
}
|
|
|
|
|
2022-03-31 21:21:01 +02:00
|
|
|
/**
|
|
|
|
* Calls this._identify with validated metaMetricsId and user traits if user is participating
|
|
|
|
* in the MetaMetrics analytics program
|
|
|
|
*
|
2022-07-27 15:28:05 +02:00
|
|
|
* @param {object} userTraits
|
2022-03-31 21:21:01 +02:00
|
|
|
*/
|
|
|
|
identify(userTraits) {
|
|
|
|
const { metaMetricsId, participateInMetaMetrics } = this.state;
|
|
|
|
|
|
|
|
if (!participateInMetaMetrics || !metaMetricsId || !userTraits) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (typeof userTraits !== 'object') {
|
|
|
|
console.warn(
|
|
|
|
`MetaMetricsController#identify: userTraits parameter must be an object. Received type: ${typeof userTraits}`,
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const allValidTraits = this._buildValidTraits(userTraits);
|
|
|
|
|
|
|
|
this._identify(allValidTraits);
|
|
|
|
}
|
|
|
|
|
2022-08-11 19:33:33 +02:00
|
|
|
// It sets an uninstall URL ("Sorry to see you go!" page),
|
|
|
|
// which is opened if a user uninstalls the extension.
|
|
|
|
updateExtensionUninstallUrl(participateInMetaMetrics, metaMetricsId) {
|
|
|
|
const query = {};
|
|
|
|
if (participateInMetaMetrics) {
|
|
|
|
// We only want to track these things if a user opted into metrics.
|
2022-08-18 23:31:07 +02:00
|
|
|
query.mmi = Buffer.from(metaMetricsId).toString('base64');
|
2022-08-11 19:33:33 +02:00
|
|
|
query.env = this.environment;
|
|
|
|
query.av = this.version;
|
|
|
|
}
|
|
|
|
const queryString = new URLSearchParams(query);
|
2022-08-18 23:31:07 +02:00
|
|
|
|
|
|
|
// this.extension not currently defined in tests
|
|
|
|
if (this.extension && this.extension.runtime) {
|
|
|
|
this.extension.runtime.setUninstallURL(
|
|
|
|
`${EXTENSION_UNINSTALL_URL}?${queryString}`,
|
|
|
|
);
|
|
|
|
}
|
2022-08-11 19:33:33 +02:00
|
|
|
}
|
|
|
|
|
2020-12-02 22:41:30 +01:00
|
|
|
/**
|
|
|
|
* Setter for the `participateInMetaMetrics` property
|
|
|
|
*
|
|
|
|
* @param {boolean} participateInMetaMetrics - Whether or not the user wants
|
|
|
|
* to participate in MetaMetrics
|
|
|
|
* @returns {string|null} the string of the new metametrics id, or null
|
|
|
|
* if not set
|
|
|
|
*/
|
|
|
|
setParticipateInMetaMetrics(participateInMetaMetrics) {
|
2021-02-04 19:15:23 +01:00
|
|
|
let { metaMetricsId } = this.state;
|
2020-12-02 22:41:30 +01:00
|
|
|
if (participateInMetaMetrics && !metaMetricsId) {
|
2021-02-04 19:15:23 +01:00
|
|
|
metaMetricsId = this.generateMetaMetricsId();
|
2020-12-02 22:41:30 +01:00
|
|
|
} else if (participateInMetaMetrics === false) {
|
2021-02-04 19:15:23 +01:00
|
|
|
metaMetricsId = null;
|
2020-12-02 22:41:30 +01:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
this.store.updateState({ participateInMetaMetrics, metaMetricsId });
|
2022-08-11 19:33:33 +02:00
|
|
|
if (participateInMetaMetrics) {
|
|
|
|
this.trackEventsAfterMetricsOptIn();
|
|
|
|
this.clearEventsAfterMetricsOptIn();
|
|
|
|
}
|
2022-08-18 23:31:07 +02:00
|
|
|
|
|
|
|
this.updateExtensionUninstallUrl(participateInMetaMetrics, metaMetricsId);
|
2021-02-04 19:15:23 +01:00
|
|
|
return metaMetricsId;
|
2020-12-02 22:41:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
get state() {
|
2021-02-04 19:15:23 +01:00
|
|
|
return this.store.getState();
|
2020-12-02 22:41:30 +01:00
|
|
|
}
|
|
|
|
|
2022-03-28 23:56:56 +02:00
|
|
|
/**
|
|
|
|
* track a page view with Segment
|
|
|
|
*
|
|
|
|
* @param {MetaMetricsPagePayload} payload - details of the page viewed
|
|
|
|
* @param {MetaMetricsPageOptions} [options] - options for handling the page
|
|
|
|
* view
|
|
|
|
*/
|
2022-11-08 19:08:08 +01:00
|
|
|
trackPage(
|
|
|
|
{ name, params, environmentType, page, referrer, actionId },
|
|
|
|
options,
|
|
|
|
) {
|
2022-03-28 23:56:56 +02:00
|
|
|
try {
|
|
|
|
if (this.state.participateInMetaMetrics === false) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
|
|
|
this.state.participateInMetaMetrics === null &&
|
|
|
|
!options?.isOptInPath
|
|
|
|
) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const { metaMetricsId } = this.state;
|
|
|
|
const idTrait = metaMetricsId ? 'userId' : 'anonymousId';
|
|
|
|
const idValue = metaMetricsId ?? METAMETRICS_ANONYMOUS_ID;
|
2022-11-08 00:03:03 +01:00
|
|
|
this._submitSegmentAPICall('page', {
|
2022-11-08 19:08:08 +01:00
|
|
|
messageId: buildUniqueMessageId({ actionId }),
|
2022-03-28 23:56:56 +02:00
|
|
|
[idTrait]: idValue,
|
|
|
|
name,
|
|
|
|
properties: {
|
|
|
|
params,
|
|
|
|
locale: this.locale,
|
|
|
|
chain_id: this.chainId,
|
|
|
|
environment_type: environmentType,
|
|
|
|
},
|
|
|
|
context: this._buildContext(referrer, page),
|
|
|
|
});
|
|
|
|
} catch (err) {
|
|
|
|
this._captureException(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* submits a metametrics event, not waiting for it to complete or allowing its error to bubble up
|
|
|
|
*
|
|
|
|
* @param {MetaMetricsEventPayload} payload - details of the event
|
|
|
|
* @param {MetaMetricsEventOptions} [options] - options for handling/routing the event
|
|
|
|
*/
|
|
|
|
trackEvent(payload, options) {
|
|
|
|
// validation is not caught and handled
|
|
|
|
this.validatePayload(payload);
|
|
|
|
this.submitEvent(payload, options).catch((err) =>
|
|
|
|
this._captureException(err),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* submits (or queues for submission) a metametrics event, performing necessary payload manipulation and
|
|
|
|
* routing the event to the appropriate segment source. Will split events
|
|
|
|
* with sensitiveProperties into two events, tracking the sensitiveProperties
|
|
|
|
* with the anonymousId only.
|
|
|
|
*
|
|
|
|
* @param {MetaMetricsEventPayload} payload - details of the event
|
|
|
|
* @param {MetaMetricsEventOptions} [options] - options for handling/routing the event
|
|
|
|
* @returns {Promise<void>}
|
|
|
|
*/
|
|
|
|
async submitEvent(payload, options) {
|
|
|
|
this.validatePayload(payload);
|
|
|
|
|
|
|
|
if (!this.state.participateInMetaMetrics && !options?.isOptIn) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We might track multiple events if sensitiveProperties is included, this array will hold
|
|
|
|
// the promises returned from this._track.
|
|
|
|
const events = [];
|
|
|
|
|
|
|
|
if (payload.sensitiveProperties) {
|
|
|
|
// sensitiveProperties will only be tracked using the anonymousId property and generic id
|
|
|
|
// If the event options already specify to exclude the metaMetricsId we throw an error as
|
|
|
|
// a signal to the developer that the event was implemented incorrectly
|
|
|
|
if (options?.excludeMetaMetricsId === true) {
|
|
|
|
throw new Error(
|
|
|
|
'sensitiveProperties was specified in an event payload that also set the excludeMetaMetricsId flag',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const combinedProperties = merge(
|
|
|
|
payload.sensitiveProperties,
|
|
|
|
payload.properties,
|
|
|
|
);
|
|
|
|
|
|
|
|
events.push(
|
|
|
|
this._track(
|
|
|
|
this._buildEventPayload({
|
|
|
|
...payload,
|
|
|
|
properties: combinedProperties,
|
2023-01-05 15:49:55 +01:00
|
|
|
isDuplicateAnonymizedEvent: true,
|
2022-03-28 23:56:56 +02:00
|
|
|
}),
|
|
|
|
{ ...options, excludeMetaMetricsId: true },
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
events.push(this._track(this._buildEventPayload(payload), options));
|
|
|
|
|
|
|
|
await Promise.all(events);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* validates a metametrics event
|
|
|
|
*
|
|
|
|
* @param {MetaMetricsEventPayload} payload - details of the event
|
|
|
|
*/
|
|
|
|
validatePayload(payload) {
|
|
|
|
// event and category are required fields for all payloads
|
|
|
|
if (!payload.event || !payload.category) {
|
|
|
|
throw new Error(
|
|
|
|
`Must specify event and category. Event was: ${
|
|
|
|
payload.event
|
|
|
|
}. Category was: ${payload.category}. Payload keys were: ${Object.keys(
|
|
|
|
payload,
|
|
|
|
)}. ${
|
|
|
|
typeof payload.properties === 'object'
|
|
|
|
? `Payload property keys were: ${Object.keys(payload.properties)}`
|
|
|
|
: ''
|
|
|
|
}`,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
handleMetaMaskStateUpdate(newState) {
|
|
|
|
const userTraits = this._buildUserTraitsObject(newState);
|
|
|
|
if (userTraits) {
|
2022-03-31 21:21:01 +02:00
|
|
|
this.identify(userTraits);
|
2022-03-28 23:56:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-11 19:33:33 +02:00
|
|
|
// Track all queued events after a user opted into metrics.
|
|
|
|
trackEventsAfterMetricsOptIn() {
|
|
|
|
const { eventsBeforeMetricsOptIn } = this.store.getState();
|
|
|
|
eventsBeforeMetricsOptIn.forEach((eventBeforeMetricsOptIn) => {
|
|
|
|
this.trackEvent(eventBeforeMetricsOptIn);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Once we track queued events after a user opts into metrics, we want to clear the event queue.
|
|
|
|
clearEventsAfterMetricsOptIn() {
|
|
|
|
this.store.updateState({
|
|
|
|
eventsBeforeMetricsOptIn: [],
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// It adds an event into a queue, which is only tracked if a user opts into metrics.
|
|
|
|
addEventBeforeMetricsOptIn(event) {
|
|
|
|
const prevState = this.store.getState().eventsBeforeMetricsOptIn;
|
|
|
|
this.store.updateState({
|
|
|
|
eventsBeforeMetricsOptIn: [...prevState, event],
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add or update traits for tracking.
|
|
|
|
updateTraits(newTraits) {
|
|
|
|
const { traits } = this.store.getState();
|
|
|
|
this.store.updateState({
|
|
|
|
traits: { ...traits, ...newTraits },
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-03-28 23:56:56 +02:00
|
|
|
/** PRIVATE METHODS */
|
|
|
|
|
2020-12-02 22:41:30 +01:00
|
|
|
/**
|
|
|
|
* Build the context object to attach to page and track events.
|
2022-01-07 16:57:33 +01:00
|
|
|
*
|
2020-12-02 22:41:30 +01:00
|
|
|
* @private
|
|
|
|
* @param {Pick<MetaMetricsContext, 'referrer'>} [referrer] - dapp origin that initialized
|
|
|
|
* the notification window.
|
|
|
|
* @param {Pick<MetaMetricsContext, 'page'>} [page] - page object describing the current
|
|
|
|
* view of the extension. Defaults to the background-process object.
|
|
|
|
* @returns {MetaMetricsContext}
|
|
|
|
*/
|
|
|
|
_buildContext(referrer, page = METAMETRICS_BACKGROUND_PAGE_OBJECT) {
|
|
|
|
return {
|
|
|
|
app: {
|
|
|
|
name: 'MetaMask Extension',
|
|
|
|
version: this.version,
|
|
|
|
},
|
|
|
|
userAgent: window.navigator.userAgent,
|
|
|
|
page,
|
|
|
|
referrer,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2020-12-02 22:41:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Build's the event payload, processing all fields into a format that can be
|
|
|
|
* fed to Segment's track method
|
2022-01-07 16:57:33 +01:00
|
|
|
*
|
2020-12-02 22:41:30 +01:00
|
|
|
* @private
|
|
|
|
* @param {
|
|
|
|
* Omit<MetaMetricsEventPayload, 'sensitiveProperties'>
|
|
|
|
* } rawPayload - raw payload provided to trackEvent
|
2022-01-07 16:57:33 +01:00
|
|
|
* @returns {SegmentEventPayload} formatted event payload for segment
|
2020-12-02 22:41:30 +01:00
|
|
|
*/
|
|
|
|
_buildEventPayload(rawPayload) {
|
|
|
|
const {
|
|
|
|
event,
|
|
|
|
properties,
|
|
|
|
revenue,
|
|
|
|
value,
|
|
|
|
currency,
|
|
|
|
category,
|
|
|
|
page,
|
|
|
|
referrer,
|
|
|
|
environmentType = ENVIRONMENT_TYPE_BACKGROUND,
|
2021-02-04 19:15:23 +01:00
|
|
|
} = rawPayload;
|
2020-12-02 22:41:30 +01:00
|
|
|
return {
|
|
|
|
event,
|
2022-11-08 19:08:08 +01:00
|
|
|
messageId: buildUniqueMessageId(rawPayload),
|
2020-12-02 22:41:30 +01:00
|
|
|
properties: {
|
|
|
|
// These values are omitted from properties because they have special meaning
|
|
|
|
// in segment. https://segment.com/docs/connections/spec/track/#properties.
|
|
|
|
// to avoid accidentally using these inappropriately, you must add them as top
|
|
|
|
// level properties on the event payload. We also exclude locale to prevent consumers
|
|
|
|
// from overwriting this context level property. We track it as a property
|
|
|
|
// because not all destinations map locale from context.
|
|
|
|
...omit(properties, ['revenue', 'locale', 'currency', 'value']),
|
|
|
|
revenue,
|
|
|
|
value,
|
|
|
|
currency,
|
|
|
|
category,
|
|
|
|
locale: this.locale,
|
2021-02-23 16:58:35 +01:00
|
|
|
chain_id: properties?.chain_id ?? this.chainId,
|
2020-12-02 22:41:30 +01:00
|
|
|
environment_type: environmentType,
|
|
|
|
},
|
|
|
|
context: this._buildContext(referrer, page),
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2020-12-02 22:41:30 +01:00
|
|
|
}
|
|
|
|
|
2022-04-13 18:46:49 +02:00
|
|
|
/**
|
|
|
|
* This method generates the MetaMetrics user traits object, omitting any
|
|
|
|
* traits that have not changed since the last invocation of this method.
|
|
|
|
*
|
|
|
|
* @param {object} metamaskState - Full metamask state object.
|
|
|
|
* @returns {MetaMetricsTraits | null} traits that have changed since last update
|
|
|
|
*/
|
2022-03-28 23:56:56 +02:00
|
|
|
_buildUserTraitsObject(metamaskState) {
|
2022-11-16 15:17:55 +01:00
|
|
|
const { traits, previousUserTraits } = this.store.getState();
|
2022-04-21 23:46:12 +02:00
|
|
|
/** @type {MetaMetricsTraits} */
|
2022-03-28 23:56:56 +02:00
|
|
|
const currentTraits = {
|
2023-04-03 17:31:04 +02:00
|
|
|
[MetaMetricsUserTrait.AddressBookEntries]: sum(
|
2022-04-13 18:46:49 +02:00
|
|
|
Object.values(metamaskState.addressBook).map(size),
|
|
|
|
),
|
2023-04-03 17:31:04 +02:00
|
|
|
[MetaMetricsUserTrait.InstallDateExt]:
|
|
|
|
traits[MetaMetricsUserTrait.InstallDateExt] || '',
|
|
|
|
[MetaMetricsUserTrait.LedgerConnectionType]:
|
|
|
|
metamaskState.ledgerTransportType,
|
|
|
|
[MetaMetricsUserTrait.NetworksAdded]: Object.values(
|
2023-03-09 22:00:28 +01:00
|
|
|
metamaskState.networkConfigurations,
|
|
|
|
).map((networkConfiguration) => networkConfiguration.chainId),
|
2023-04-03 17:31:04 +02:00
|
|
|
[MetaMetricsUserTrait.NetworksWithoutTicker]: Object.values(
|
2023-03-09 22:00:28 +01:00
|
|
|
metamaskState.networkConfigurations,
|
|
|
|
)
|
|
|
|
.filter(({ ticker }) => !ticker)
|
|
|
|
.map(({ chainId }) => chainId),
|
2023-04-03 17:31:04 +02:00
|
|
|
[MetaMetricsUserTrait.NftAutodetectionEnabled]:
|
|
|
|
metamaskState.useNftDetection,
|
|
|
|
[MetaMetricsUserTrait.NumberOfAccounts]: Object.values(
|
|
|
|
metamaskState.identities,
|
|
|
|
).length,
|
|
|
|
[MetaMetricsUserTrait.NumberOfNftCollections]:
|
|
|
|
this._getAllUniqueNFTAddressesLength(metamaskState.allNfts),
|
|
|
|
[MetaMetricsUserTrait.NumberOfNfts]: this._getAllNFTsFlattened(
|
2022-11-15 19:49:42 +01:00
|
|
|
metamaskState.allNfts,
|
2023-04-03 17:31:04 +02:00
|
|
|
).length,
|
|
|
|
[MetaMetricsUserTrait.NumberOfTokens]:
|
|
|
|
this._getNumberOfTokens(metamaskState),
|
|
|
|
[MetaMetricsUserTrait.OpenseaApiEnabled]: metamaskState.openSeaEnabled,
|
|
|
|
[MetaMetricsUserTrait.ThreeBoxEnabled]: false, // deprecated, hard-coded as false
|
|
|
|
[MetaMetricsUserTrait.Theme]: metamaskState.theme || 'default',
|
|
|
|
[MetaMetricsUserTrait.TokenDetectionEnabled]:
|
|
|
|
metamaskState.useTokenDetection,
|
2023-03-06 20:35:00 +01:00
|
|
|
///: BEGIN:ONLY_INCLUDE_IN(flask)
|
2023-04-03 17:31:04 +02:00
|
|
|
[MetaMetricsUserTrait.DesktopEnabled]:
|
|
|
|
metamaskState.desktopEnabled || false,
|
2023-03-02 21:55:27 +01:00
|
|
|
///: END:ONLY_INCLUDE_IN
|
2023-04-03 17:31:04 +02:00
|
|
|
[MetaMetricsUserTrait.SecurityProviders]:
|
|
|
|
metamaskState.transactionSecurityCheckEnabled ? ['opensea'] : [],
|
2022-03-28 23:56:56 +02:00
|
|
|
};
|
|
|
|
|
2022-11-16 15:17:55 +01:00
|
|
|
if (!previousUserTraits) {
|
|
|
|
this.store.updateState({ previousUserTraits: currentTraits });
|
2022-03-28 23:56:56 +02:00
|
|
|
return currentTraits;
|
|
|
|
}
|
|
|
|
|
2022-11-16 15:17:55 +01:00
|
|
|
if (previousUserTraits && !isEqual(previousUserTraits, currentTraits)) {
|
2022-03-28 23:56:56 +02:00
|
|
|
const updates = pickBy(
|
|
|
|
currentTraits,
|
2022-11-16 15:17:55 +01:00
|
|
|
(v, k) => !isEqual(previousUserTraits[k], v),
|
2022-03-28 23:56:56 +02:00
|
|
|
);
|
2022-11-16 15:17:55 +01:00
|
|
|
this.store.updateState({ previousUserTraits: currentTraits });
|
2022-03-28 23:56:56 +02:00
|
|
|
return updates;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2022-03-31 21:21:01 +02:00
|
|
|
/**
|
|
|
|
* Returns a new object of all valid user traits. For dates, we transform them into ISO-8601 timestamp strings.
|
|
|
|
*
|
|
|
|
* @see {@link https://segment.com/docs/connections/spec/common/#timestamps}
|
2022-07-27 15:28:05 +02:00
|
|
|
* @param {object} userTraits
|
|
|
|
* @returns {object}
|
2022-03-31 21:21:01 +02:00
|
|
|
*/
|
|
|
|
_buildValidTraits(userTraits) {
|
|
|
|
return Object.entries(userTraits).reduce((validTraits, [key, value]) => {
|
|
|
|
if (this._isValidTraitDate(value)) {
|
|
|
|
validTraits[key] = value.toISOString();
|
|
|
|
} else if (this._isValidTrait(value)) {
|
|
|
|
validTraits[key] = value;
|
|
|
|
} else {
|
|
|
|
console.warn(
|
|
|
|
`MetaMetricsController: "${key}" value is not a valid trait type`,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return validTraits;
|
|
|
|
}, {});
|
|
|
|
}
|
|
|
|
|
2022-04-10 09:42:00 +02:00
|
|
|
/**
|
2023-02-16 20:23:29 +01:00
|
|
|
* Returns an array of all of the NFTs the user
|
2022-04-21 23:46:12 +02:00
|
|
|
* possesses across all networks and accounts.
|
2022-04-10 09:42:00 +02:00
|
|
|
*
|
2022-11-15 19:49:42 +01:00
|
|
|
* @param {object} allNfts
|
2022-04-21 23:46:12 +02:00
|
|
|
* @returns {[]}
|
2022-04-10 09:42:00 +02:00
|
|
|
*/
|
2022-11-15 19:49:42 +01:00
|
|
|
_getAllNFTsFlattened = memoize((allNfts = {}) => {
|
|
|
|
return Object.values(allNfts).reduce((result, chainNFTs) => {
|
2022-06-15 21:52:32 +02:00
|
|
|
return result.concat(...Object.values(chainNFTs));
|
|
|
|
}, []);
|
2022-04-21 23:46:12 +02:00
|
|
|
});
|
2022-04-10 09:42:00 +02:00
|
|
|
|
2022-04-21 23:46:12 +02:00
|
|
|
/**
|
2023-02-16 20:23:29 +01:00
|
|
|
* Returns the number of unique NFT addresses the user
|
2022-04-21 23:46:12 +02:00
|
|
|
* possesses across all networks and accounts.
|
|
|
|
*
|
2022-11-15 19:49:42 +01:00
|
|
|
* @param {object} allNfts
|
2022-04-21 23:46:12 +02:00
|
|
|
* @returns {number}
|
|
|
|
*/
|
2022-11-15 19:49:42 +01:00
|
|
|
_getAllUniqueNFTAddressesLength(allNfts = {}) {
|
|
|
|
const allNFTAddresses = this._getAllNFTsFlattened(allNfts).map(
|
2022-04-21 23:46:12 +02:00
|
|
|
(nft) => nft.address,
|
|
|
|
);
|
|
|
|
const uniqueAddresses = new Set(allNFTAddresses);
|
|
|
|
return uniqueAddresses.size;
|
2022-04-10 09:42:00 +02:00
|
|
|
}
|
|
|
|
|
2022-04-15 18:35:07 +02:00
|
|
|
/**
|
|
|
|
* @param {object} metamaskState
|
|
|
|
* @returns number of unique token addresses
|
|
|
|
*/
|
|
|
|
_getNumberOfTokens(metamaskState) {
|
|
|
|
return Object.values(metamaskState.allTokens).reduce(
|
|
|
|
(result, accountsByChain) => {
|
|
|
|
return result + sum(Object.values(accountsByChain).map(size));
|
|
|
|
},
|
|
|
|
0,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-03-31 21:21:01 +02:00
|
|
|
/**
|
|
|
|
* Calls segment.identify with given user traits
|
|
|
|
*
|
|
|
|
* @see {@link https://segment.com/docs/connections/sources/catalog/libraries/server/node/#identify}
|
|
|
|
* @private
|
2022-07-27 15:28:05 +02:00
|
|
|
* @param {object} userTraits
|
2022-03-31 21:21:01 +02:00
|
|
|
*/
|
|
|
|
_identify(userTraits) {
|
|
|
|
const { metaMetricsId } = this.state;
|
|
|
|
|
|
|
|
if (!userTraits || Object.keys(userTraits).length === 0) {
|
|
|
|
console.warn('MetaMetricsController#_identify: No userTraits found');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2022-11-08 00:03:03 +01:00
|
|
|
this._submitSegmentAPICall('identify', {
|
2022-03-31 21:21:01 +02:00
|
|
|
userId: metaMetricsId,
|
|
|
|
traits: userTraits,
|
|
|
|
});
|
|
|
|
} catch (err) {
|
|
|
|
this._captureException(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validates the trait value. Segment accepts any data type. We are adding validation here to
|
|
|
|
* support data types for our Segment destination(s) e.g. MixPanel
|
|
|
|
*
|
|
|
|
* @param {*} value
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
|
|
|
_isValidTrait(value) {
|
|
|
|
const type = typeof value;
|
|
|
|
|
|
|
|
return (
|
|
|
|
type === 'string' ||
|
|
|
|
type === 'boolean' ||
|
|
|
|
type === 'number' ||
|
|
|
|
this._isValidTraitArray(value) ||
|
|
|
|
this._isValidTraitDate(value)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Segment accepts any data type value. We have special logic to validate arrays.
|
|
|
|
*
|
|
|
|
* @param {*} value
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
|
|
|
_isValidTraitArray = (value) => {
|
|
|
|
return (
|
|
|
|
Array.isArray(value) &&
|
|
|
|
(value.every((element) => {
|
|
|
|
return typeof element === 'string';
|
|
|
|
}) ||
|
|
|
|
value.every((element) => {
|
|
|
|
return typeof element === 'boolean';
|
|
|
|
}) ||
|
|
|
|
value.every((element) => {
|
|
|
|
return typeof element === 'number';
|
|
|
|
}))
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if the value is an accepted date type
|
|
|
|
*
|
|
|
|
* @param {*} value
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
|
|
|
_isValidTraitDate = (value) => {
|
|
|
|
return Object.prototype.toString.call(value) === '[object Date]';
|
|
|
|
};
|
|
|
|
|
2020-12-02 22:41:30 +01:00
|
|
|
/**
|
|
|
|
* Perform validation on the payload and update the id type to use before
|
|
|
|
* sending to Segment. Also examines the options to route and handle the
|
|
|
|
* event appropriately.
|
2022-01-07 16:57:33 +01:00
|
|
|
*
|
2020-12-02 22:41:30 +01:00
|
|
|
* @private
|
|
|
|
* @param {SegmentEventPayload} payload - properties to attach to event
|
2020-12-03 20:05:11 +01:00
|
|
|
* @param {MetaMetricsEventOptions} [options] - options for routing and
|
2020-12-02 22:41:30 +01:00
|
|
|
* handling the event
|
|
|
|
* @returns {Promise<void>}
|
|
|
|
*/
|
|
|
|
_track(payload, options) {
|
|
|
|
const {
|
|
|
|
isOptIn,
|
|
|
|
metaMetricsId: metaMetricsIdOverride,
|
|
|
|
matomoEvent,
|
|
|
|
flushImmediately,
|
2021-02-04 19:15:23 +01:00
|
|
|
} = options || {};
|
|
|
|
let idType = 'userId';
|
|
|
|
let idValue = this.state.metaMetricsId;
|
|
|
|
let excludeMetaMetricsId = options?.excludeMetaMetricsId ?? false;
|
2020-12-02 22:41:30 +01:00
|
|
|
// This is carried over from the old implementation, and will likely need
|
|
|
|
// to be updated to work with the new tracking plan. I think we should use
|
|
|
|
// a config setting for this instead of trying to match the event name
|
2021-02-04 19:15:23 +01:00
|
|
|
const isSendFlow = Boolean(payload.event.match(/^send|^confirm/iu));
|
2021-06-25 00:37:44 +02:00
|
|
|
if (isSendFlow) {
|
2021-02-04 19:15:23 +01:00
|
|
|
excludeMetaMetricsId = true;
|
2020-12-02 22:41:30 +01:00
|
|
|
}
|
|
|
|
// If we are tracking sensitive data we will always use the anonymousId
|
|
|
|
// property as well as our METAMETRICS_ANONYMOUS_ID. This prevents us from
|
|
|
|
// associating potentially identifiable information with a specific id.
|
|
|
|
// During the opt in flow we will track all events, but do so with the
|
|
|
|
// anonymous id. The one exception to that rule is after the user opts in
|
|
|
|
// to MetaMetrics. When that happens we receive back the user's new
|
|
|
|
// MetaMetrics id before it is fully persisted to state. To avoid a race
|
|
|
|
// condition we explicitly pass the new id to the track method. In that
|
|
|
|
// case we will track the opt in event to the user's id. In all other cases
|
|
|
|
// we use the metaMetricsId from state.
|
|
|
|
if (excludeMetaMetricsId || (isOptIn && !metaMetricsIdOverride)) {
|
2021-02-04 19:15:23 +01:00
|
|
|
idType = 'anonymousId';
|
|
|
|
idValue = METAMETRICS_ANONYMOUS_ID;
|
2020-12-02 22:41:30 +01:00
|
|
|
} else if (isOptIn && metaMetricsIdOverride) {
|
2021-02-04 19:15:23 +01:00
|
|
|
idValue = metaMetricsIdOverride;
|
2020-12-02 22:41:30 +01:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
payload[idType] = idValue;
|
2020-12-02 22:41:30 +01:00
|
|
|
|
2021-04-26 18:05:43 +02:00
|
|
|
// If this is an event on the old matomo schema, add a key to the payload
|
|
|
|
// to designate it as such
|
|
|
|
if (matomoEvent === true) {
|
|
|
|
payload.properties.legacy_event = true;
|
|
|
|
}
|
|
|
|
|
2020-12-02 22:41:30 +01:00
|
|
|
// Promises will only resolve when the event is sent to segment. For any
|
|
|
|
// event that relies on this promise being fulfilled before performing UI
|
|
|
|
// updates, or otherwise delaying user interaction, supply the
|
|
|
|
// 'flushImmediately' flag to the trackEvent method.
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const callback = (err) => {
|
|
|
|
if (err) {
|
2020-12-23 04:54:49 +01:00
|
|
|
// The error that segment gives us has some manipulation done to it
|
|
|
|
// that seemingly breaks with lockdown enabled. Creating a new error
|
|
|
|
// here prevents the system from freezing when the network request to
|
|
|
|
// segment fails for any reason.
|
2021-02-04 19:15:23 +01:00
|
|
|
const safeError = new Error(err.message);
|
|
|
|
safeError.stack = err.stack;
|
|
|
|
return reject(safeError);
|
2020-12-02 22:41:30 +01:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
return resolve();
|
|
|
|
};
|
2020-12-02 22:41:30 +01:00
|
|
|
|
2022-11-08 00:03:03 +01:00
|
|
|
this._submitSegmentAPICall('track', payload, callback);
|
2020-12-02 22:41:30 +01:00
|
|
|
if (flushImmediately) {
|
2021-04-26 18:05:43 +02:00
|
|
|
this.segment.flush();
|
2020-12-02 22:41:30 +01:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2020-12-02 22:41:30 +01:00
|
|
|
}
|
2022-11-08 00:03:03 +01:00
|
|
|
|
|
|
|
// Method below submits the request to analytics SDK.
|
|
|
|
// It will also add event to controller store
|
|
|
|
// and pass a callback to remove it from store once request is submitted to segment
|
|
|
|
// Saving segmentApiCalls in controller store in MV3 ensures that events are tracked
|
|
|
|
// even if service worker terminates before events are submiteed to segment.
|
|
|
|
_submitSegmentAPICall(eventType, payload, callback) {
|
2022-11-23 19:00:05 +01:00
|
|
|
const { metaMetricsId, participateInMetaMetrics } = this.state;
|
|
|
|
if (!participateInMetaMetrics || !metaMetricsId) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-11-08 00:03:03 +01:00
|
|
|
const messageId = payload.messageId || generateRandomId();
|
|
|
|
let timestamp = new Date();
|
|
|
|
if (payload.timestamp) {
|
|
|
|
const payloadDate = new Date(payload.timestamp);
|
|
|
|
if (isValidDate(payloadDate)) {
|
|
|
|
timestamp = payloadDate;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const modifiedPayload = { ...payload, messageId, timestamp };
|
|
|
|
this.store.updateState({
|
|
|
|
segmentApiCalls: {
|
|
|
|
...this.store.getState().segmentApiCalls,
|
|
|
|
[messageId]: {
|
|
|
|
eventType,
|
|
|
|
payload: {
|
|
|
|
...modifiedPayload,
|
|
|
|
timestamp: modifiedPayload.timestamp.toString(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
const modifiedCallback = (result) => {
|
|
|
|
const { segmentApiCalls } = this.store.getState();
|
|
|
|
delete segmentApiCalls[messageId];
|
|
|
|
this.store.updateState({
|
|
|
|
segmentApiCalls,
|
|
|
|
});
|
|
|
|
return callback?.(result);
|
|
|
|
};
|
|
|
|
this.segment[eventType](modifiedPayload, modifiedCallback);
|
|
|
|
}
|
2020-12-02 22:41:30 +01:00
|
|
|
}
|