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(metrics): ✨ Adding patched analytics. #1232

Merged
merged 10 commits into from
Jul 19, 2023
3 changes: 3 additions & 0 deletions add-on/src/lib/ipfs-companion.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { guiURLString, migrateOptions, optionDefaults, safeURL, storeMissingOpti
import { getExtraInfoSpec } from './redirect-handler/blockOrObserve.js'
import createRuntimeChecks from './runtime-checks.js'
import { initState, offlinePeerCount } from './state.js'
import { handleConsentFromState, trackView } from '../lib/telemetry.js'

// this won't work in webworker context. Needs to be enabled manually
// https://github.com/debug-js/debug/issues/916
Expand Down Expand Up @@ -65,7 +66,9 @@ export default async function init (inQuickImport = false) {
if (state.active) {
// It's ok for this to fail, node might be unavailable or mis-configured
try {
handleConsentFromState(state)
ipfs = await initIpfsClient(browser, state, inQuickImport)
trackView('init')
} catch (err) {
console.error('[ipfs-companion] Failed to init IPFS client', err)
notify(
Expand Down
40 changes: 40 additions & 0 deletions add-on/src/lib/storage-provider/WebExtensionStorageProvider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import type { consentTypes } from '@ipfs-shipyard/ignite-metrics/typings/countly'
import type { StorageProviderInterface } from '@ipfs-shipyard/ignite-metrics/StorageProvider'
import browser from 'webextension-polyfill'

export class WebExtensionStorageProvider implements StorageProviderInterface {
async setStore (consentArray: consentTypes[]): Promise<void> {
try {
const jsonString = JSON.stringify(consentArray)
if ('localStorage' in globalThis) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wont this always be false?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

firefox will make this true :-( in their event listener pages.

globalThis.localStorage.setItem('@ipfs-shipyard/ignite-metrics:consent', jsonString)
} else {
await browser.storage.local.set({ '@ipfs-shipyard/ignite-metrics:consent': jsonString })
}
} catch (err) {
// eslint-disable-next-line no-console
console.error(err)
}
}

async getStore (): Promise<consentTypes[]> {
try {
let jsonString
if ('localStorage' in globalThis) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto

jsonString = globalThis.localStorage.getItem('@ipfs-shipyard/ignite-metrics:consent')
} else {
jsonString = (await browser.storage.local.get(['@ipfs-shipyard/ignite-metrics:consent']))['@ipfs-shipyard/ignite-metrics:consent']
}
if (jsonString != null) {
return JSON.parse(jsonString)
}
} catch (err) {
// eslint-disable-next-line no-console
console.error(err)
}
/**
* Return minimal consent if there is nothing in the store.
*/
return ['minimal']
}
}
8 changes: 6 additions & 2 deletions add-on/src/lib/telemetry.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import browser from 'webextension-polyfill'
import MetricsProvider from '@ipfs-shipyard/ignite-metrics/vanilla'
import MetricsProvider from '@ipfs-shipyard/ignite-metrics/MetricsProvider'
import PatchedCountly from 'countly-sdk-web'
import debug from 'debug'
import { WebExtensionStorageProvider } from './storage-provider/WebExtensionStorageProvider.js'

const log = debug('ipfs-companion:telemetry')

const metricsProvider = new MetricsProvider({
appKey: '393f72eb264c28a1b59973da1e0a3938d60dc38a',
autoTrack: false,
storageProvider: null
metricsService: PatchedCountly,
storage: 'none',
storageProvider: new WebExtensionStorageProvider()
})

/**
Expand Down
Loading