2020-09-14 19:04:05 +02:00
|
|
|
/**
|
|
|
|
* This file is intended to be renamed to metametrics.js once the conversion is complete.
|
|
|
|
* MetaMetrics is our own brand, and should remain aptly named regardless of the underlying
|
|
|
|
* metrics system. This file implements Segment analytics tracking.
|
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
import React, {
|
|
|
|
Component,
|
|
|
|
createContext,
|
|
|
|
useEffect,
|
2020-12-02 22:41:30 +01:00
|
|
|
useRef,
|
|
|
|
useCallback,
|
2021-02-04 19:15:23 +01:00
|
|
|
} from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2022-03-25 15:26:09 +01:00
|
|
|
import { matchPath, useLocation } from 'react-router-dom';
|
2021-02-04 19:15:23 +01:00
|
|
|
import { captureException, captureMessage } from '@sentry/browser';
|
2020-09-14 19:04:05 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
import { omit } from 'lodash';
|
2021-04-28 21:53:59 +02:00
|
|
|
import { getEnvironmentType } from '../../app/scripts/lib/util';
|
2021-02-04 19:15:23 +01:00
|
|
|
import { PATH_NAME_MAP } from '../helpers/constants/routes';
|
2022-08-11 19:33:33 +02:00
|
|
|
import { CONTEXT_PROPS } from '../../shared/constants/metametrics';
|
2022-03-25 15:26:09 +01:00
|
|
|
import { useSegmentContext } from '../hooks/useSegmentContext';
|
2020-12-02 22:41:30 +01:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
import { trackMetaMetricsEvent, trackMetaMetricsPage } from '../store/actions';
|
2020-12-02 22:41:30 +01:00
|
|
|
|
|
|
|
// type imports
|
|
|
|
/**
|
2021-04-28 21:53:59 +02:00
|
|
|
* @typedef {import('../../shared/constants/metametrics').MetaMetricsEventPayload} MetaMetricsEventPayload
|
|
|
|
* @typedef {import('../../shared/constants/metametrics').MetaMetricsEventOptions} MetaMetricsEventOptions
|
|
|
|
* @typedef {import('../../shared/constants/metametrics').MetaMetricsPageObject} MetaMetricsPageObject
|
|
|
|
* @typedef {import('../../shared/constants/metametrics').MetaMetricsReferrerObject} MetaMetricsReferrerObject
|
2020-12-02 22:41:30 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
// types
|
|
|
|
/**
|
|
|
|
* @typedef {Omit<MetaMetricsEventPayload, 'environmentType' | 'page' | 'referrer'>} UIMetricsEventPayload
|
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* @typedef {(
|
|
|
|
* payload: UIMetricsEventPayload,
|
|
|
|
* options: MetaMetricsEventOptions
|
|
|
|
* ) => Promise<void>} UITrackEventMethod
|
|
|
|
*/
|
2020-09-14 19:04:05 +02:00
|
|
|
|
2020-12-02 22:41:30 +01:00
|
|
|
/**
|
|
|
|
* @type {React.Context<UITrackEventMethod>}
|
|
|
|
*/
|
2020-09-14 19:04:05 +02:00
|
|
|
export const MetaMetricsContext = createContext(() => {
|
|
|
|
captureException(
|
2020-11-03 00:41:28 +01:00
|
|
|
Error(
|
|
|
|
`MetaMetrics context function was called from a react node that is not a descendant of a MetaMetrics context provider`,
|
|
|
|
),
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
});
|
2020-09-14 19:04:05 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const PATHS_TO_CHECK = Object.keys(PATH_NAME_MAP);
|
2020-09-14 19:04:05 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
export function MetaMetricsProvider({ children }) {
|
2021-02-04 19:15:23 +01:00
|
|
|
const location = useLocation();
|
|
|
|
const context = useSegmentContext();
|
2020-10-21 23:10:55 +02:00
|
|
|
|
2022-08-11 19:33:33 +02:00
|
|
|
// Sometimes we want to track context properties inside the event's "properties" object.
|
|
|
|
const addContextPropsIntoEventProperties = (payload, options) => {
|
|
|
|
const fields = options?.contextPropsIntoEventProperties;
|
|
|
|
if (!fields || fields.length === 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!payload.properties) {
|
|
|
|
payload.properties = {};
|
|
|
|
}
|
|
|
|
if (fields.includes(CONTEXT_PROPS.PAGE_TITLE)) {
|
|
|
|
payload.properties[CONTEXT_PROPS.PAGE_TITLE] = context.page?.title;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-10-21 23:10:55 +02:00
|
|
|
/**
|
2020-12-02 22:41:30 +01:00
|
|
|
* @type {UITrackEventMethod}
|
2020-10-21 23:10:55 +02:00
|
|
|
*/
|
2020-12-02 22:41:30 +01:00
|
|
|
const trackEvent = useCallback(
|
|
|
|
(payload, options) => {
|
2022-08-11 19:33:33 +02:00
|
|
|
addContextPropsIntoEventProperties(payload, options);
|
2020-12-02 22:41:30 +01:00
|
|
|
trackMetaMetricsEvent(
|
|
|
|
{
|
|
|
|
...payload,
|
|
|
|
environmentType: getEnvironmentType(),
|
|
|
|
...context,
|
|
|
|
},
|
|
|
|
options,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2020-12-02 22:41:30 +01:00
|
|
|
},
|
|
|
|
[context],
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2020-09-14 19:04:05 +02:00
|
|
|
|
|
|
|
// Used to prevent double tracking page calls
|
2021-02-04 19:15:23 +01:00
|
|
|
const previousMatch = useRef();
|
2020-09-14 19:04:05 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Anytime the location changes, track a page change with segment.
|
|
|
|
* Previously we would manually track changes to history and keep a
|
|
|
|
* reference to the previous url, but with page tracking we can see
|
|
|
|
* which page the user is on and their navigation path.
|
|
|
|
*/
|
|
|
|
useEffect(() => {
|
2021-02-04 19:15:23 +01:00
|
|
|
const environmentType = getEnvironmentType();
|
2020-12-02 22:41:30 +01:00
|
|
|
const match = matchPath(location.pathname, {
|
|
|
|
path: PATHS_TO_CHECK,
|
|
|
|
exact: true,
|
|
|
|
strict: true,
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2020-12-02 22:41:30 +01:00
|
|
|
// Start by checking for a missing match route. If this falls through to
|
|
|
|
// the else if, then we know we have a matched route for tracking.
|
|
|
|
if (!match) {
|
|
|
|
captureMessage(`Segment page tracking found unmatched route`, {
|
|
|
|
extra: {
|
|
|
|
previousMatch,
|
|
|
|
currentPath: location.pathname,
|
|
|
|
},
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2020-12-02 22:41:30 +01:00
|
|
|
} else if (
|
|
|
|
previousMatch.current !== match.path &&
|
|
|
|
!(
|
|
|
|
environmentType === 'notification' &&
|
|
|
|
match.path === '/' &&
|
|
|
|
previousMatch.current === undefined
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
// When a notification window is open by a Dapp we do not want to track
|
|
|
|
// the initial home route load that can sometimes happen. To handle
|
|
|
|
// this we keep track of the previousMatch, and we skip the event track
|
|
|
|
// in the event that we are dealing with the initial load of the
|
|
|
|
// homepage
|
2021-02-04 19:15:23 +01:00
|
|
|
const { path, params } = match;
|
|
|
|
const name = PATH_NAME_MAP[path];
|
2020-12-02 22:41:30 +01:00
|
|
|
trackMetaMetricsPage(
|
|
|
|
{
|
2020-09-14 19:04:05 +02:00
|
|
|
name,
|
2020-12-02 22:41:30 +01:00
|
|
|
// We do not want to send addresses or accounts in any events
|
|
|
|
// Some routes include these as params.
|
|
|
|
params: omit(params, ['account', 'address']),
|
|
|
|
environmentType,
|
|
|
|
page: context.page,
|
|
|
|
referrer: context.referrer,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
isOptInPath: location.pathname.startsWith('/initialize'),
|
|
|
|
},
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2020-09-14 19:04:05 +02:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
previousMatch.current = match?.path;
|
|
|
|
}, [location, context]);
|
2020-09-14 19:04:05 +02:00
|
|
|
|
|
|
|
return (
|
|
|
|
<MetaMetricsContext.Provider value={trackEvent}>
|
|
|
|
{children}
|
|
|
|
</MetaMetricsContext.Provider>
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2020-09-14 19:04:05 +02:00
|
|
|
}
|
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
MetaMetricsProvider.propTypes = { children: PropTypes.node };
|
2020-09-14 19:04:05 +02:00
|
|
|
|
|
|
|
export class LegacyMetaMetricsProvider extends Component {
|
|
|
|
static propTypes = {
|
|
|
|
children: PropTypes.node,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2020-09-14 19:04:05 +02:00
|
|
|
|
|
|
|
static defaultProps = {
|
|
|
|
children: undefined,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2020-09-14 19:04:05 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
static contextType = MetaMetricsContext;
|
2020-09-14 19:04:05 +02:00
|
|
|
|
|
|
|
static childContextTypes = {
|
|
|
|
// This has to be different than the type name for the old metametrics file
|
|
|
|
// using the same name would result in whichever was lower in the tree to be
|
|
|
|
// used.
|
|
|
|
trackEvent: PropTypes.func,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2020-09-14 19:04:05 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
getChildContext() {
|
2020-09-14 19:04:05 +02:00
|
|
|
return {
|
|
|
|
trackEvent: this.context,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2020-09-14 19:04:05 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
render() {
|
2021-02-04 19:15:23 +01:00
|
|
|
return this.props.children;
|
2020-09-14 19:04:05 +02:00
|
|
|
}
|
|
|
|
}
|