Cookie banner that blocks trackers
Cookie box that loads no tracker before the visitor's yes
For when the answer to “It runs ad or analytics tracking (Meta Pixel, Google tag, TikTok Pixel) and needs to ask first” is yes.
A cookie box where no cookie, pixel or third-party request happens until the visitor says yes to that exact purpose, and an old yes never covers a purpose added later.
Skip the rebuild
You do not write this one. It arrives in your project as ordinary source you can read, change and keep, with its audit record and its caveats beside it.
- Download the Playground. It is free and runs on your own machine.
- Start a new app and tick “It runs ad or analytics tracking (Meta Pixel, Google tag, TikTok Pixel) and needs to ask first”.
- This block is written in, along with anything else you ticked.
The mistake it removes
A tracker tag pasted into the page head runs before any banner can ask, so every visit is sent before consent. A banner that stores one global yes keeps honoring it after a new pixel is added, which is consent to something the visitor was never shown.
What you still have to do
A copied file cannot own your secrets, your host or your legal obligations. This is the part the block deliberately does not claim.
- Do one keyboard pass and one screen-reader pass of the box on your own pages before launch. The box is a labeled region with a fieldset, a legend and real buttons, but the test drives it with clicks in headless Chromium, which is not a screen reader and is not your page.
- Put privacySection(config) on your privacy page, read every paragraph it writes, and correct any sentence that is not true of your site. Then have the page checked by someone qualified for the laws where your visitors live; this block writes the cookie section, not the rest of the policy.
- A script that is already running stays in memory after Change my answer or a no, until the next page load, which will not load it. The Meta, Google and TikTok loaders send their own revoke call when they ran on that page; anything you load by src does not get one.
- Cookie deletion reaches cookies that page JavaScript can see. An HttpOnly cookie a vendor sets from its own server response on your domain cannot be deleted from the browser; list it anyway so the privacy page names it, and clear it server side if you need to.
- Deletion runs on path / for the host and every parent domain. A cookie a tracker wrote on another path survives.
- Global Privacy Control means no for every purpose that asks, and the box does not show to that browser. There is no override for a visitor who sends GPC and still wants to say yes.
- The Meta, Google and TikTok loaders were driven against stub scripts in the test, never against the live vendor files, so a change on the vendor's side is caught only by the upstream check dates above.
- The answer lives in localStorage. In a private window that blocks storage, the box asks on every page and nothing optional loads.
What lands in your project
vanilla
- vanilla/consent.js
A classic script, served from your own domain at the path the stamper's scriptPath names. It reads the purpose list from the page, shows the box when there is no current answer, and loads each purpose's scripts only after its yes. window.consentKit.granted(id) and window.consentKit.reset() are there for your own code, and a consent-kit:granted event fires on document per purpose.
node
- node/consent-stamp.js
Build time only. injectConsent(html, config) stamps the box, the purpose list and one first-party script tag into a page; privacySection(config) writes the cookie section of your privacy page, one paragraph per purpose plus the Change my answer button. validateConfig() refuses a list that would load anything early.
What you supply
- purposes: a list of { id, label, description, essential, scripts, cookies }. scripts are { src: 'https://...' or '/path.js' } or { vendor: 'meta-pixel' | 'google-tag' | 'tiktok-pixel', id }. cookies are names to delete on a no, and a trailing * matches a prefix (_ga_*)
- scriptPath: where you serve vanilla/consent.js on your own site (default /consent-kit.js)
- privacyUrl: the page holding privacySection() and its Change my answer button (default /privacy/#cookies)
- storageKey: the localStorage key for the answer (default consent-kit)
Licensed MIT. It is a starting point, not a finished product.
Get it
Download the Playground See the other blocks
Nothing here is locked. The files are yours, in your folder, under a permissive license.
Questions
- Is Cookie banner that blocks trackers audited?
- No. No full audit has been run against this block yet, which is not the same as a pass.
- What do I still have to do myself?
- Do one keyboard pass and one screen-reader pass of the box on your own pages before launch. The box is a labeled region with a fieldset, a legend and real buttons, but the test drives it with clicks in headless Chromium, which is not a screen reader and is not your page.
- How do I get this code?
- Download the Playground, start a new app, and tick “It runs ad or analytics tracking (Meta Pixel, Google tag, TikTok Pixel) and needs to ask first”. The block is written into your project as ordinary source you can read and edit.
All pre-built code blocks · Learn to build from zero · The coding guide