Skip to content

Commit 98622b7

Browse files
authored
feat(telemetry): Refactor Metrics Tracking from background service_worker (#1172)
* feat(telemetry): ♻️ Init Telemetry away from background service_worker. * feat(telemetry): ♻️ Track metrics from page context instead of service_worker context
1 parent 2de3028 commit 98622b7

File tree

8 files changed

+21
-25
lines changed

8 files changed

+21
-25
lines changed

add-on/src/background/background.js

-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ browser.runtime.setUninstallURL(getUninstallURL(browser))
1313

1414
// init add-on after all libs are loaded
1515
document.addEventListener('DOMContentLoaded', async () => {
16-
browser.runtime.sendMessage({ telemetry: { trackView: 'background' } })
1716
// setting debug namespaces require page reload to get applied
1817
const debugNs = (await browser.storage.local.get({ logNamespaces: optionDefaults.logNamespaces })).logNamespaces
1918
if (debugNs !== localStorage.debug) {

add-on/src/landing-pages/welcome/store.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict'
22
/* eslint-env browser, webextensions */
33
import browser from 'webextension-polyfill'
4+
import { handleConsentFromState, trackView } from '../../lib/telemetry.js'
45

56
export default function createWelcomePageStore (i18n, runtime) {
67
return function welcomePageStore (state, emitter) {
@@ -9,7 +10,8 @@ export default function createWelcomePageStore (i18n, runtime) {
910
state.webuiRootUrl = null
1011
let port
1112
emitter.on('DOMContentLoaded', async () => {
12-
browser.runtime.sendMessage({ telemetry: { trackView: 'welcome' } })
13+
handleConsentFromState(state)
14+
trackView('welcome')
1315
emitter.emit('render')
1416
port = runtime.connect({ name: 'browser-action-port' })
1517
port.onMessage.addListener(async (message) => {

add-on/src/lib/ipfs-companion.js

-18
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import createRuntimeChecks from './runtime-checks.js'
2323
import { createContextMenus, findValueForContext, contextMenuCopyAddressAtPublicGw, contextMenuCopyRawCid, contextMenuCopyCanonicalAddress, contextMenuViewOnGateway, contextMenuCopyPermalink, contextMenuCopyCidAddress } from './context-menus.js'
2424
import { registerSubdomainProxy } from './http-proxy.js'
2525
import { runPendingOnInstallTasks } from './on-installed.js'
26-
import { handleConsentFromState, startSession, endSession, trackView } from './telemetry.js'
2726
const log = debug('ipfs-companion:main')
2827
log.error = debug('ipfs-companion:main:error')
2928

@@ -57,11 +56,8 @@ export default async function init () {
5756
runtime = await createRuntimeChecks(browser)
5857
state = initState(options)
5958
notify = createNotifier(getState)
60-
// ensure consent is set properly on app init
61-
handleConsentFromState(state)
6259

6360
if (state.active) {
64-
startSession()
6561
// It's ok for this to fail, node might be unavailable or mis-configured
6662
try {
6763
ipfs = await initIpfsClient(browser, state)
@@ -172,16 +168,6 @@ export default async function init () {
172168
const result = validIpfsOrIpns(path) ? resolveToPublicUrl(path) : null
173169
return Promise.resolve({ pubGwUrlForIpfsOrIpnsPath: result })
174170
}
175-
if (request.telemetry) {
176-
return Promise.resolve(onTelemetryMessage(request.telemetry, sender))
177-
}
178-
}
179-
180-
function onTelemetryMessage (request, sender) {
181-
if (request.trackView) {
182-
const { version } = browser.runtime.getManifest()
183-
return trackView(request.trackView, { version })
184-
}
185171
}
186172

187173
// PORTS (connection-based messaging)
@@ -568,8 +554,6 @@ export default async function init () {
568554
await registerSubdomainProxy(getState, runtime)
569555
shouldRestartIpfsClient = true
570556
shouldStopIpfsClient = !state.active
571-
// Any time the extension switches active state, start or stop the current session.
572-
state.active ? startSession() : endSession()
573557
break
574558
case 'ipfsNodeType':
575559
if (change.oldValue !== braveNodeType && change.newValue === braveNodeType) {
@@ -636,8 +620,6 @@ export default async function init () {
636620
break
637621
}
638622
}
639-
// ensure consent is set properly on state changes
640-
handleConsentFromState(state)
641623

642624
if ((state.active && shouldRestartIpfsClient) || shouldStopIpfsClient) {
643625
try {

add-on/src/lib/telemetry.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import browser from 'webextension-polyfill'
12
import MetricsProvider from '@ipfs-shipyard/ignite-metrics/vanilla'
23
import debug from 'debug'
34

@@ -36,7 +37,8 @@ export function handleConsentFromState (state) {
3637
const ignoredViewsRegex = []
3738
export function trackView (view, segments) {
3839
log('trackView called for view: ', view)
39-
metricsProvider.trackView(view, ignoredViewsRegex, segments)
40+
const { version } = browser.runtime.getManifest()
41+
metricsProvider.trackView(view, ignoredViewsRegex, { ...segments, version })
4042
}
4143

4244
export const startSession = (...args) => metricsProvider.startSession(...args)

add-on/src/options/store.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import browser from 'webextension-polyfill'
55
import { optionDefaults } from '../lib/options.js'
66
import createRuntimeChecks from '../lib/runtime-checks.js'
7+
import { handleConsentFromState, trackView } from '../lib/telemetry.js'
78

89
// The store contains and mutates the state for the app
910
export default function optionStore (state, emitter) {
@@ -20,7 +21,8 @@ export default function optionStore (state, emitter) {
2021
}
2122

2223
emitter.on('DOMContentLoaded', async () => {
23-
browser.runtime.sendMessage({ telemetry: { trackView: 'options' } })
24+
handleConsentFromState(state)
25+
trackView('options')
2426
updateStateOptions()
2527
browser.storage.onChanged.addListener(updateStateOptions)
2628
})

add-on/src/popup/browser-action/store.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { browserActionFilesCpImportCurrentTab } from '../../lib/ipfs-import.js'
77
import { ipfsContentPath } from '../../lib/ipfs-path.js'
88
import { welcomePage, optionsPage } from '../../lib/constants.js'
99
import { contextMenuViewOnGateway, contextMenuCopyAddressAtPublicGw, contextMenuCopyPermalink, contextMenuCopyRawCid, contextMenuCopyCanonicalAddress, contextMenuCopyCidAddress } from '../../lib/context-menus.js'
10+
import { endSession, handleConsentFromState, startSession, trackView } from '../../lib/telemetry.js'
1011

1112
// The store contains and mutates the state for the app
1213
export default (state, emitter) => {
@@ -38,7 +39,8 @@ export default (state, emitter) => {
3839
let port
3940

4041
emitter.on('DOMContentLoaded', async () => {
41-
browser.runtime.sendMessage({ telemetry: { trackView: 'browser-action' } })
42+
handleConsentFromState(state)
43+
trackView('browser-action')
4244

4345
// initial render with status stub
4446
emitter.emit('render')
@@ -205,6 +207,7 @@ export default (state, emitter) => {
205207
const prev = state.active
206208
state.active = !prev
207209
if (!state.active) {
210+
endSession()
208211
state.gatewayAddress = state.pubGwURLString
209212
state.ipfsApiUrl = null
210213
state.gatewayVersion = null
@@ -213,6 +216,8 @@ export default (state, emitter) => {
213216
}
214217
try {
215218
await browser.storage.local.set({ active: state.active })
219+
startSession()
220+
handleConsentFromState(state)
216221
} catch (error) {
217222
console.error(`Unable to update global Active flag due to ${error}`)
218223
state.active = prev

add-on/src/popup/quick-import.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { formatImportDirectory } from '../lib/ipfs-import.js'
1212
import all from 'it-all'
1313
import drop from 'drag-and-drop-files'
1414
import { filesize } from 'filesize'
15+
import { handleConsentFromState, trackView } from '../lib/telemetry.js'
1516

1617
document.title = browser.i18n.getMessage('quickImport_page_title')
1718

@@ -48,7 +49,8 @@ function quickImportStore (state, emitter) {
4849
let port
4950

5051
emitter.on('DOMContentLoaded', async () => {
51-
browser.runtime.sendMessage({ telemetry: { trackView: 'quick-import' } })
52+
handleConsentFromState(state)
53+
trackView('quick-import')
5254
// initialize connection to the background script which will trigger UI updates
5355
port = browser.runtime.connect({ name: 'browser-action-port' })
5456
port.onMessage.addListener(async (message) => {

add-on/src/recovery/recovery.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import browser, { i18n, runtime } from 'webextension-polyfill'
77
import { nodeOffSvg } from '../landing-pages/welcome/page.js'
88
import createWelcomePageStore from '../landing-pages/welcome/store.js'
99
import { optionsPage } from '../lib/constants.js'
10+
import { handleConsentFromState, trackView } from '../lib/telemetry.js'
1011
import './recovery.css'
1112

1213
const app = choo()
@@ -19,7 +20,8 @@ const optionsPageLink = html`<a class="navy link underline-under hover-aqua" id=
1920
app.use(createWelcomePageStore(i18n, runtime))
2021
// Register our single route
2122
app.route('*', (state) => {
22-
browser.runtime.sendMessage({ telemetry: { trackView: 'recovery' } })
23+
handleConsentFromState(state)
24+
trackView('recovery')
2325
const { hash } = window.location
2426
const { href: publicURI } = new URL(decodeURIComponent(hash.slice(1)))
2527

0 commit comments

Comments
 (0)