-
Notifications
You must be signed in to change notification settings - Fork 0
fix: try to improve analytics by reducing reporting delay on page load #53
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
documentation-ui/src/custom/docs/components/analytics/bootstrap.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' | ||
| import { CookieCategoryName, type CookieConsent } from '../gdpr/types' | ||
| import { retrieveConsentCategoriesFromCookies } from '../gdpr/service/cookie-consent-service' | ||
| import { | ||
| hasGoogleAnalyticsScript, | ||
| loadGoogleAnalytics, | ||
| setGoogleConsentDefault, | ||
| } from './consent-mode' | ||
| import { bootstrapGoogleAnalytics } from './bootstrap' | ||
|
|
||
| vi.mock('./consent-mode', () => ({ | ||
| hasGoogleAnalyticsScript: vi.fn(() => false), | ||
| loadGoogleAnalytics: vi.fn(), | ||
| setGoogleConsentDefault: vi.fn(), | ||
| })) | ||
|
|
||
| vi.mock('../gdpr/service/cookie-consent-service', () => ({ | ||
| retrieveConsentCategoriesFromCookies: vi.fn(), | ||
| })) | ||
|
|
||
| const TEST_GA_ID = 'G-TEST12345' | ||
|
|
||
| const buildConsent = (analyticsGranted: boolean): CookieConsent => ({ | ||
| [CookieCategoryName.Essential]: true, | ||
| [CookieCategoryName.Analytics]: analyticsGranted, | ||
| }) | ||
|
|
||
| beforeEach(() => { | ||
| vi.mocked(hasGoogleAnalyticsScript).mockReturnValue(false) | ||
| vi.mocked(retrieveConsentCategoriesFromCookies).mockReturnValue(buildConsent(false)) | ||
| }) | ||
|
|
||
| afterEach(() => { | ||
| vi.clearAllMocks() | ||
| }) | ||
|
|
||
| describe('bootstrapGoogleAnalytics', () => { | ||
| it('seeds a granted default before loading GA for an already-consented visitor', () => { | ||
| vi.mocked(retrieveConsentCategoriesFromCookies).mockReturnValue(buildConsent(true)) | ||
|
|
||
| bootstrapGoogleAnalytics(TEST_GA_ID) | ||
|
|
||
| expect(setGoogleConsentDefault).toHaveBeenCalledWith({ analytics_storage: 'granted' }) | ||
| expect(loadGoogleAnalytics).toHaveBeenCalledWith(TEST_GA_ID) | ||
|
|
||
| const defaultOrder = vi.mocked(setGoogleConsentDefault).mock.invocationCallOrder[0] | ||
| const loadOrder = vi.mocked(loadGoogleAnalytics).mock.invocationCallOrder[0] | ||
| expect(defaultOrder).toBeLessThan(loadOrder) | ||
| }) | ||
|
|
||
| it('seeds a denied default for a first-time or declining visitor', () => { | ||
| vi.mocked(retrieveConsentCategoriesFromCookies).mockReturnValue(buildConsent(false)) | ||
|
|
||
| bootstrapGoogleAnalytics(TEST_GA_ID) | ||
|
|
||
| expect(setGoogleConsentDefault).toHaveBeenCalledWith({ analytics_storage: 'denied' }) | ||
| expect(loadGoogleAnalytics).toHaveBeenCalledWith(TEST_GA_ID) | ||
| }) | ||
|
|
||
| it('does nothing when no gaId is provided', () => { | ||
| bootstrapGoogleAnalytics('') | ||
|
|
||
| expect(retrieveConsentCategoriesFromCookies).not.toHaveBeenCalled() | ||
| expect(setGoogleConsentDefault).not.toHaveBeenCalled() | ||
| expect(loadGoogleAnalytics).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('is idempotent: does nothing when GA for the same gaId is already injected', () => { | ||
| vi.mocked(hasGoogleAnalyticsScript).mockReturnValue(true) | ||
|
|
||
| bootstrapGoogleAnalytics(TEST_GA_ID) | ||
|
|
||
| expect(hasGoogleAnalyticsScript).toHaveBeenCalledWith(TEST_GA_ID) | ||
| expect(retrieveConsentCategoriesFromCookies).not.toHaveBeenCalled() | ||
| expect(setGoogleConsentDefault).not.toHaveBeenCalled() | ||
| expect(loadGoogleAnalytics).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('re-initialises when only a script for a different gaId is present', () => { | ||
| // hasGoogleAnalyticsScript(gaId) reports false when the injected script | ||
| // targets a different measurement ID, so the bootstrap should proceed and | ||
| // let loadGoogleAnalytics update it. | ||
| vi.mocked(hasGoogleAnalyticsScript).mockReturnValue(false) | ||
|
|
||
| bootstrapGoogleAnalytics(TEST_GA_ID) | ||
|
|
||
| expect(hasGoogleAnalyticsScript).toHaveBeenCalledWith(TEST_GA_ID) | ||
| expect(loadGoogleAnalytics).toHaveBeenCalledWith(TEST_GA_ID) | ||
| }) | ||
| }) |
43 changes: 43 additions & 0 deletions
43
documentation-ui/src/custom/docs/components/analytics/bootstrap.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| /** | ||
| * One-time bootstrap for GA4 + Google Consent Mode v2, deliberately decoupled | ||
| * from React. | ||
| * | ||
| * Running the bootstrap from a React `useEffect` means GA is not initialised | ||
| * until after the tree has rendered, committed and flushed its passive effects, | ||
| * which on a heavy page delays the first `page_view` by hundreds of | ||
| * milliseconds. Because this helper (and the primitives it composes) have no | ||
| * React dependency, it can instead be invoked synchronously from a plain-JS | ||
| * entry point — e.g. the sphinx injection script, before the React root is | ||
| * mounted — so GA starts as early as the page's JavaScript can run. | ||
| */ | ||
|
|
||
| import { retrieveConsentCategoriesFromCookies } from '../gdpr/service/cookie-consent-service' | ||
| import { CookieCategoryName } from '../gdpr/types' | ||
| import { | ||
| hasGoogleAnalyticsScript, | ||
| loadGoogleAnalytics, | ||
| setGoogleConsentDefault, | ||
| } from './consent-mode' | ||
|
|
||
| /** | ||
| * Initialises GA once, seeding the consent default from the visitor's stored | ||
| * decision so a returning, already-consented visitor's first `page_view` is | ||
| * sent with consent granted (rather than going out cookieless and relying on a | ||
| * later `consent update`). | ||
| * | ||
| * Idempotent and safe to call from multiple entry points: the sphinx injection | ||
| * can call it synchronously before mounting React, and `GoogleAnalyticsWithConsent` | ||
| * calls it from an effect as a fallback for contexts without that injection | ||
| * (e.g. Next.js). Whichever runs first wins; subsequent calls are no-ops. | ||
| */ | ||
| export function bootstrapGoogleAnalytics(gaId: string): void { | ||
| if (!gaId || typeof document === 'undefined' || hasGoogleAnalyticsScript(gaId)) { | ||
| return | ||
| } | ||
|
|
||
| const analyticsGranted = retrieveConsentCategoriesFromCookies()[CookieCategoryName.Analytics] | ||
|
|
||
| // The consent default must be set before GA's config call. | ||
| setGoogleConsentDefault({ analytics_storage: analyticsGranted ? 'granted' : 'denied' }) | ||
| loadGoogleAnalytics(gaId) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,3 @@ | ||
| export * from './bootstrap' | ||
| export * from './consent-mode' | ||
| export * from './GoogleAnalyticsWithConsent' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.