From 7870d6d194d600d131d0b8172f7314888faa3dd5 Mon Sep 17 00:00:00 2001 From: Erik Marks <25517051+rekmarks@users.noreply.github.com> Date: Thu, 8 Oct 2020 11:00:56 -0700 Subject: [PATCH] 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 --- app/scripts/controllers/transactions/index.js | 1 + .../lib/rpc-method-middleware/handlers/log-web3-usage.js | 8 +++++--- app/scripts/lib/segment.js | 8 ++++++++ app/scripts/lib/setupWeb3.js | 4 ++-- app/scripts/metamask-controller.js | 4 ++-- 5 files changed, 18 insertions(+), 7 deletions(-) diff --git a/app/scripts/controllers/transactions/index.js b/app/scripts/controllers/transactions/index.js index 5b53bebd2..b259fd6bf 100644 --- a/app/scripts/controllers/transactions/index.js +++ b/app/scripts/controllers/transactions/index.js @@ -861,6 +861,7 @@ export default class TransactionController extends EventEmitter { this._trackSegmentEvent({ event: 'Swap Completed', + category: 'swaps', properties: { ...txMeta.swapMetaData, token_to_amount_received: tokensReceived, diff --git a/app/scripts/lib/rpc-method-middleware/handlers/log-web3-usage.js b/app/scripts/lib/rpc-method-middleware/handlers/log-web3-usage.js index dbdb2ca6b..e493aeffe 100644 --- a/app/scripts/lib/rpc-method-middleware/handlers/log-web3-usage.js +++ b/app/scripts/lib/rpc-method-middleware/handlers/log-web3-usage.js @@ -45,10 +45,12 @@ function logWeb3UsageHandler ( } if (!recordedWeb3Usage[origin][name]) { recordedWeb3Usage[origin][name] = true + sendMetrics({ - action, - name, - customVariables: { origin }, + event: `Website Accessed window.web3`, + category: 'inpage_provider', + properties: { action, web3Property: name }, + referrerUrl: origin, }) } diff --git a/app/scripts/lib/segment.js b/app/scripts/lib/segment.js index 6b0c90f11..7093b0ac1 100644 --- a/app/scripts/lib/segment.js +++ b/app/scripts/lib/segment.js @@ -64,6 +64,7 @@ export function getTrackSegmentEvent ( * @param {string} event - The event name. * @param {string} category - The event category. * @param {Object} [properties] - The event properties. + * @param {string} [referrerUrl] - The event's referrer URL, if relevant. * @param {boolean} [excludeMetaMetricsId] - `true` if the user's MetaMetrics id should * not be included, and `false` otherwise. Default: `true` */ @@ -72,6 +73,7 @@ export function getTrackSegmentEvent ( category, properties = {}, excludeMetaMetricsId = true, + referrerUrl, }) { if (!event || !category) { throw new Error('Must specify event and category.') @@ -99,6 +101,12 @@ export function getTrackSegmentEvent ( trackOptions.userId = metaMetricsId } + if (referrerUrl) { + trackOptions.context.referrer = { + url: referrerUrl, + } + } + segment.track(trackOptions) } } diff --git a/app/scripts/lib/setupWeb3.js b/app/scripts/lib/setupWeb3.js index 493ac2767..ad86751e1 100644 --- a/app/scripts/lib/setupWeb3.js +++ b/app/scripts/lib/setupWeb3.js @@ -47,7 +47,7 @@ export default function setupWeb3 (log) { const name = stringifyKey(key) window.ethereum.request({ method: 'metamask_logInjectedWeb3Usage', - params: [{ action: 'window.web3 get', name }], + params: [{ action: 'get', name }], }) } @@ -59,7 +59,7 @@ export default function setupWeb3 (log) { if (shouldLogUsage) { window.ethereum.request({ method: 'metamask_logInjectedWeb3Usage', - params: [{ action: 'window.web3 set', name }], + params: [{ action: 'set', name }], }) } diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index e50586938..22b326c7d 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -123,7 +123,7 @@ export default class MetamaskController extends EventEmitter { const { currentLocale, metaMetricsId, - } = this.preferencesController.getState() + } = this.preferencesController.store.getState() return { currentLocale, metaMetricsId } }, ) @@ -1672,7 +1672,7 @@ export default class MetamaskController extends EventEmitter { })) engine.push(createMethodMiddleware({ origin, - sendMetrics: this.sendBackgroundMetaMetrics.bind(this), + sendMetrics: this.trackSegmentEvent, })) // filter and subscription polyfills engine.push(filterMiddleware)