Skip to content

Commit

Permalink
feat: Add option to disable stats collecting (#585)
Browse files Browse the repository at this point in the history
  • Loading branch information
tarampampam authored May 20, 2024
1 parent 874cb97 commit b74bad5
Show file tree
Hide file tree
Showing 7 changed files with 191 additions and 38 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ jobs:
tag: ${{ github.ref }}

chrome:
name: Publish on the Chrome Web Store
name: Publish on Chrome Web Store
runs-on: ubuntu-latest
needs: [build]
steps:
Expand Down
39 changes: 29 additions & 10 deletions src/entrypoints/background/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ import { setExtensionIcon, setExtensionTitle } from './ui'
const debug = (msg: string, ...args: Array<unknown>): void => console.debug(`%c😈 ${msg}`, 'font-weight:bold', ...args)
/** Convert milliseconds to seconds */
const m2s = (millis: number): number => Math.round(millis / 1000)

// stats collector is used to collect statistics about the extension usage. personal information is not collected
// and will never be collected. the collected data is used to overview the extension usage and track the most popular
// features
let stats: StatsCollector | undefined = undefined

// register the error handler to catch global errors (unhandled exceptions) and unhandled promise rejections to
Expand Down Expand Up @@ -81,14 +85,20 @@ let stats: StatsCollector | undefined = undefined
const latestBrowserVersions = new LatestBrowserVersions(new StorageArea('latest-browser-versions', 'local'))
debug('initial latest browser versions', ...(await latestBrowserVersions.get()))

// stats collector is used to collect statistics about the extension usage. personal information is not collected
// and will never be collected. the collected data is used to overview the extension usage and track the most popular
// features
stats = new GaCollector(uuid, {
extVersion: chrome.runtime.getManifest().version,
osFamily: hostOS,
browser: detectBrowser(),
})
// creates a new stats collector instance with the given UUID (or unset if the UUID is not provided)
const newStatsCollector = (uuid: string) => {
return new GaCollector(uuid, {
extVersion: chrome.runtime.getManifest().version,
osFamily: hostOS,
browser: detectBrowser(),
})
}

if (initSettings.stats.enabled) {
stats = newStatsCollector(uuid) // initialize the stats collector on startup
} else {
debug('collection of usage statistics is disabled')
}

// handlers is a map of functions that can be called from the popup or content scripts (and not only from them).
// think about them as a kind of API for the extension
Expand Down Expand Up @@ -205,7 +215,16 @@ let stats: StatsCollector | undefined = undefined
debug('all features have been disabled')
}

// update the remote user-agents list URI on changes, if needed
// 🚀 re-initialize the stats collector on changes, if needed
if (s.stats.enabled && !stats) {
stats = newStatsCollector(uuid)
debug('collection of usage statistics is enabled')
} else if (!s.stats.enabled) {
stats = undefined
debug('collection of usage statistics is disabled')
}

// 🚀 update the remote user-agents list URI on changes, if needed
if (remoteUserAgentList.url?.toString() !== s.remoteUseragentList.uri) {
if (remoteUserAgentList.setUrl(s.remoteUseragentList.uri)) {
// note: we do not await the result because we do not want to block the execution
Expand Down Expand Up @@ -266,7 +285,7 @@ let stats: StatsCollector | undefined = undefined
listenRuntime(handlers)

// send an initial event about the extension loading
stats.send(newExtensionLoadedEvent(initSettings)).then((err) => err && debug('stats error', err))
stats?.send(newExtensionLoadedEvent(initSettings)).then((err) => err && debug('stats error', err))
})().catch((error: unknown): void => {
if (stats) {
stats.send(newErrorEvent(error, { page: 'background' })).then((err) => err && debug('stats error', err))
Expand Down
3 changes: 3 additions & 0 deletions src/entrypoints/background/persistent/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ export default class {
mode: 'blacklist',
domains: ['localhost', '127.0.0.1'],
},
stats: {
enabled: true,
},
}

/** A list of change listeners */
Expand Down
19 changes: 19 additions & 0 deletions src/entrypoints/options/pages/general/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export default function General(): React.JSX.Element {
const [remoteUAUpdIntervalSec, setRemoteUAUpdIntervalSec] = useState<number>()
const [remoteListUpdateBtnEnabled, setRemoteListUpdateBtnEnabled] = useState<boolean>(true)
const [remoteListUpdateStatus, setRemoteListUpdateStatus] = useState<string>()
const [statsEnabled, setStatsEnabled, setStatsEnabledId] = [...useState<boolean>(false), useId()]

// on component mount
useEffect(() => {
Expand All @@ -60,6 +61,7 @@ export default function General(): React.JSX.Element {
setRemoteUAListEnabled(settings.remoteUseragentList.enabled)
setRemoteUAListUrl(settings.remoteUseragentList.uri)
setRemoteUAUpdIntervalSec(Math.round(settings.remoteUseragentList.updateIntervalMillis / 1000))
setStatsEnabled(settings.stats.enabled)
})
}, []) // eslint-disable-line react-hooks/exhaustive-deps

Expand Down Expand Up @@ -254,6 +256,23 @@ export default function General(): React.JSX.Element {
/>
</Grid.Column>
</Grid.Row>

<Grid.Row>
<Grid.Column>
<label htmlFor={setStatsEnabledId}>{i18n('stats_enabled')}</label>
<Grid.Hint>{i18n('stats_enabled_hint')}</Grid.Hint>
</Grid.Column>
<Grid.Column>
<Switch
id={setStatsEnabledId}
checked={statsEnabled}
onChange={async (v) => {
setStatsEnabled(v)
await saveSettings({ stats: { enabled: v } }, delayFor.switch)
}}
/>
</Grid.Column>
</Grid.Row>
</Grid>
</>
)
Expand Down
Loading

0 comments on commit b74bad5

Please sign in to comment.