2019-03-05 16:45:01 +01:00
|
|
|
/* eslint camelcase: 0 */
|
|
|
|
|
2020-01-09 04:34:58 +01:00
|
|
|
import ethUtil from 'ethereumjs-util'
|
2019-03-05 16:45:01 +01:00
|
|
|
|
2020-07-28 21:09:20 +02:00
|
|
|
const inDevelopment = process.env.METAMASK_DEBUG || process.env.IN_TEST
|
2019-03-05 16:45:01 +01:00
|
|
|
|
2020-07-28 21:59:27 +02:00
|
|
|
let projectId = process.env.METAMETRICS_PROJECT_ID
|
|
|
|
if (!projectId) {
|
|
|
|
projectId = inDevelopment ? 1 : 2
|
|
|
|
}
|
|
|
|
|
2019-03-05 16:45:01 +01:00
|
|
|
const METAMETRICS_BASE_URL = 'https://chromeextensionmm.innocraft.cloud/piwik.php'
|
2020-07-28 21:59:27 +02:00
|
|
|
const METAMETRICS_REQUIRED_PARAMS = `?idsite=${projectId}&rec=1&apiv=1`
|
2019-03-05 16:45:01 +01:00
|
|
|
const METAMETRICS_BASE_FULL = METAMETRICS_BASE_URL + METAMETRICS_REQUIRED_PARAMS
|
|
|
|
|
Use `pathname` instead of URL for `currentPath` metrics parameter (#9158)
The `currentPath` parameter passed to our metrics utility had been
passed the full URL rather than just the path, contrary to what the
name would imply. We only used the path portion, so passing the full
URL did lead to complications.
Now just the `pathname` is passed in, rather than the full URL. This
simplifies the metrics logic, and it incidentally fixes two bugs.
The main bug fixed is regarding Firefox metrics. Previously we had
assumed the `currentPath` would start with `chrome-extension://`, which
of course was not true on Firefox. This lead to us incorrectly parsing
the `currentPath`, so path tracking was broken for Firefox events.
This broken parsing is now bypassed entirely, so metrics should now
work the same on Firefox as on Chrome.
The second bug was that we were incorrectly setting the tracking URL
for background events during tests. As a result, we were incorrectly
detecting ourselves as an internal site that had referred the user to
us. But this was not of major concern, since it only affected test
metrics (which get sent to the development Matomo project).
Lastly, this change let us discard the `pathname` parameter used in
the `overrides` parameter of the `metricsEvent` function. Now that
`currentPath` is equivalent to `pathname`, the `pathname` parameter is
redundant.
2020-08-07 20:32:46 +02:00
|
|
|
const METAMETRICS_TRACKING_URL = inDevelopment
|
2019-03-05 16:45:01 +01:00
|
|
|
? 'http://www.metamask.io/metametrics'
|
|
|
|
: 'http://www.metamask.io/metametrics-prod'
|
|
|
|
|
2019-05-20 19:32:26 +02:00
|
|
|
/** ***************Custom variables*************** **/
|
2020-05-05 15:40:36 +02:00
|
|
|
// Custom variable declarations
|
2019-03-05 16:45:01 +01:00
|
|
|
const METAMETRICS_CUSTOM_GAS_LIMIT_CHANGE = 'gasLimitChange'
|
|
|
|
const METAMETRICS_CUSTOM_GAS_PRICE_CHANGE = 'gasPriceChange'
|
2019-03-05 19:14:02 +01:00
|
|
|
const METAMETRICS_CUSTOM_FUNCTION_TYPE = 'functionType'
|
2019-03-05 16:45:01 +01:00
|
|
|
const METAMETRICS_CUSTOM_RECIPIENT_KNOWN = 'recipientKnown'
|
2020-08-07 21:28:23 +02:00
|
|
|
const METAMETRICS_REQUEST_ORIGIN = 'origin'
|
2019-03-05 16:45:01 +01:00
|
|
|
const METAMETRICS_CUSTOM_FROM_NETWORK = 'fromNetwork'
|
|
|
|
const METAMETRICS_CUSTOM_TO_NETWORK = 'toNetwork'
|
|
|
|
const METAMETRICS_CUSTOM_ERROR_FIELD = 'errorField'
|
|
|
|
const METAMETRICS_CUSTOM_ERROR_MESSAGE = 'errorMessage'
|
|
|
|
const METAMETRICS_CUSTOM_RPC_NETWORK_ID = 'networkId'
|
|
|
|
const METAMETRICS_CUSTOM_RPC_CHAIN_ID = 'chainId'
|
2019-04-05 06:02:47 +02:00
|
|
|
const METAMETRICS_CUSTOM_GAS_CHANGED = 'gasChanged'
|
2019-05-20 19:32:26 +02:00
|
|
|
const METAMETRICS_CUSTOM_ASSET_SELECTED = 'assetSelected'
|
2019-03-05 16:45:01 +01:00
|
|
|
|
|
|
|
const customVariableNameIdMap = {
|
2019-03-05 19:14:02 +01:00
|
|
|
[METAMETRICS_CUSTOM_FUNCTION_TYPE]: 1,
|
|
|
|
[METAMETRICS_CUSTOM_RECIPIENT_KNOWN]: 2,
|
2020-08-07 21:28:23 +02:00
|
|
|
[METAMETRICS_REQUEST_ORIGIN]: 3,
|
2019-03-05 16:45:01 +01:00
|
|
|
[METAMETRICS_CUSTOM_GAS_LIMIT_CHANGE]: 4,
|
|
|
|
[METAMETRICS_CUSTOM_GAS_PRICE_CHANGE]: 5,
|
2019-05-20 19:32:26 +02:00
|
|
|
|
2019-03-05 19:14:02 +01:00
|
|
|
[METAMETRICS_CUSTOM_FROM_NETWORK]: 1,
|
|
|
|
[METAMETRICS_CUSTOM_TO_NETWORK]: 2,
|
2019-05-20 19:32:26 +02:00
|
|
|
|
2019-03-05 19:14:02 +01:00
|
|
|
[METAMETRICS_CUSTOM_RPC_NETWORK_ID]: 1,
|
|
|
|
[METAMETRICS_CUSTOM_RPC_CHAIN_ID]: 2,
|
2019-05-20 19:32:26 +02:00
|
|
|
|
|
|
|
[METAMETRICS_CUSTOM_ERROR_FIELD]: 3,
|
|
|
|
[METAMETRICS_CUSTOM_ERROR_MESSAGE]: 4,
|
|
|
|
|
2019-04-05 06:02:47 +02:00
|
|
|
[METAMETRICS_CUSTOM_GAS_CHANGED]: 1,
|
2019-05-20 19:32:26 +02:00
|
|
|
[METAMETRICS_CUSTOM_ASSET_SELECTED]: 2,
|
2019-03-05 16:45:01 +01:00
|
|
|
}
|
2019-05-20 19:32:26 +02:00
|
|
|
/** ********************************************************** **/
|
|
|
|
|
|
|
|
const METAMETRICS_CUSTOM_NETWORK = 'network'
|
|
|
|
const METAMETRICS_CUSTOM_ENVIRONMENT_TYPE = 'environmentType'
|
|
|
|
const METAMETRICS_CUSTOM_ACTIVE_CURRENCY = 'activeCurrency'
|
|
|
|
const METAMETRICS_CUSTOM_ACCOUNT_TYPE = 'accountType'
|
|
|
|
const METAMETRICS_CUSTOM_NUMBER_OF_ACCOUNTS = 'numberOfAccounts'
|
2020-05-13 15:12:07 +02:00
|
|
|
const METAMETRICS_CUSTOM_NUMBER_OF_TOKENS = 'numberOfTokens'
|
2020-05-05 15:40:36 +02:00
|
|
|
const METAMETRICS_CUSTOM_VERSION = 'version'
|
2019-03-05 16:45:01 +01:00
|
|
|
|
|
|
|
const customDimensionsNameIdMap = {
|
|
|
|
[METAMETRICS_CUSTOM_NETWORK]: 5,
|
|
|
|
[METAMETRICS_CUSTOM_ENVIRONMENT_TYPE]: 6,
|
|
|
|
[METAMETRICS_CUSTOM_ACTIVE_CURRENCY]: 7,
|
|
|
|
[METAMETRICS_CUSTOM_ACCOUNT_TYPE]: 8,
|
2020-05-13 15:12:07 +02:00
|
|
|
[METAMETRICS_CUSTOM_NUMBER_OF_ACCOUNTS]: 9,
|
|
|
|
[METAMETRICS_CUSTOM_NUMBER_OF_TOKENS]: 10,
|
2020-05-05 15:40:36 +02:00
|
|
|
[METAMETRICS_CUSTOM_VERSION]: 11,
|
2019-03-05 16:45:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function composeUrlRefParamAddition (previousPath, confirmTransactionOrigin) {
|
2019-10-29 17:14:41 +01:00
|
|
|
const externalOrigin = confirmTransactionOrigin && confirmTransactionOrigin !== 'metamask'
|
Use `pathname` instead of URL for `currentPath` metrics parameter (#9158)
The `currentPath` parameter passed to our metrics utility had been
passed the full URL rather than just the path, contrary to what the
name would imply. We only used the path portion, so passing the full
URL did lead to complications.
Now just the `pathname` is passed in, rather than the full URL. This
simplifies the metrics logic, and it incidentally fixes two bugs.
The main bug fixed is regarding Firefox metrics. Previously we had
assumed the `currentPath` would start with `chrome-extension://`, which
of course was not true on Firefox. This lead to us incorrectly parsing
the `currentPath`, so path tracking was broken for Firefox events.
This broken parsing is now bypassed entirely, so metrics should now
work the same on Firefox as on Chrome.
The second bug was that we were incorrectly setting the tracking URL
for background events during tests. As a result, we were incorrectly
detecting ourselves as an internal site that had referred the user to
us. But this was not of major concern, since it only affected test
metrics (which get sent to the development Matomo project).
Lastly, this change let us discard the `pathname` parameter used in
the `overrides` parameter of the `metricsEvent` function. Now that
`currentPath` is equivalent to `pathname`, the `pathname` parameter is
redundant.
2020-08-07 20:32:46 +02:00
|
|
|
return `&urlref=${externalOrigin ? 'EXTERNAL' : encodeURIComponent(`${METAMETRICS_TRACKING_URL}${previousPath}`)}`
|
2019-03-05 16:45:01 +01:00
|
|
|
}
|
|
|
|
|
2019-05-20 19:32:26 +02:00
|
|
|
// composes query params of the form &dimension[0-999]=[value]
|
2019-03-05 16:45:01 +01:00
|
|
|
function composeCustomDimensionParamAddition (customDimensions) {
|
|
|
|
const customDimensionParamStrings = Object.keys(customDimensions).reduce((acc, name) => {
|
|
|
|
return [...acc, `dimension${customDimensionsNameIdMap[name]}=${customDimensions[name]}`]
|
|
|
|
}, [])
|
|
|
|
return `&${customDimensionParamStrings.join('&')}`
|
|
|
|
}
|
|
|
|
|
2019-05-20 19:32:26 +02:00
|
|
|
// composes query params in form: &cvar={[id]:[[name],[value]]}
|
|
|
|
// Example: &cvar={"1":["OS","iphone 5.0"],"2":["Matomo Mobile Version","1.6.2"],"3":["Locale","en::en"],"4":["Num Accounts","2"]}
|
2019-03-05 16:45:01 +01:00
|
|
|
function composeCustomVarParamAddition (customVariables) {
|
|
|
|
const customVariableIdValuePairs = Object.keys(customVariables).reduce((acc, name) => {
|
|
|
|
return {
|
|
|
|
[customVariableNameIdMap[name]]: [name, customVariables[name]],
|
|
|
|
...acc,
|
|
|
|
}
|
|
|
|
}, {})
|
|
|
|
return `&cvar=${encodeURIComponent(JSON.stringify(customVariableIdValuePairs))}`
|
|
|
|
}
|
|
|
|
|
|
|
|
function composeParamAddition (paramValue, paramName) {
|
|
|
|
return paramValue !== 0 && !paramValue
|
|
|
|
? ''
|
|
|
|
: `&${paramName}=${paramValue}`
|
|
|
|
}
|
|
|
|
|
2019-05-20 19:32:26 +02:00
|
|
|
/**
|
|
|
|
* @name composeUrl
|
|
|
|
* @param {Object} config - configuration object for composing the metametrics url
|
|
|
|
* @property {object} config.eventOpts Object containing event category, action and name descriptors
|
|
|
|
* @property {object} config.customVariables Object containing custom properties with values relevant to a specific event
|
|
|
|
* @property {object} config.pageOpts Objects containing information about a page/route the event is dispatched from
|
|
|
|
* @property {number} config.network The selected network of the user when the event occurs
|
|
|
|
* @property {string} config.environmentType The "environment" the user is using the app from: 'popup', 'notification' or 'fullscreen'
|
|
|
|
* @property {string} config.activeCurrency The current the user has select as their primary currency at the time of the event
|
|
|
|
* @property {string} config.accountType The account type being used at the time of the event: 'hardware', 'imported' or 'default'
|
|
|
|
* @property {number} config.numberOfTokens The number of tokens that the user has added at the time of the event
|
|
|
|
* @property {number} config.numberOfAccounts The number of accounts the user has added at the time of the event
|
2020-08-10 17:10:30 +02:00
|
|
|
* @property {string} config.version The current version of the MetaMask extension
|
Use `pathname` instead of URL for `currentPath` metrics parameter (#9158)
The `currentPath` parameter passed to our metrics utility had been
passed the full URL rather than just the path, contrary to what the
name would imply. We only used the path portion, so passing the full
URL did lead to complications.
Now just the `pathname` is passed in, rather than the full URL. This
simplifies the metrics logic, and it incidentally fixes two bugs.
The main bug fixed is regarding Firefox metrics. Previously we had
assumed the `currentPath` would start with `chrome-extension://`, which
of course was not true on Firefox. This lead to us incorrectly parsing
the `currentPath`, so path tracking was broken for Firefox events.
This broken parsing is now bypassed entirely, so metrics should now
work the same on Firefox as on Chrome.
The second bug was that we were incorrectly setting the tracking URL
for background events during tests. As a result, we were incorrectly
detecting ourselves as an internal site that had referred the user to
us. But this was not of major concern, since it only affected test
metrics (which get sent to the development Matomo project).
Lastly, this change let us discard the `pathname` parameter used in
the `overrides` parameter of the `metricsEvent` function. Now that
`currentPath` is equivalent to `pathname`, the `pathname` parameter is
redundant.
2020-08-07 20:32:46 +02:00
|
|
|
* @property {string} config.previousPath The pathname of the URL the user was on prior to the URL they are on at the time of the event
|
|
|
|
* @property {string} config.currentPath The pathname of the URL the user is on at the time of the event
|
2019-05-20 19:32:26 +02:00
|
|
|
* @property {string} config.metaMetricsId A random id assigned to a user at the time of opting in to metametrics. A hexadecimal number
|
|
|
|
* @property {string} config.confirmTransactionOrigin The origin on a transaction
|
|
|
|
* @property {boolean} config.excludeMetaMetricsId Whether or not the tracked event data should be associated with a metametrics id
|
|
|
|
* @property {boolean} config.isNewVisit Whether or not the event should be tracked as a new visit/user sessions
|
2020-01-13 19:36:36 +01:00
|
|
|
* @returns {string} - Returns a url to be passed to fetch to make the appropriate request to matomo.
|
2019-05-20 19:32:26 +02:00
|
|
|
* Example: https://chromeextensionmm.innocraft.cloud/piwik.php?idsite=1&rec=1&apiv=1&e_c=Navigation&e_a=Home&e_n=Clicked%20Send:%20Eth&urlref=http%3A%2F%2Fwww.metamask.io%2Fmetametrics%2Fhome.html%23send&dimension5=3&dimension6=fullscreen&dimension7=ETH&dimension8=default&dimension9=0&dimension10=3&url=http%3A%2F%2Fwww.metamask.io%2Fmetametrics%2Fhome.html%23&_id=49c10aff19795e9a&rand=7906028754863992&pv_id=53acad&uid=49c1
|
|
|
|
*/
|
2019-05-08 21:51:33 +02:00
|
|
|
function composeUrl (config) {
|
2019-03-05 16:45:01 +01:00
|
|
|
const {
|
|
|
|
eventOpts = {},
|
|
|
|
customVariables = '',
|
|
|
|
pageOpts = '',
|
|
|
|
network,
|
|
|
|
environmentType,
|
|
|
|
activeCurrency,
|
|
|
|
accountType,
|
|
|
|
numberOfTokens,
|
|
|
|
numberOfAccounts,
|
2020-05-05 15:40:36 +02:00
|
|
|
version,
|
2019-03-05 16:45:01 +01:00
|
|
|
previousPath = '',
|
|
|
|
currentPath,
|
|
|
|
metaMetricsId,
|
|
|
|
confirmTransactionOrigin,
|
|
|
|
excludeMetaMetricsId,
|
|
|
|
isNewVisit,
|
|
|
|
} = config
|
|
|
|
const base = METAMETRICS_BASE_FULL
|
|
|
|
|
|
|
|
const e_c = composeParamAddition(eventOpts.category, 'e_c')
|
|
|
|
const e_a = composeParamAddition(eventOpts.action, 'e_a')
|
|
|
|
const e_n = composeParamAddition(eventOpts.name, 'e_n')
|
|
|
|
const new_visit = isNewVisit ? `&new_visit=1` : ''
|
|
|
|
|
2020-02-15 21:56:48 +01:00
|
|
|
const cvar = (customVariables && composeCustomVarParamAddition(customVariables)) || ''
|
2019-03-05 16:45:01 +01:00
|
|
|
|
|
|
|
const action_name = ''
|
|
|
|
|
|
|
|
const urlref = previousPath && composeUrlRefParamAddition(previousPath, confirmTransactionOrigin)
|
|
|
|
|
2020-08-14 13:47:43 +02:00
|
|
|
const dimensions = pageOpts.hideDimensions
|
|
|
|
? ''
|
|
|
|
: (
|
|
|
|
composeCustomDimensionParamAddition({
|
|
|
|
network,
|
|
|
|
environmentType,
|
|
|
|
activeCurrency,
|
|
|
|
accountType,
|
|
|
|
version,
|
|
|
|
numberOfTokens: (customVariables && customVariables.numberOfTokens) || numberOfTokens,
|
|
|
|
numberOfAccounts: (customVariables && customVariables.numberOfAccounts) || numberOfAccounts,
|
|
|
|
})
|
|
|
|
)
|
Use `pathname` instead of URL for `currentPath` metrics parameter (#9158)
The `currentPath` parameter passed to our metrics utility had been
passed the full URL rather than just the path, contrary to what the
name would imply. We only used the path portion, so passing the full
URL did lead to complications.
Now just the `pathname` is passed in, rather than the full URL. This
simplifies the metrics logic, and it incidentally fixes two bugs.
The main bug fixed is regarding Firefox metrics. Previously we had
assumed the `currentPath` would start with `chrome-extension://`, which
of course was not true on Firefox. This lead to us incorrectly parsing
the `currentPath`, so path tracking was broken for Firefox events.
This broken parsing is now bypassed entirely, so metrics should now
work the same on Firefox as on Chrome.
The second bug was that we were incorrectly setting the tracking URL
for background events during tests. As a result, we were incorrectly
detecting ourselves as an internal site that had referred the user to
us. But this was not of major concern, since it only affected test
metrics (which get sent to the development Matomo project).
Lastly, this change let us discard the `pathname` parameter used in
the `overrides` parameter of the `metricsEvent` function. Now that
`currentPath` is equivalent to `pathname`, the `pathname` parameter is
redundant.
2020-08-07 20:32:46 +02:00
|
|
|
const url = currentPath ? `&url=${encodeURIComponent(`${METAMETRICS_TRACKING_URL}${currentPath}`)}` : ''
|
2019-03-05 16:45:01 +01:00
|
|
|
const _id = metaMetricsId && !excludeMetaMetricsId ? `&_id=${metaMetricsId.slice(2, 18)}` : ''
|
|
|
|
const rand = `&rand=${String(Math.random()).slice(2)}`
|
Use `pathname` instead of URL for `currentPath` metrics parameter (#9158)
The `currentPath` parameter passed to our metrics utility had been
passed the full URL rather than just the path, contrary to what the
name would imply. We only used the path portion, so passing the full
URL did lead to complications.
Now just the `pathname` is passed in, rather than the full URL. This
simplifies the metrics logic, and it incidentally fixes two bugs.
The main bug fixed is regarding Firefox metrics. Previously we had
assumed the `currentPath` would start with `chrome-extension://`, which
of course was not true on Firefox. This lead to us incorrectly parsing
the `currentPath`, so path tracking was broken for Firefox events.
This broken parsing is now bypassed entirely, so metrics should now
work the same on Firefox as on Chrome.
The second bug was that we were incorrectly setting the tracking URL
for background events during tests. As a result, we were incorrectly
detecting ourselves as an internal site that had referred the user to
us. But this was not of major concern, since it only affected test
metrics (which get sent to the development Matomo project).
Lastly, this change let us discard the `pathname` parameter used in
the `overrides` parameter of the `metricsEvent` function. Now that
`currentPath` is equivalent to `pathname`, the `pathname` parameter is
redundant.
2020-08-07 20:32:46 +02:00
|
|
|
const pv_id = currentPath ? `&pv_id=${ethUtil.bufferToHex(ethUtil.sha3(currentPath)).slice(2, 8)}` : ''
|
2020-08-13 22:00:09 +02:00
|
|
|
|
|
|
|
let uid = ''
|
|
|
|
if (excludeMetaMetricsId) {
|
|
|
|
uid = '&uid=0000000000000000'
|
|
|
|
} else if (metaMetricsId) {
|
|
|
|
uid = `&uid=${metaMetricsId.slice(2, 18)}`
|
|
|
|
}
|
2019-03-05 16:45:01 +01:00
|
|
|
|
|
|
|
return [ base, e_c, e_a, e_n, cvar, action_name, urlref, dimensions, url, _id, rand, pv_id, uid, new_visit ].join('')
|
|
|
|
}
|
|
|
|
|
2019-12-06 17:14:50 +01:00
|
|
|
export function sendMetaMetricsEvent (config) {
|
2020-04-15 19:23:27 +02:00
|
|
|
return window.fetch(composeUrl(config), {
|
2019-03-05 16:45:01 +01:00
|
|
|
'headers': {},
|
|
|
|
'method': 'GET',
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
export function verifyUserPermission (config, props) {
|
|
|
|
const {
|
|
|
|
eventOpts = {},
|
|
|
|
} = config
|
|
|
|
const { userPermissionPreferences } = props
|
|
|
|
const {
|
|
|
|
allowAll,
|
|
|
|
allowNone,
|
|
|
|
allowSendMetrics,
|
|
|
|
} = userPermissionPreferences
|
|
|
|
|
|
|
|
if (allowNone) {
|
|
|
|
return false
|
|
|
|
} else if (allowAll) {
|
|
|
|
return true
|
|
|
|
} else if (allowSendMetrics && eventOpts.name === 'send') {
|
|
|
|
return true
|
|
|
|
} else {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const trackableSendCounts = {
|
|
|
|
1: true,
|
|
|
|
10: true,
|
|
|
|
30: true,
|
|
|
|
50: true,
|
|
|
|
100: true,
|
|
|
|
250: true,
|
|
|
|
500: true,
|
|
|
|
1000: true,
|
|
|
|
2500: true,
|
|
|
|
5000: true,
|
|
|
|
10000: true,
|
|
|
|
25000: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
export function sendCountIsTrackable (sendCount) {
|
|
|
|
return Boolean(trackableSendCounts[sendCount])
|
|
|
|
}
|