Skip to content
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
44 changes: 31 additions & 13 deletions src/bundles/analytics.js
Original file line number Diff line number Diff line change
Expand Up @@ -366,9 +366,38 @@ const createAnalyticsBundle = ({
if (!root.Countly) {
root.Countly = {}
root.Countly.q = []
// @ts-ignore
await import('countly-sdk-web')
if (DISABLE_ALL_ANALYTICS) {
// Set up mock Countly methods to prevent errors
root.Countly.opt_out = () => {}
root.Countly.opt_in = () => {}
root.Countly.init = () => {}
} else {
// @ts-ignore
await import('countly-sdk-web')
}
}

// Always set up route subscription (essential for app functionality)
store.subscribeToSelectors(['selectRouteInfo'], ({ routeInfo }) => {
// skip routes with no hash, as we'll be immediately redirected to `/#`
if (!root.location || !root.location.hash) return
/*
By tracking the pattern rather than the window.location, we limit the info
we collect to just the app sections that are viewed, and avoid recording
specific CIDs or local repo paths that would contain personal information.
*/
// Only track pageviews if analytics are enabled
if (DISABLE_ALL_ANALYTICS) return
root.Countly.q.push(['track_pageview', routeInfo.pattern])
})

// Skip all Countly configuration and user flows when analytics are disabled
// Requests to https://countly.ipfs.tech will always fail since the domain was shut down
// See: https://github.com/ipfs/ipfs-webui/issues/2334
if (DISABLE_ALL_ANALYTICS) {
return
}

const Countly = root.Countly

Countly.require_consent = true
Expand Down Expand Up @@ -403,17 +432,6 @@ const createAnalyticsBundle = ({
store.doEnableAnalytics()
}

store.subscribeToSelectors(['selectRouteInfo'], ({ routeInfo }) => {
// skip routes with no hash, as we'll be immediately redirected to `/#`
if (!root.location || !root.location.hash) return
/*
By tracking the pattern rather than the window.location, we limit the info
we collect to just the app sections that are viewed, and avoid recording
specific CIDs or local repo paths that would contain personal information.
*/
root.Countly.q.push(['track_pageview', routeInfo.pattern])
})

// Fix for storybook error 'Countly.init is not a function'
if (typeof Countly.init === 'function') {
await countlyAppKeyPromise
Expand Down
13 changes: 12 additions & 1 deletion src/bundles/analytics.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ describe('new/returning user default behavior', () => {
// should not show analytics banner for these users
expect(store.selectShowAnalyticsBanner()).toBe(false)
})
/* This test is disabled because analytics are completely disabled when DISABLE_ALL_ANALYTICS is true.
See: https://github.com/ipfs/ipfs-webui/issues/2334
it('should enable analytics for returning user who opted_out prior to new opt-in by default updates', () => {
const mockDefaultState = {
lastEnabledAt: 0,
Expand All @@ -82,7 +84,16 @@ describe('new/returning user default behavior', () => {
expect(store.selectAnalyticsConsent()).toEqual(['sessions', 'events', 'views', 'location'])
// should show analytics banner for these users
expect(store.selectShowAnalyticsBanner()).toBe(true)
}) */
it('should remain disabled by default when DISABLE_ALL_ANALYTICS is true', () => {
const store = createStore({})
expect(global.Countly.opt_in).not.toHaveBeenCalled()
expect(store.selectAnalyticsConsent()).toEqual([])
expect(store.selectAnalyticsOptedOut()).toBe(true)
expect(store.selectShowAnalyticsBanner()).toBe(false)
})
/* This test is disabled because analytics are completely disabled when DISABLE_ALL_ANALYTICS is true.
See: https://github.com/ipfs/ipfs-webui/issues/2334
it('should hide analytics banner if user has closed the banner', () => {
const mockDefaultState = {
lastEnabledAt: 0,
Expand All @@ -97,7 +108,7 @@ describe('new/returning user default behavior', () => {
store.doToggleShowAnalyticsBanner(false)
expect(store.selectShowAnalyticsBanner()).toBe(false)
expect(store.selectAnalyticsOptedOutPriorToDefaultOptIn()).toBe(false)
})
}) */
})

describe('user enables and disables analytics', () => {
Expand Down