Single-Page Applications change how websites navigate, but they also change how consent management needs to work.

In a traditional multi-page website, navigation often triggers a fresh document load. In a React SPA, the browser can remain on the same document while the application changes routes.

That creates several engineering challenges:

  • Consent must persist across routes.
  • Dynamically loaded scripts must respect consent.
  • Analytics must handle virtual page views.
  • Consent withdrawal must affect running integrations.
  • Route components must not create duplicate tracking initialisations.

Let's look at a practical architecture.

1. Why route changes matter

Imagine a SaaS application with these routes:

/ /dashboard /settings /billing /reports 

A visitor accepts analytics on the homepage and moves to the dashboard.

The browser may not reload. The application simply renders a different route.

If consent state exists only inside the homepage component, it may disappear when that component unmounts. If analytics is initialised independently inside each route, it may initialise more than once.

The solution is to separate consent management from route-specific UI.

2. Recommended architecture

Application Root | v CMP Initialisation | v Central Consent Adapter | +---- Analytics integrations | +---- Marketing integrations | +---- Functional integrations | v Route and Component Events 

The CMP should remain the authoritative source of saved preferences.

A React context or application-level state adapter can expose the current preferences to route components. It should not become a separate source of truth that conflicts with the CMP.

3. Create a persistent consent provider

A basic React structure:

import { createContext, useContext } from "react"; const ConsentContext = createContext(null); export function ConsentProvider({ value, children }) { return ( <ConsentContext.Provider value={value}> {children} </ConsentContext.Provider> ); } export function useConsent() { return useContext(ConsentContext); } 

Place the provider above the route components.

function App() { const consent = useConsentFromCMP(); return ( <ConsentProvider value={consent}> <AppRoutes /> </ConsentProvider> ); } 

useConsentFromCMP() is a placeholder. Replace it with the actual API documented by your consent provider.

4. Control dynamic scripts

A route-specific component should not initialise an external service merely because it has mounted.

For example:

import { useEffect } from "react"; function Analytics({ allowed }) { useEffect(() => { if (!allowed) return; initialiseAnalytics(); }, [allowed]); return null; } 

The service's initialisation function should be idempotent, so repeated rendering does not register duplicate instances.

Also implement the provider's supported shutdown or opt-out mechanism when consent is withdrawn. Removing a script element alone does not reverse code that has already executed.

5. Next.js: use a persistent layout

The Next.js App Router uses layouts, pages and client-side navigation.

A root layout can host the application-wide consent integration:

import { ConsentProvider } from "./components/ConsentProvider"; export default function RootLayout({ children }) { return ( <html lang="en"> <body> <ConsentProvider> {children} </ConsentProvider> </body> </html> ); } 

This example illustrates component placement. The actual CMP may require a specific server-side or client-side setup.

Do not assume that placing a CMP in a root layout automatically blocks all third-party services. Configure each integration using the provider's documented mechanism.

6. Virtual page views in React

Client-side navigation does not necessarily generate a traditional browser page load.

For analytics, a route change can be treated as a virtual page view:

import { useEffect } from "react"; function PageViewTracker({ pathname, allowed }) { useEffect(() => { if (!allowed) return; sendPageView(pathname); }, [pathname, allowed]); return null; } 

sendPageView() is a placeholder for the analytics provider's documented API.

Important considerations:

  • Check consent before sending events.
  • Avoid duplicate events from automatic and manual tracking.
  • Do not send sensitive URL parameters.
  • Stop future events after withdrawal.
  • Decide how to handle navigation that occurred before consent was granted.

7. Google Consent Mode across route changes

Google's developer documentation says consent updates should be tracked on the page where the user makes the change, before navigation.

For example:

gtag("consent", "update", { analytics_storage: "granted" }); 

This is a simplified illustration. Map all relevant user preferences to the appropriate Consent Mode v2 signals.

When using Google Tag Manager, use its consent APIs, including the appropriate default and update mechanisms. Do not assume a generic data-layer event is equivalent to a consent update.

Google Consent Mode controls supported Google tags. It does not automatically manage Meta Pixel, PostHog, chat widgets or unrelated third-party scripts.

8. CMPs with SPA support

Two useful examples of documented developer functionality:

CookieYes

  • React and Next.js SDKs.
  • useOnConsentChange for reacting to consent updates.
  • GatedScript for loading scripts after consent.
  • Integrations for services such as Google Analytics, Meta Pixel, Clarity and PostHog.

Its React documentation also explains why checking the saved categories on the initial mount matters for returning visitors.

Usercentrics

  • SPA implementation guidance.
  • refreshScripts() for CMP v3.
  • enableScriptsForServicesWithConsent() for CMP v2.
  • Event-listener options for script handling.

For ConsentX, check the current dashboard installation instructions and confirm the exact SPA and script-control capabilities available for your implementation. Do not assume these other providers' APIs are compatible with ConsentX.

9. Test every navigation scenario

Use a fresh browser session and test:

Scenario What to verify
Initial visit Non-essential services remain blocked where required
Navigation without consent Route changes do not activate blocked scripts
Analytics accepted Approved analytics works
Marketing rejected Marketing integrations remain blocked
Consent withdrawn Future processing stops
Return visit Saved choices are restored
Dynamic component Newly mounted integrations respect consent

Inspect Network requests, cookies, local storage and session storage. A visible banner is not proof of correct script control.

10. Developer checklist

  • [ ] CMP integrated at application level.
  • [ ] One authoritative consent state.
  • [ ] Preferences persist across routes.
  • [ ] Dynamic scripts are consent-aware.
  • [ ] Route-based analytics respects consent.
  • [ ] Duplicate page views are prevented.
  • [ ] Consent updates happen before navigation where required.
  • [ ] Withdrawal stops future processing.
  • [ ] Returning visitors are handled correctly.
  • [ ] All routes are tested in a clean browser session.

Conclusion

SPA consent management is fundamentally about state, lifecycle and script control.

Treat consent as persistent application state, use the CMP's documented integration methods, and make every route and dynamically mounted component follow the same rules.