2020-10-06 20:28:38 +02:00
|
|
|
import Analytics from 'analytics-node'
|
|
|
|
|
|
|
|
const inDevelopment = process.env.METAMASK_DEBUG || process.env.IN_TEST
|
|
|
|
|
|
|
|
const flushAt = inDevelopment ? 1 : undefined
|
|
|
|
|
2020-10-08 18:41:23 +02:00
|
|
|
const METAMETRICS_ANONYMOUS_ID = '0x0000000000000000'
|
|
|
|
|
2020-10-06 20:28:38 +02:00
|
|
|
const segmentNoop = {
|
|
|
|
track () {
|
|
|
|
// noop
|
|
|
|
},
|
|
|
|
page () {
|
|
|
|
// noop
|
|
|
|
},
|
|
|
|
identify () {
|
|
|
|
// noop
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// We do not want to track events on development builds unless specifically
|
|
|
|
// provided a SEGMENT_WRITE_KEY. This also holds true for test environments and
|
|
|
|
// E2E, which is handled in the build process by never providing the SEGMENT_WRITE_KEY
|
|
|
|
// which process.env.IN_TEST is true
|
2020-10-08 18:41:23 +02:00
|
|
|
const segment = process.env.SEGMENT_WRITE_KEY
|
2020-10-06 20:28:38 +02:00
|
|
|
? new Analytics(process.env.SEGMENT_WRITE_KEY, { flushAt })
|
|
|
|
: segmentNoop
|
|
|
|
|
2020-10-08 18:41:23 +02:00
|
|
|
/**
|
|
|
|
* Returns a function for tracking Segment events.
|
|
|
|
*
|
|
|
|
* @param {string} metamaskVersion - The current version of the MetaMask
|
|
|
|
* extension.
|
|
|
|
* @param {Function} getParticipateInMetrics - A function that returns
|
|
|
|
* whether the user participates in MetaMetrics.
|
|
|
|
* @param {Function} getMetricsState - A function for getting state relevant
|
|
|
|
* to MetaMetrics/Segment.
|
|
|
|
*/
|
|
|
|
export function getTrackSegmentEvent (
|
|
|
|
metamaskVersion,
|
|
|
|
getParticipateInMetrics,
|
|
|
|
getMetricsState,
|
|
|
|
) {
|
|
|
|
const version = process.env.METAMASK_ENVIRONMENT === 'production'
|
|
|
|
? metamaskVersion
|
|
|
|
: `${metamaskVersion}-${process.env.METAMASK_ENVIRONMENT}`
|
|
|
|
|
|
|
|
const segmentContext = {
|
|
|
|
app: {
|
|
|
|
name: 'MetaMask Extension',
|
|
|
|
version,
|
|
|
|
},
|
|
|
|
page: {
|
|
|
|
path: '/background-process',
|
|
|
|
title: 'Background Process',
|
|
|
|
url: '/background-process',
|
|
|
|
},
|
|
|
|
userAgent: window.navigator.userAgent,
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tracks a Segment event per the given arguments.
|
|
|
|
*
|
|
|
|
* @param {string} event - The event name.
|
|
|
|
* @param {string} category - The event category.
|
|
|
|
* @param {Object} [properties] - The event properties.
|
2020-10-08 20:00:56 +02:00
|
|
|
* @param {string} [referrerUrl] - The event's referrer URL, if relevant.
|
2020-10-08 18:41:23 +02:00
|
|
|
* @param {boolean} [excludeMetaMetricsId] - `true` if the user's MetaMetrics id should
|
|
|
|
* not be included, and `false` otherwise. Default: `true`
|
|
|
|
*/
|
|
|
|
return function trackSegmentEvent ({
|
|
|
|
event,
|
|
|
|
category,
|
|
|
|
properties = {},
|
|
|
|
excludeMetaMetricsId = true,
|
2020-10-08 20:00:56 +02:00
|
|
|
referrerUrl,
|
2020-10-08 18:41:23 +02:00
|
|
|
}) {
|
|
|
|
if (!event || !category) {
|
|
|
|
throw new Error('Must specify event and category.')
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!getParticipateInMetrics()) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
const { currentLocale, metaMetricsId } = getMetricsState()
|
|
|
|
|
|
|
|
const trackOptions = {
|
|
|
|
event,
|
|
|
|
category,
|
|
|
|
context: {
|
|
|
|
...segmentContext,
|
|
|
|
locale: currentLocale.replace('_', '-'),
|
|
|
|
},
|
|
|
|
properties,
|
|
|
|
}
|
|
|
|
|
|
|
|
if (excludeMetaMetricsId) {
|
|
|
|
trackOptions.anonymousId = METAMETRICS_ANONYMOUS_ID
|
|
|
|
} else {
|
|
|
|
trackOptions.userId = metaMetricsId
|
|
|
|
}
|
|
|
|
|
2020-10-08 20:00:56 +02:00
|
|
|
if (referrerUrl) {
|
|
|
|
trackOptions.context.referrer = {
|
|
|
|
url: referrerUrl,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-08 18:41:23 +02:00
|
|
|
segment.track(trackOptions)
|
|
|
|
}
|
|
|
|
}
|