Zero-dependency consent manager
Cookie consent without the bloat.
A small TypeScript package for static sites and SPAs. It stores consent, blocks optional scripts, handles privacy-safe embeds, and keeps the UI maintainable.
- 0
- runtime dependencies
- 4
- consent categories
- <20 KB
- target bundle
Live demo
Test the real package behavior.
Use the buttons, open preferences, and try the YouTube placeholder. The sample events below show what runs immediately and what waits for consent.
Sample events
These examples simulate the kind of code each category would run on a production page.
YouTube placeholder
The iframe is not created until the visitor grants external-media consent or chooses to load this video once.
Category examples
What each consent type means in practice.
Necessary code runs immediately. Analytics and marketing examples stay blocked until the matching category is granted.
Necessary
Core site behavior: CSRF tokens, form validation, session state, language choice, and security checks.
// Do not gate first-party form handling.
document.querySelector('form')?.addEventListener('submit', saveLead);
Analytics
Page-view or event measurement with tools such as Plausible, Umami, or Google Analytics.
<script type="text/plain" data-consent="analytics">
loadPlausible();
</script>
Marketing
Advertising and conversion tracking, for example Meta Pixel, Google Ads, or remarketing tags.
Consent.whenGranted('marketing', () => {
loadMetaPixel();
});
Tutorial / how to
Drop it into production in four steps.
This is the path you can follow for a plain HTML site. Frameworks use the same API; initialize it from a client-side entry.
Add the package files
Use the built files directly or publish this folder as an npm package.
<link rel="stylesheet" href="/consent-kit.css">
<script type="module" src="/consent-boot.js"></script>
Initialize ConsentKit
Increase the version when your policy or category purpose changes.
import { Consent } from './consent-kit.js';
Consent.init({
version: 1,
locale: 'en',
expirationDays: 180,
googleConsentMode: true
});
Gate third-party scripts
Do not load analytics, ads, pixels, maps, or media until the matching category is granted.
<script type="text/plain" data-consent="analytics">
loadGoogleAnalytics();
</script>
Consent.whenGranted('marketing', loadMetaPixel);
Add privacy-safe embeds
YouTube uses a placeholder and upgrades to youtube-nocookie.com after consent.
<div
class="ck-youtube"
data-video-id="VIDEO_ID"
data-youtube-url="https://www.youtube.com/watch?v=VIDEO_ID"
data-title="Product demo">
</div>
Consent audit log
Should you track consent history?
For a small static site, local storage is often enough for runtime behavior. If you need to prove consent later, keep a server-side audit log of changes, not only the latest browser state.
Usually enough
Store the latest consent in the browser with category values, timestamp, policy version, and expiration. This package does that by default.
Add when needed
Send `consentChanged` events to your backend if you need proof for regulated analytics, logged-in accounts, ad campaigns, or formal compliance evidence.
Example backend payload
window.addEventListener('consentChanged', (event) => {
fetch('/api/consent-events', {
method: 'POST',
headers: { 'content-type': 'application/json' },
body: JSON.stringify({
consent: event.detail.consent,
policyVersion: event.detail.consent.version,
page: location.pathname
})
});
});
Download
Use the package build or copy the source.
The build produces an ES module, UMD bundle, CSS, and TypeScript declarations. For local testing, run the commands below from this workspace.
npm install && npm run build