Sign inGet started
Tracking

React cookie consent and analytics

View as MarkdownOpen in ChatGPTOpen in Claude
Connect your agent
Add to CursorAdd to VS Code

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 answerAfter a yesAfter a no
Page views and eventsSent, without an idSentNot sent
Kept on the deviceNothing across visitsA first-party id, na_id, for 13 monthsNothing; what the tag kept is removed
identify and groupDo nothingWorkDo 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/tag
src/App.tsx
import { 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:

src/ConsentBanner.tsx
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.

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:

src/ConsentBanner.tsx
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.

Next steps

Related
  • What the tag collects: Page views, clicks, forms, scroll, errors and page speed, counted without code. What never leaves the page.
  • Add the tag: Put one script line in the head of every page. Where it goes in plain HTML, site builders and each framework.
  • Next.js analytics: Add analytics to Next.js with @nextanalytica/tag: install, events, consent and AI crawlers, counted without cookies until a yes and read by your agent.
  • Next.js custom events: Send custom events from a Next.js app with @nextanalytica/tag: sign-ups, demo requests and conversions your agent reads by channel.
  • Next.js cookie consent: Wire a cookie banner in Next.js to analytics: count without cookies until the answer, and pass the saved answer on every page.
  • Next.js revenue tracking: Record orders in a Next.js app, from the browser or from your server, so revenue ties to the visit, channel and campaign it came from.
Still have questions?
Was this page useful?