React cookie consent and analytics
Wire a React cookie consent banner to your analytics so nothing is stored before a yes, with react-cookie-consent or your own component.
Updated
A cookie consent banner in React does two things: it asks the visitor, and it tells your analytics the answer, on every page, not only the moment they click. The common mistakes are analytics that sets its cookie before anyone answers, and a banner that saves the answer but never passes it on. With nextanalytica, the tag counts without cookies until there's an answer, and one call passes it: consent(true) or consent(false).
We checked the react-cookie-consent README on November 29, 2026.
What should happen before the visitor answers?
Nothing should be kept on their device for analytics. nextanalytica's tag works that way by default: it sends page views and events, keeps nothing across visits and identifies nobody, counting each visitor with a code that changes every day. So the visitors who ignore your banner are still counted, without a cookie. See Cookieless counting and consent.
What changes after a yes or a no?
| Before an answer | After a yes | After a no | |
|---|---|---|---|
| Page views and events | Sent, without an id | Sent | Not sent |
| Kept on the device | Nothing across visits | A first-party id, na_id, for 13 months | Nothing; what the tag kept is removed |
identify and group | Do nothing | Work | Do nothing |
A browser sending Global Privacy Control always counts as no.
How do you add the tag in React?
Install the package and put <Analytics> once at your app's root. It renders nothing and adds the tag in the browser. Calls such as consent() made before the tag has loaded wait and go in order, so you can pass a saved answer as soon as the app starts:
pnpm add @nextanalytica/tagimport { Analytics, consent } from "@nextanalytica/tag/react";
import { ConsentBanner } from "./ConsentBanner";
const saved = localStorage.getItem("consent"); // "yes", "no" or null
if (saved !== null) consent(saved === "yes"); // the tag knows the answer only for the page it's on
export function App() {
return (
<>
<Analytics project="na_…" />
{saved === null && <ConsentBanner />}
{/* your app */}
</>
);
}Copy na_…, your project's key, from Site. In a plain HTML page, the script line in <head> does the same job; see Add the tag.
How do you build the banner?
A banner is a few lines. Save the answer and tell the tag:
import { useState } from "react";
import { consent } from "@nextanalytica/tag/react";
export function ConsentBanner() {
const [open, setOpen] = useState(true);
if (!open) return null;
const answer = (yes: boolean) => {
localStorage.setItem("consent", yes ? "yes" : "no");
consent(yes);
setOpen(false);
};
return (
<div role="dialog" aria-label="Cookies">
<p>We use a cookie to recognize returning visitors. Is that OK?</p>
<button onClick={() => answer(true)}>Yes</button>
<button onClick={() => answer(false)}>No</button>
</div>
);
}Give both buttons the same weight: a "No" hidden behind a settings link isn't a real choice.
How do you use react-cookie-consent?
react-cookie-consent renders the banner and saves the answer in a cookie, CookieConsent by default. Turn on the decline button and pass both answers to the tag:
import CookieConsent from "react-cookie-consent";
import { consent } from "@nextanalytica/tag/react";
export function ConsentBanner() {
return (
<CookieConsent enableDeclineButton onAccept={() => consent(true)} onDecline={() => consent(false)}>
We use a cookie to recognize returning visitors.
</CookieConsent>
);
}On later page loads, read the saved answer with getCookieConsentValue() from the same package and pass it to consent() when the app starts, as in App.tsx above.
Can the tag wait for a yes instead?
Yes: <Analytics project="na_…" cookieless={false} /> sends nothing until consent(true). Events wait in memory, never on the device. You then count only the visitors who agree.
Do you need the banner at all?
That depends on where your visitors are and what your site stores. You decide; nextanalytica's privacy page says what it does. If nothing on your site needs consent besides a tag like this one, ask whoever advises you on privacy whether you need a banner at all.