1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-23 18:41:38 +01:00
metamask-extension/app/scripts/lib/rpc-method-middleware/handlers/log-web3-usage.js
Erik Marks 7870d6d194
Use Segment for web3 usage metrics; fix background Segment bugs (#9525)
* Use segment for window.web3 usage metrics
* Add Segment referrer information
* Fix track segment event bug
* Fix missing category bug in tx segment event

Co-authored-by: Mark Stacey <markjstacey@gmail.com>
2020-10-08 11:00:56 -07:00

60 lines
1.7 KiB
JavaScript

import { MESSAGE_TYPE } from '../../enums'
/**
* This RPC method is called by our inpage web3 proxy whenever window.web3 is
* accessed. We're collecting data on window.web3 usage so that we can warn
* website maintainers, and possibly our users, before we remove window.web3
* by November 16, 2020.
*/
const logWeb3Usage = {
methodName: MESSAGE_TYPE.LOG_WEB3_USAGE,
implementation: logWeb3UsageHandler,
}
export default logWeb3Usage
const recordedWeb3Usage = {}
/**
* @typedef {Object} LogWeb3UsageOptions
* @property {string} origin - The origin of the request.
* @property {Function} sendMetrics - A function that registers a metrics event.
*/
/**
* @typedef {Object} LogWeb3UsageParam
* @property {string} action - The action taken (get or set).
* @property {string} name - The window.web3 property name subject to the action.
*/
/**
* @param {import('json-rpc-engine').JsonRpcRequest<[LogWeb3UsageParam]>} req - The JSON-RPC request object.
* @param {import('json-rpc-engine').JsonRpcResponse<true>} res - The JSON-RPC response object.
* @param {Function} _next - The json-rpc-engine 'next' callback.
* @param {Function} end - The json-rpc-engine 'end' callback.
* @param {LogWeb3UsageOptions} options
*/
function logWeb3UsageHandler (
req, res, _next, end,
{ origin, sendMetrics },
) {
const { action, name } = req.params[0]
if (!recordedWeb3Usage[origin]) {
recordedWeb3Usage[origin] = {}
}
if (!recordedWeb3Usage[origin][name]) {
recordedWeb3Usage[origin][name] = true
sendMetrics({
event: `Website Accessed window.web3`,
category: 'inpage_provider',
properties: { action, web3Property: name },
referrerUrl: origin,
})
}
res.result = true
return end()
}