Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(telemetry): Refactor Metrics Tracking from background service_worker #1172

Merged
merged 2 commits into from
Mar 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion add-on/src/background/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ browser.runtime.setUninstallURL(getUninstallURL(browser))

// init add-on after all libs are loaded
document.addEventListener('DOMContentLoaded', async () => {
browser.runtime.sendMessage({ telemetry: { trackView: 'background' } })
// setting debug namespaces require page reload to get applied
const debugNs = (await browser.storage.local.get({ logNamespaces: optionDefaults.logNamespaces })).logNamespaces
if (debugNs !== localStorage.debug) {
Expand Down
4 changes: 3 additions & 1 deletion add-on/src/landing-pages/welcome/store.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict'
/* eslint-env browser, webextensions */
import browser from 'webextension-polyfill'
import { handleConsentFromState, trackView } from '../../lib/telemetry.js'

export default function createWelcomePageStore (i18n, runtime) {
return function welcomePageStore (state, emitter) {
Expand All @@ -9,7 +10,8 @@ export default function createWelcomePageStore (i18n, runtime) {
state.webuiRootUrl = null
let port
emitter.on('DOMContentLoaded', async () => {
browser.runtime.sendMessage({ telemetry: { trackView: 'welcome' } })
handleConsentFromState(state)
trackView('welcome')
emitter.emit('render')
port = runtime.connect({ name: 'browser-action-port' })
port.onMessage.addListener(async (message) => {
Expand Down
18 changes: 0 additions & 18 deletions add-on/src/lib/ipfs-companion.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import createRuntimeChecks from './runtime-checks.js'
import { createContextMenus, findValueForContext, contextMenuCopyAddressAtPublicGw, contextMenuCopyRawCid, contextMenuCopyCanonicalAddress, contextMenuViewOnGateway, contextMenuCopyPermalink, contextMenuCopyCidAddress } from './context-menus.js'
import { registerSubdomainProxy } from './http-proxy.js'
import { runPendingOnInstallTasks } from './on-installed.js'
import { handleConsentFromState, startSession, endSession, trackView } from './telemetry.js'
const log = debug('ipfs-companion:main')
log.error = debug('ipfs-companion:main:error')

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

if (state.active) {
startSession()
// It's ok for this to fail, node might be unavailable or mis-configured
try {
ipfs = await initIpfsClient(browser, state)
Expand Down Expand Up @@ -172,16 +168,6 @@ export default async function init () {
const result = validIpfsOrIpns(path) ? resolveToPublicUrl(path) : null
return Promise.resolve({ pubGwUrlForIpfsOrIpnsPath: result })
}
if (request.telemetry) {
return Promise.resolve(onTelemetryMessage(request.telemetry, sender))
}
}

function onTelemetryMessage (request, sender) {
if (request.trackView) {
const { version } = browser.runtime.getManifest()
return trackView(request.trackView, { version })
}
}

// PORTS (connection-based messaging)
Expand Down Expand Up @@ -568,8 +554,6 @@ export default async function init () {
await registerSubdomainProxy(getState, runtime)
shouldRestartIpfsClient = true
shouldStopIpfsClient = !state.active
// Any time the extension switches active state, start or stop the current session.
state.active ? startSession() : endSession()
break
case 'ipfsNodeType':
if (change.oldValue !== braveNodeType && change.newValue === braveNodeType) {
Expand Down Expand Up @@ -636,8 +620,6 @@ export default async function init () {
break
}
}
// ensure consent is set properly on state changes
handleConsentFromState(state)

if ((state.active && shouldRestartIpfsClient) || shouldStopIpfsClient) {
try {
Expand Down
4 changes: 3 additions & 1 deletion add-on/src/lib/telemetry.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import browser from 'webextension-polyfill'
import MetricsProvider from '@ipfs-shipyard/ignite-metrics/vanilla'
import debug from 'debug'

Expand Down Expand Up @@ -36,7 +37,8 @@ export function handleConsentFromState (state) {
const ignoredViewsRegex = []
export function trackView (view, segments) {
log('trackView called for view: ', view)
metricsProvider.trackView(view, ignoredViewsRegex, segments)
const { version } = browser.runtime.getManifest()
metricsProvider.trackView(view, ignoredViewsRegex, { ...segments, version })
}

export const startSession = (...args) => metricsProvider.startSession(...args)
Expand Down
4 changes: 3 additions & 1 deletion add-on/src/options/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import browser from 'webextension-polyfill'
import { optionDefaults } from '../lib/options.js'
import createRuntimeChecks from '../lib/runtime-checks.js'
import { handleConsentFromState, trackView } from '../lib/telemetry.js'

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

emitter.on('DOMContentLoaded', async () => {
browser.runtime.sendMessage({ telemetry: { trackView: 'options' } })
handleConsentFromState(state)
trackView('options')
updateStateOptions()
browser.storage.onChanged.addListener(updateStateOptions)
})
Expand Down
7 changes: 6 additions & 1 deletion add-on/src/popup/browser-action/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { browserActionFilesCpImportCurrentTab } from '../../lib/ipfs-import.js'
import { ipfsContentPath } from '../../lib/ipfs-path.js'
import { welcomePage, optionsPage } from '../../lib/constants.js'
import { contextMenuViewOnGateway, contextMenuCopyAddressAtPublicGw, contextMenuCopyPermalink, contextMenuCopyRawCid, contextMenuCopyCanonicalAddress, contextMenuCopyCidAddress } from '../../lib/context-menus.js'
import { endSession, handleConsentFromState, startSession, trackView } from '../../lib/telemetry.js'

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

emitter.on('DOMContentLoaded', async () => {
browser.runtime.sendMessage({ telemetry: { trackView: 'browser-action' } })
handleConsentFromState(state)
trackView('browser-action')

// initial render with status stub
emitter.emit('render')
Expand Down Expand Up @@ -205,6 +207,7 @@ export default (state, emitter) => {
const prev = state.active
state.active = !prev
if (!state.active) {
endSession()
state.gatewayAddress = state.pubGwURLString
state.ipfsApiUrl = null
state.gatewayVersion = null
Expand All @@ -213,6 +216,8 @@ export default (state, emitter) => {
}
try {
await browser.storage.local.set({ active: state.active })
startSession()
handleConsentFromState(state)
} catch (error) {
console.error(`Unable to update global Active flag due to ${error}`)
state.active = prev
Expand Down
4 changes: 3 additions & 1 deletion add-on/src/popup/quick-import.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { formatImportDirectory } from '../lib/ipfs-import.js'
import all from 'it-all'
import drop from 'drag-and-drop-files'
import { filesize } from 'filesize'
import { handleConsentFromState, trackView } from '../lib/telemetry.js'

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

Expand Down Expand Up @@ -48,7 +49,8 @@ function quickImportStore (state, emitter) {
let port

emitter.on('DOMContentLoaded', async () => {
browser.runtime.sendMessage({ telemetry: { trackView: 'quick-import' } })
handleConsentFromState(state)
trackView('quick-import')
// initialize connection to the background script which will trigger UI updates
port = browser.runtime.connect({ name: 'browser-action-port' })
port.onMessage.addListener(async (message) => {
Expand Down
4 changes: 3 additions & 1 deletion add-on/src/recovery/recovery.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import browser, { i18n, runtime } from 'webextension-polyfill'
import { nodeOffSvg } from '../landing-pages/welcome/page.js'
import createWelcomePageStore from '../landing-pages/welcome/store.js'
import { optionsPage } from '../lib/constants.js'
import { handleConsentFromState, trackView } from '../lib/telemetry.js'
import './recovery.css'

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

Expand Down