|
| 1 | +/** |
| 2 | + * @vitest-environment jsdom |
| 3 | + * @vitest-environment-options {"url":"https://www.sim.ai"} |
| 4 | + */ |
| 5 | +import { writeFileSync } from 'node:fs' |
| 6 | +import { act } from 'react' |
| 7 | +import { type ConsentManagerOptions, ConsentManagerProvider } from '@c15t/nextjs/headless' |
| 8 | +import { clearConsentRuntimeCache, getOrCreateConsentRuntime } from 'c15t' |
| 9 | +import { PathnameContext } from 'next/dist/shared/lib/hooks-client-context.shared-runtime' |
| 10 | +import { createRoot, type Root } from 'react-dom/client' |
| 11 | +import { afterEach, describe, expect, it, vi } from 'vitest' |
| 12 | +import { getGlobalConsentScripts } from '@/lib/consent/scripts' |
| 13 | +import { GoogleAnalyticsPageViewTracker } from '@/app/_shell/consent/google-analytics-page-view-tracker' |
| 14 | + |
| 15 | +let root: Root | undefined |
| 16 | + |
| 17 | +afterEach(() => { |
| 18 | + act(() => root?.unmount()) |
| 19 | + root = undefined |
| 20 | + clearConsentRuntimeCache() |
| 21 | + vi.useRealTimers() |
| 22 | + window.dataLayer = [] |
| 23 | + Reflect.deleteProperty(window, 'gtag') |
| 24 | + window.history.replaceState({}, '', '/') |
| 25 | +}) |
| 26 | + |
| 27 | +describe('Google navigation measurement', () => { |
| 28 | + it.each([ |
| 29 | + { initialPath: '/', navigateBeforeConsent: false, navigateBeforeReady: false }, |
| 30 | + { initialPath: '/', navigateBeforeConsent: true, navigateBeforeReady: false }, |
| 31 | + { initialPath: '/', navigateBeforeConsent: true, navigateBeforeReady: true }, |
| 32 | + { |
| 33 | + initialPath: '/workspace/workspace-id', |
| 34 | + navigateBeforeConsent: false, |
| 35 | + navigateBeforeReady: false, |
| 36 | + }, |
| 37 | + ])( |
| 38 | + 'preserves consent and navigation semantics ($initialPath, $navigateBeforeConsent, $navigateBeforeReady)', |
| 39 | + ({ initialPath, navigateBeforeConsent, navigateBeforeReady }) => { |
| 40 | + vi.stubEnv('NODE_ENV', 'production') |
| 41 | + vi.stubGlobal('IS_REACT_ACT_ENVIRONMENT', true) |
| 42 | + vi.stubGlobal('matchMedia', (media: string) => ({ |
| 43 | + matches: false, |
| 44 | + media, |
| 45 | + onchange: null, |
| 46 | + addListener: () => {}, |
| 47 | + removeListener: () => {}, |
| 48 | + addEventListener: () => {}, |
| 49 | + removeEventListener: () => {}, |
| 50 | + dispatchEvent: () => true, |
| 51 | + })) |
| 52 | + vi.useFakeTimers() |
| 53 | + const initialUrl = `${initialPath}?utm_source=proof` |
| 54 | + window.history.replaceState({}, '', initialUrl) |
| 55 | + const google = getGlobalConsentScripts().find((script) => script.id === 'gtag') |
| 56 | + const deniedConsent = { |
| 57 | + necessary: true, |
| 58 | + functionality: false, |
| 59 | + experience: false, |
| 60 | + measurement: false, |
| 61 | + marketing: false, |
| 62 | + } |
| 63 | + google?.onBeforeLoad?.({ |
| 64 | + id: 'gtag', |
| 65 | + elementId: 'gtag', |
| 66 | + hasConsent: false, |
| 67 | + consents: deniedConsent, |
| 68 | + }) |
| 69 | + |
| 70 | + const options = { |
| 71 | + mode: 'offline', |
| 72 | + colorScheme: 'light', |
| 73 | + consentCategories: ['necessary', 'measurement', 'marketing'], |
| 74 | + store: { enabled: false }, |
| 75 | + } satisfies ConsentManagerOptions |
| 76 | + const { consentStore } = getOrCreateConsentRuntime(options) |
| 77 | + consentStore.setState({ |
| 78 | + hasFetchedBanner: true, |
| 79 | + loadedScripts: { gtag: true }, |
| 80 | + consents: deniedConsent, |
| 81 | + policyCategories: ['necessary', 'measurement', 'marketing'], |
| 82 | + }) |
| 83 | + const container = document.createElement('div') |
| 84 | + root = createRoot(container) |
| 85 | + const render = (path: string) => { |
| 86 | + window.history.pushState({}, '', path) |
| 87 | + act(() => { |
| 88 | + root?.render( |
| 89 | + <ConsentManagerProvider options={options}> |
| 90 | + <PathnameContext.Provider value={new URL(window.location.href).pathname}> |
| 91 | + <GoogleAnalyticsPageViewTracker /> |
| 92 | + </PathnameContext.Provider> |
| 93 | + </ConsentManagerProvider> |
| 94 | + ) |
| 95 | + }) |
| 96 | + } |
| 97 | + const pageViews = () => |
| 98 | + window.dataLayer.filter( |
| 99 | + (row) => Array.isArray(row) && row[0] === 'event' && row[1] === 'page_view' |
| 100 | + ) |
| 101 | + const latestContext = () => |
| 102 | + window.dataLayer.filter((row) => Array.isArray(row) && row[0] === 'set').at(-1) |
| 103 | + |
| 104 | + render(navigateBeforeReady ? '/pricing?secret=private' : initialUrl) |
| 105 | + if (navigateBeforeConsent) { |
| 106 | + render('/pricing?secret=private') |
| 107 | + expect(latestContext()).toEqual([ |
| 108 | + 'set', |
| 109 | + expect.objectContaining({ |
| 110 | + page_location: 'https://www.sim.ai/pricing', |
| 111 | + page_referrer: 'https://www.sim.ai/?utm_source=proof', |
| 112 | + }), |
| 113 | + ]) |
| 114 | + } |
| 115 | + expect(pageViews()).toHaveLength(0) |
| 116 | + |
| 117 | + act(() => { |
| 118 | + consentStore.setState({ consents: { ...deniedConsent, measurement: true } }) |
| 119 | + }) |
| 120 | + expect(pageViews()).toHaveLength(navigateBeforeConsent ? 1 : 0) |
| 121 | + render('/demo') |
| 122 | + render('/demo') |
| 123 | + expect(pageViews()).toHaveLength(navigateBeforeConsent ? 2 : 1) |
| 124 | + |
| 125 | + act(() => { |
| 126 | + consentStore.setState({ consents: deniedConsent }) |
| 127 | + }) |
| 128 | + render('/workspace/workspace-id/files/file-id') |
| 129 | + expect(pageViews()).toHaveLength(navigateBeforeConsent ? 2 : 1) |
| 130 | + expect(latestContext()).toEqual([ |
| 131 | + 'set', |
| 132 | + expect.objectContaining({ |
| 133 | + page_location: 'https://www.sim.ai/workspace', |
| 134 | + page_referrer: 'https://www.sim.ai/demo', |
| 135 | + page_title: 'Sim', |
| 136 | + }), |
| 137 | + ]) |
| 138 | + |
| 139 | + const reportPath = process.env.GAUGE_TRACKER_REPORT_PATH |
| 140 | + if (reportPath && navigateBeforeConsent && !navigateBeforeReady) { |
| 141 | + writeFileSync( |
| 142 | + reportPath, |
| 143 | + JSON.stringify( |
| 144 | + { |
| 145 | + status: 'passed', |
| 146 | + checks: [ |
| 147 | + 'denied navigation context', |
| 148 | + 'consent emission gate', |
| 149 | + 'delayed consent after navigation', |
| 150 | + 'navigation deduplication', |
| 151 | + 'withdrawn consent context', |
| 152 | + ], |
| 153 | + commands: window.dataLayer, |
| 154 | + limitation: |
| 155 | + 'Real React, Next router context and c15t store; Google SDK collection is not exercised.', |
| 156 | + }, |
| 157 | + null, |
| 158 | + 2 |
| 159 | + ) |
| 160 | + ) |
| 161 | + } |
| 162 | + } |
| 163 | + ) |
| 164 | +}) |
0 commit comments