Skip to content

Add a cookie consent banner to React

c15t puts the banner in your React app as code. It holds tags until the visitor chooses and keeps Google Consent Mode v2 in sync.

Ask your coding agent
Sample Consent banner with c15t’s English strings

Install c15t in a React app

c15t is the open source consent library behind Inth Consent. It renders the banner from your own bundle and records each choice in your Inth project.

npm install c15t @c15t/integrations

Add your Inth project URL to .env. Copy it from your Inth dashboard. Vite reads it at build time, so set it before you build.

.env

VITE_C15T_BACKEND_URL=https://your-project.inth.app

Add the c15t Vite plugin. It gives the backend URL to hosted() and writes no file into your project.

vite.config.ts

import react from '@vitejs/plugin-react';
import { consentManifest } from 'c15t/build';
import { defineConfig } from 'vite';

export default defineConfig({ plugins: [react(), consentManifest()] });

Code on this page comes from the c15t React quickstart, checked 28 October 2026.

React Router framework mode and Remix also work. The browser resolves consent after hydration there.

Mount the provider and the banner

Create src/consent.tsx. ConsentProvider starts consent and loads your scripts. ConsentBanner and ConsentDialog render when the visitor’s policy asks for them.

ConsentDialogLink lets visitors open their preferences again after the banner closes.

src/consent.tsx

import { posthog } from '@c15t/integrations/posthog';
import {
  ConsentBanner,
  ConsentDialog,
  ConsentDialogLink,
  ConsentProvider,
  hosted,
} from 'c15t/react';
import type { ReactNode } from 'react';

const options = {
  // Asks VITE_C15T_BACKEND_URL for each visitor's policy.
  mode: hosted(),
  scripts: [
    posthog({
      id: 'phc_your_project_key',
      initOptions: { cookieless_mode: 'never' },
      loadMode: 'after-consent',
    }),
  ],
};

export const Consent = ({ children }: { children: ReactNode }) => (
  <ConsentProvider options={options}>
    {children}
    <ConsentBanner />
    <ConsentDialog />
    <footer>
      <ConsentDialogLink>Privacy settings</ConsentDialogLink>
    </footer>
  </ConsentProvider>
);

Wrap your app with it in src/main.tsx. Keep Consent mounted for the life of the app. If you use a client router, render the router inside Consent.

src/main.tsx

import { createRoot } from 'react-dom/client';

import { App } from './app';
import { Consent } from './consent';

const root = document.getElementById('root');
if (!root) {
  throw new Error('Missing #root element');
}
createRoot(root).render(
  <Consent>
    <App />
  </Consent>
);

There is no stylesheet to import. The banner and the dialog bring their own styles.

Hold scripts until consent

List each tag in the scripts array of ConsentProvider. With loadMode: 'after-consent', the PostHog SDK stays off the page until the visitor allows measurement.

Remove every other loader for PostHog, so it loads once and only through c15t. React scripts and embeds covers more vendors, iframes and the network blocker. The c15t integrations list the helper for each vendor.

src/marketing-banner.tsx

import { useConsent } from 'c15t/react';

export function MarketingBanner() {
  return useConsent('marketing') ? <aside>Spring sale</aside> : null;
}

src/consent-scripts.ts

import { gtag } from '@c15t/integrations/google-tag';

export const scripts = [gtag({ id: 'G-XXXXXXXXXX', category: 'measurement' })];

Show the right banner in each region

Your Inth project sets a policy for each region, such as opt in for the EU and UK or opt out for California. With hosted(), the browser asks your backend for the policy, and the backend reads the visitor’s location from that request. Regional policies need no extra code.

Test each region before launch. See how regional policies work.

When the banner appears

hosted() sends one /init request to your backend. The Vite plugin adds a small script to index.html that sends this request while your JavaScript downloads, so the banner can show sooner.

For a banner in the first server HTML, use the Next.js or TanStack Start setup. Measure your own pages before and after you add the banner.

Check that consent gates work

Build with vite build, run vite preview and open a private window with the Network tab open. Test under an opt in policy, such as the EU policy.

  1. Under an opt in policy, no PostHog request leaves the page before a choice.
  2. After Reject All and a reload, PostHog stays off.
  3. When you turn on measurement in Privacy settings, PostHog loads.
  4. When you turn measurement off, the page reloads without PostHog.

Choices post to your Inth project. Check that Inth has a record for each one. The c15t verify consent guide has the full release checklist.

Use Inth in production

c15t is open source. Point it at your Inth project, which hosts the backend and keeps the consent records. See Inth open source.

c15t also has a local mode with no backend. It keeps no consent records, so the docs advise against it in production.

Want to see what else your repo sends before consent? Run a free codebase scan.

Cookie consent banners for other frameworks

The same banner and the same Inth project work in other stacks. See the setup for Next.js, TanStack Start, Nuxt, Vue, Astro, Svelte, SvelteKit, HTML and JavaScript.

Want to compare React libraries? Read Inth vs react-cookie-consent.

See how the Inth consent banner works.

Questions about React consent