Next.js cookies and analytics
How cookies() works in the Next.js App Router, how to save a visitor's consent with it, and how to run analytics that counts without cookies until a yes.
Updated
In the Next.js App Router, cookies() from next/headers reads cookies in Server Components and sets them only in Server Functions (Server Actions) and Route Handlers; it's asynchronous, so you await it. For analytics, the question is whether the tool sets a cookie before the visitor agrees. nextanalytica doesn't: it counts without cookies until your banner gets an answer, so the visitors who never click it are still counted.
We checked Next.js's cookies reference on November 29, 2026.
How does cookies() work in Next.js?
- It's async. Call
await cookies(), or React'suse, before you read or set anything. - Reading works in Server Components, Server Functions and Route Handlers.
- Setting and deleting work only in a Server Function or a Route Handler. Setting a cookie while a Server Component renders isn't supported.
Read a cookie in a Server Component:
import { cookies } from "next/headers";
export default async function RootLayout({ children }: { children: React.ReactNode }) {
const consent = (await cookies()).get("consent")?.value; // "yes", "no" or undefined
// ...
}Set one from a Server Action:
"use server";
import { cookies } from "next/headers";
export async function saveConsent(yes: boolean) {
(await cookies()).set("consent", yes ? "yes" : "no", { path: "/", maxAge: 60 * 60 * 24 * 365, sameSite: "lax" });
}Do you need a cookie banner for analytics?
It depends on where your visitors are, what your analytics stores on their device and what you do with the data. In the EU, analytics cookies usually need the visitor's consent. Some regulators exempt audience measurement under strict conditions: France's CNIL, for example, asks that it serve only your own audience statistics, produce anonymous data, not be combined with other data and not follow a person across sites. You decide what applies to your site; nextanalytica's privacy page says what it does.
What you can control is what your analytics does before anyone answers.
How does nextanalytica count before consent?
Until a visitor agrees, the tag keeps nothing on the device across visits and identifies nobody. A visitor is counted with a code made at our edge from a daily salt, your project, the IP address and the user agent; the salt is deleted after two days and the IP is never stored. After a yes, the tag keeps a first-party id, na_id, so the same person on two days counts once. See Cookieless counting and consent.
So you don't lose the visitors who never click your banner. A no is different: after consent(false) the tag sends nothing more for that visitor.
How do you wire a banner to the tag in Next.js?
The tag knows the answer only for the page it's on, so tell it the saved answer on every page. Your root layout can read the consent cookie on the server: with a yes, the tag line carries data-consent="granted"; with a no, leave the tag out, since it would send nothing anyway. Copy the line with your project's key from Site:
import Script from "next/script";
import { cookies } from "next/headers";
import { ConsentBanner } from "./consent-banner";
export default async function RootLayout({ children }: { children: React.ReactNode }) {
const consent = (await cookies()).get("consent")?.value; // "yes", "no" or undefined
return (
<html lang="en">
<body>
{children}
{consent === undefined && <ConsentBanner />}
{consent !== "no" && (
<Script
src="https://t.nextanalytica.app/t.js"
data-project="na_…"
{...(consent === "yes" ? { "data-consent": "granted" } : {})}
strategy="afterInteractive"
/>
)}
</body>
</html>
);
}Then a client component saves the answer with the Server Action above and tells the tag:
"use client";
import { useState } from "react";
import { saveConsent } from "./actions";
declare global {
interface Window { nextanalytica?: { consent: (on: boolean) => void } }
}
export function ConsentBanner() {
const [open, setOpen] = useState(true);
if (!open) return null;
const answer = async (yes: boolean) => {
window.nextanalytica?.consent(yes);
await saveConsent(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>
);
}A yes starts the visitor's id; a no removes everything the tag kept and stops it sending. A browser sending Global Privacy Control always counts as no. Reading the cookie in the layout makes your pages render on each request.
Can the tag wait for a yes instead?
Yes. Add data-cookieless="off" to the script, and the tag sends nothing until consent(true): events wait in memory, never on the device.
<Script src="https://t.nextanalytica.app/t.js" data-project="na_…" data-cookieless="off" strategy="afterInteractive" />You then count only the visitors who agree.