diff --git a/src/bundles/analytics.js b/src/bundles/analytics.js index 766cc22b8..d0c61f6f4 100644 --- a/src/bundles/analytics.js +++ b/src/bundles/analytics.js @@ -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 @@ -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 diff --git a/src/bundles/analytics.test.js b/src/bundles/analytics.test.js index 7f81a15a8..11e72a6f6 100644 --- a/src/bundles/analytics.test.js +++ b/src/bundles/analytics.test.js @@ -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, @@ -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, @@ -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', () => {