Tracking
React Router cookie consent
Wire a cookie banner in React Router to analytics: count without cookies until the answer, and pass the saved answer on every page.
Updated
In React Router, pass the saved answer with consent() from @nextanalytica/tag in an effect in app/root.tsx, and again when the visitor clicks your banner. Until there's an answer, nextanalytica counts without cookies.
How does it look?
app/root.tsx
import { useEffect } from "react";
import { Analytics } from "@nextanalytica/tag/react";
import { consent } from "@nextanalytica/tag";
export function ConsentFromStorage() {
useEffect(() => {
const saved = localStorage.getItem("consent"); // "yes", "no" or null
if (saved !== null) consent(saved === "yes");
}, []);
return <Analytics project="na_…" />;
}
// render <ConsentFromStorage /> inside <body>, next to <Scripts />What can you ask your agent?
- "How many visitors this week came before answering the banner?"