2021-02-04 19:15:23 +01:00
|
|
|
import { useContext, useCallback } from 'react';
|
2022-01-12 20:31:54 +01:00
|
|
|
import { useSelector } from 'react-redux';
|
|
|
|
import { useRouteMatch } from 'react-router-dom';
|
2021-02-04 19:15:23 +01:00
|
|
|
import { MetaMetricsContext } from '../contexts/metametrics';
|
|
|
|
import { MetaMetricsContext as NewMetaMetricsContext } from '../contexts/metametrics.new';
|
2022-01-12 20:31:54 +01:00
|
|
|
import { PATH_NAME_MAP } from '../helpers/constants/routes';
|
|
|
|
import { txDataSelector } from '../selectors';
|
2021-02-04 19:15:23 +01:00
|
|
|
import { useEqualityCheck } from './useEqualityCheck';
|
2020-05-08 18:41:43 +02:00
|
|
|
|
2020-12-02 22:41:30 +01:00
|
|
|
// Type imports
|
|
|
|
/**
|
|
|
|
* @typedef {import('../contexts/metametrics.new').UIMetricsEventPayload} UIMetricsEventPayload
|
2021-04-28 21:53:59 +02:00
|
|
|
* @typedef {import('../../shared/constants/metametrics').MetaMetricsEventOptions} MetaMetricsEventOptions
|
2020-12-02 22:41:30 +01:00
|
|
|
*/
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
export function useMetricEvent(config = {}, overrides = {}) {
|
2021-02-04 19:15:23 +01:00
|
|
|
const metricsEvent = useContext(MetaMetricsContext);
|
2020-11-03 00:41:28 +01:00
|
|
|
const trackEvent = useCallback(() => metricsEvent(config, overrides), [
|
|
|
|
config,
|
|
|
|
metricsEvent,
|
|
|
|
overrides,
|
2021-02-04 19:15:23 +01:00
|
|
|
]);
|
|
|
|
return trackEvent;
|
2020-05-08 18:41:43 +02:00
|
|
|
}
|
2020-09-14 19:04:05 +02:00
|
|
|
|
|
|
|
/**
|
2020-11-03 00:41:28 +01:00
|
|
|
* track a metametrics event using segment
|
|
|
|
* e.g metricsEvent({ event: 'Unlocked MetaMask', category: 'Navigation' })
|
|
|
|
*
|
2022-01-07 16:57:33 +01:00
|
|
|
* @param {UIMetricsEventPayload} payload - payload of the event to track
|
2020-12-02 22:41:30 +01:00
|
|
|
* @param {MetaMetricsEventOptions} options - options for handling/routing event
|
2022-01-07 16:57:33 +01:00
|
|
|
* @returns {() => Promise<void>} function to execute the tracking event
|
2020-11-03 00:41:28 +01:00
|
|
|
*/
|
2020-12-02 22:41:30 +01:00
|
|
|
export function useNewMetricEvent(payload, options) {
|
2021-02-04 19:15:23 +01:00
|
|
|
const memoizedPayload = useEqualityCheck(payload);
|
|
|
|
const memoizedOptions = useEqualityCheck(options);
|
|
|
|
const metricsEvent = useContext(NewMetaMetricsContext);
|
2020-12-02 22:41:30 +01:00
|
|
|
|
|
|
|
return useCallback(() => metricsEvent(memoizedPayload, memoizedOptions), [
|
2020-11-03 00:41:28 +01:00
|
|
|
metricsEvent,
|
2020-12-02 22:41:30 +01:00
|
|
|
memoizedPayload,
|
|
|
|
memoizedOptions,
|
2021-02-04 19:15:23 +01:00
|
|
|
]);
|
2020-09-14 19:04:05 +02:00
|
|
|
}
|
2022-01-12 20:31:54 +01:00
|
|
|
|
|
|
|
const PATHS_TO_CHECK = Object.keys(PATH_NAME_MAP);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the current page if it matches our route map, as well as the origin
|
|
|
|
* if there is a confirmation that was triggered by a dapp. These values are
|
|
|
|
* not required but add valuable context to events, and should be included in
|
|
|
|
* the context object on the event payload.
|
|
|
|
*
|
|
|
|
* @returns {{
|
|
|
|
* page?: MetaMetricsPageObject
|
|
|
|
* referrer?: MetaMetricsReferrerObject
|
|
|
|
* }}
|
|
|
|
*/
|
|
|
|
export function useMetaMetricsContext() {
|
|
|
|
const match = useRouteMatch({
|
|
|
|
path: PATHS_TO_CHECK,
|
|
|
|
exact: true,
|
|
|
|
strict: true,
|
|
|
|
});
|
|
|
|
const txData = useSelector(txDataSelector) || {};
|
|
|
|
const confirmTransactionOrigin = txData.origin;
|
|
|
|
|
|
|
|
const referrer = confirmTransactionOrigin
|
|
|
|
? {
|
|
|
|
url: confirmTransactionOrigin,
|
|
|
|
}
|
|
|
|
: undefined;
|
|
|
|
|
|
|
|
const page = match
|
|
|
|
? {
|
|
|
|
path: match.path,
|
|
|
|
title: PATH_NAME_MAP[match.path],
|
|
|
|
url: match.path,
|
|
|
|
}
|
|
|
|
: undefined;
|
|
|
|
|
|
|
|
return {
|
|
|
|
page,
|
|
|
|
referrer,
|
|
|
|
};
|
|
|
|
}
|