1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-22 18:00:18 +01:00
metamask-extension/ui/hooks/useMetricEvent.js

41 lines
1.4 KiB
JavaScript
Raw Normal View History

import { useContext, useCallback } from 'react';
import { MetaMetricsContext } from '../contexts/metametrics';
import { MetaMetricsContext as NewMetaMetricsContext } from '../contexts/metametrics.new';
import { useEqualityCheck } from './useEqualityCheck';
2020-12-02 22:41:30 +01:00
// Type imports
/**
* @typedef {import('../contexts/metametrics.new').UIMetricsEventPayload} UIMetricsEventPayload
* @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 = {}) {
const metricsEvent = useContext(MetaMetricsContext);
2020-11-03 00:41:28 +01:00
const trackEvent = useCallback(() => metricsEvent(config, overrides), [
config,
metricsEvent,
overrides,
]);
return trackEvent;
}
/**
2020-11-03 00:41:28 +01:00
* track a metametrics event using segment
* e.g metricsEvent({ event: 'Unlocked MetaMask', category: 'Navigation' })
*
2020-12-02 22:41:30 +01:00
* @param {UIMetricsEventPayload} payload - payload of the event to track
* @param {MetaMetricsEventOptions} options - options for handling/routing event
* @return {() => 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) {
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,
]);
}