Frontend · Skill
Add a cookie consent banner
add-cookie-consent-banner
Adds a consent banner that blocks analytics and ad scripts until the visitor accepts, and remembers that choice. Use this when the user wants a cookie banner, consent before tracking, a GDPR notice, or to keep trackers off until opt-in.
Works with: Any AI Not yet model-tested; testing Jan 2027
Paste it into ChatGPT, Claude, Gemini or any AI chat, then tell it about your project. How it works
Install in Claude Code, Cursor or Codex (optional)
Only for AI coding tools that read skill folders. Run one line in a terminal, in your project folder. It downloads this skill's text files from TheSkillVault and nothing else; you can read them first with “View raw SKILL.md”.
Claude Code (all your projects)
curl -fsSL --create-dirs 'https://theskillvault.ai/api/skills/add-cookie-consent-banner/raw' -o ~/'.claude/skills/add-cookie-consent-banner/SKILL.md'Cursor (this project)
curl -fsSL --create-dirs 'https://theskillvault.ai/api/skills/add-cookie-consent-banner/raw' -o '.cursor/skills/add-cookie-consent-banner/SKILL.md'Codex and other Agent Skills tools (this project)
curl -fsSL --create-dirs 'https://theskillvault.ai/api/skills/add-cookie-consent-banner/raw' -o '.agents/skills/add-cookie-consent-banner/SKILL.md'
Or connect the whole library over MCP
Add TheSkillVault to Cursor or add this to your app's MCP settings (read-only; help):
{
"mcpServers": {
"theskillvault": {
"url": "https://theskillvault.ai/mcp"
}
}
}Load trackers only after a real yes. The banner is the gate. It is not the privacy page. For the page text and a privacy-friendly analytics setup, use add-analytics-and-privacy-policy.
This is not legal advice. Rules depend on who visits and where you operate. If you are unsure, ask a lawyer before you rely on the banner.
When to use
- Analytics, ads or a chat widget set cookies or send an identifier before the visitor chooses.
- You need Accept and Reject, with the same visual weight.
- The choice must survive a reload.
When not to use
- The only measurement is a cookie-free tool whose vendor documents that it sets no tracking cookies. You may not need a banner. Still say so on the privacy page.
- You need the privacy policy wording itself. That is the other skill.
Steps
- List scripts that are not required for the page to function: analytics, ads, session-replay, embedded marketing pixels. The sign-in cookie and the consent choice itself stay on.
- Render a banner with two buttons, Accept and Reject. Do not pre-tick a box. Do not load the optional scripts in the first paint.
- Save the choice in a first-party cookie or in local storage, with the date. Read it before you decide whether to inject a script.
- On Accept, then add the script tags. On Reject, leave them out. Reloading must not ask again and must not start the trackers after a reject.
- If you use Google tags, set consent to denied before any tag runs, and update it to granted only after Accept. Check the current consent API rather than copying an old snippet.
- Offer a way back, such as a "Cookie settings" link, so the visitor can change the choice.
- Link the banner to the privacy page. Keep the banner copy to one or two short sentences.
- Test in a fresh profile with the network panel. No tracker request appears until Accept. Reject stays quiet after reload.
"use client";
const KEY = "cookie-consent";
export function CookieBanner({ onChoice }: { onChoice: (value: "accept" | "reject") => void }) {
const save = (value: "accept" | "reject") => {
localStorage.setItem(KEY, value);
onChoice(value);
};
return (
<div role="dialog" aria-label="Cookie consent">
<p>Optional analytics stay off until you accept.</p>
<button type="button" onClick={() => save("reject")}>Reject</button>
<button type="button" onClick={() => save("accept")}>Accept</button>
</div>
);
}
Mount the analytics script only when the saved value is accept.
Gotchas
- A tag in the root layout runs before the banner exists. Put optional scripts behind the saved choice.
- A "continue" click that counts as accept, with no reject, is not a choice. Give a real reject.
- Blocking the script tag is not enough if another component still calls the tracker. Search the repo for the script URL.
- Dark patterns (a tiny reject, a pre-selected accept) fail the point of the banner and may fail the rule that applies to you.
Safety
- The consent value is personal data in some places. Store only the choice and the date, not a copy of the tracking id.
- Do not send the visitor to a third-party script "just to see if they would have consented".
Portability
Works with any framework, or with plain HTML. If you use Next.js, Astro or a static host, the rule is the same: do not emit the tracker until Accept. A server-rendered page must not set analytics cookies before the choice.
Example prompt
"Add a cookie banner that blocks analytics and ads until Accept, with Reject just as easy, and remember the choice."
Done when
- A fresh visit sends no analytics or ad request before a choice.
- Reject keeps those requests off after reload.
- Accept loads them, and a settings link can change the stored choice.
---
House original by TheSkillVault (CC-BY-4.0).
Licence
Licence: CC BY 4.0. Attribution when you reuse it: From TheSkillVault, https://theskillvault.ai, CC BY 4.0