Documentation
Complete reference for @zerakicreative/cookie-consent — installation, configuration, Google Analytics setup, translations, and the programmatic API.
On this page
1. Installation
npm
npm install @zerakicreative/cookie-consent
CDN — jsDelivr
No build step required. Add this before your closing </body> tag:
<script src="https://cdn.jsdelivr.net/npm/@zerakicreative/cookie-consent@1.0.3/dist/zeraki-cookie-consent.iife.js"></script>
2. Quick Start
Option 1 — Script Tag (CDN)
The global ZerakiCookieBanner is available immediately after the script loads. No build step required.
<!-- 1. Load the widget --> <script src="https://cdn.jsdelivr.net/npm/@zerakicreative/cookie-consent@1.0.3/dist/zeraki-cookie-consent.iife.js"></script> <!-- 2. Initialise with your options --> <script> ZerakiCookieBanner.init({ // ── Branding ────────────────────────────────────── companyName: 'Your Company', primaryColor: '#2563EB', // any hex / RGB / HSL // ── Banner layout ───────────────────────────────── position: 'bottom-bar', // 'bottom-bar' | 'top-bar' | 'bottom-left' | 'bottom-right' floatingButtonPosition: 'bottom-right', // 'bottom-right' | 'bottom-left' // ── Policy links ────────────────────────────────── privacyPolicyUrl: '/privacy', cookiePolicyUrl: '/cookies', // ── Preferences text (all optional) ────────────── translations: { bannerTitle: 'We use cookies', bannerText: 'We use cookies to improve your experience.', acceptAll: 'Accept All', rejectAll: 'Reject All', customize: 'Preferences', savePreferences: 'Save Preferences', }, // ── Callbacks ───────────────────────────────────── onConsentChange: (consent) => console.log(consent), onAcceptAll: (consent) => console.log('Accepted', consent), onRejectAll: () => console.log('Rejected'), }); </script>
Option 2 — npm / ES Module (React / Vue / Svelte)
import { CookieBanner } from '@zerakicreative/cookie-consent'; new CookieBanner({ companyName: 'Your Company', primaryColor: '#2563EB', position: 'bottom-bar', floatingButtonPosition: 'bottom-right', privacyPolicyUrl: '/privacy', cookiePolicyUrl: '/cookies', onConsentChange: (consent) => console.log(consent), }).init();
React
Call .init() inside a useEffect so it runs after the DOM mounts:
import { useEffect } from 'react'; import { CookieBanner } from '@zerakicreative/cookie-consent'; export default function App() { useEffect(() => { const banner = new CookieBanner({ primaryColor: '#2563EB', privacyPolicyUrl: '/privacy' }).init(); return () => banner.destroy(); }, []); }
3. Configuration Reference
All options are optional. Pass them as a plain object to new CookieBanner({ ... }) or ZerakiCookieBanner.init({ ... }).
Branding
| Option | Type | Default | Description |
|---|---|---|---|
companyName | string | "Our Website" | Used in the modal heading and aria labels. |
primaryColor | string | "#2563EB" | Accent colour for buttons and toggles. Accepts any CSS colour value. |
Legal links
| Option | Type | Default | Description |
|---|---|---|---|
privacyPolicyUrl | string | "#" | Link shown in the banner and modal. |
cookiePolicyUrl | string | "#" | Link shown in the banner and modal. |
Appearance
| Option | Type | Default | Description |
|---|---|---|---|
theme | string | "light" | "light" · "dark" · "auto" (follows OS preference). |
position | string | "bottom-bar" | Where the banner appears. See Banner Positions. |
showFloatingButton | boolean | true | Show a floating cookie icon after consent is given, so users can change preferences. |
floatingButtonPosition | string | "bottom-right" | "bottom-right" or "bottom-left". |
customStyles | string | — | Inject additional CSS into the widget's shadow scope. Use to override colours, fonts, or spacing. |
Behaviour
| Option | Type | Default | Description |
|---|---|---|---|
autoShow | boolean | true | Automatically show the banner on first visit. Set to false to trigger manually with .show(). |
autoDetectRegion | boolean | true | Detect the user's regulation (GDPR, CCPA, etc.) from their timezone. |
forcedRegion | string | — | Override auto-detection. See Region Codes. |
cookieExpiry | number | 365 | Days before the saved consent expires and the banner reappears. |
consentVersion | string | "1" | Bump this string whenever your cookie policy changes to force a re-prompt for all users. |
Callbacks
| Option | Type | Description |
|---|---|---|
onReady | () => void | Fires once the widget is mounted in the DOM. |
onConsentChange | (consent) => void | Fires whenever the user saves a consent choice. |
onAcceptAll | (consent) => void | Fires when the user clicks Accept All. |
onRejectAll | () => void | Fires when the user clicks Reject All. |
4. Banner Positions
| Value | Description |
|---|---|
bottom-bar | Full-width sticky bar at the bottom of the viewport. Default. |
top-bar | Full-width sticky bar at the top of the viewport. |
bottom-left | Floating card, anchored to the bottom-left corner. |
bottom-right | Floating card, anchored to the bottom-right corner. |
bottom-center | Floating card, centred at the bottom of the viewport. |
5. Google Analytics & GTM Integration
There are three ways to integrate analytics — choose the one that matches your setup.
Option 1 — Built-in Google Consent Mode v2 (recommended)
Pass a googleAnalytics object and the banner handles all Consent Mode signalling automatically. Works for both CDN and npm.
GA4 — CDN
ZerakiCookieBanner.init({ googleAnalytics: { measurementId: 'G-XXXXXXXXXX', // your GA4 Measurement ID consentModeVersion: 'v2', autoLoad: true, // injects the gtag.js script after consent }, });
GA4 — npm
new CookieBanner({ googleAnalytics: { measurementId: 'G-XXXXXXXXXX', consentModeVersion: 'v2', autoLoad: true, }, }).init();
Google Tag Manager — CDN
ZerakiCookieBanner.init({ googleAnalytics: { tagManagerId: 'GTM-XXXXXXX', // use tagManagerId instead of measurementId consentModeVersion: 'v2', autoLoad: true, }, });
Google Tag Manager — npm
new CookieBanner({ googleAnalytics: { tagManagerId: 'GTM-XXXXXXX', consentModeVersion: 'v2', autoLoad: true, }, }).init();
| Option | Type | Description |
|---|---|---|
measurementId | string | GA4 Measurement ID (e.g. G-XXXXXXXXXX). |
tagManagerId | string | GTM Container ID (e.g. GTM-XXXXXXX). Use instead of measurementId. |
consentModeVersion | string | "v1" or "v2". v2 adds ad_user_data and ad_personalization signals. |
autoLoad | boolean | When true, the widget injects the GA4/GTM script tag automatically after consent is established. |
Option 2 — Manual gtag.js via callbacks
Use this when you already have gtag.js loaded on the page and want to send consent updates yourself based on what the user chooses.
CDN
ZerakiCookieBanner.init({ onAcceptAll: (consent) => { gtag('consent', 'update', { analytics_storage: 'granted', ad_storage: 'granted', ad_user_data: 'granted', ad_personalization: 'granted', }); }, onRejectAll: () => { gtag('consent', 'update', { analytics_storage: 'denied', ad_storage: 'denied', ad_user_data: 'denied', ad_personalization: 'denied', }); }, onConsentChange: (consent) => { gtag('consent', 'update', { analytics_storage: consent.analytics ? 'granted' : 'denied', ad_storage: consent.marketing ? 'granted' : 'denied', ad_user_data: consent.marketing ? 'granted' : 'denied', ad_personalization: consent.marketing ? 'granted' : 'denied', }); }, });
npm
new CookieBanner({ onAcceptAll: (consent) => { gtag('consent', 'update', { analytics_storage: 'granted', ad_storage: 'granted', ad_user_data: 'granted', ad_personalization: 'granted', }); }, onRejectAll: () => { gtag('consent', 'update', { analytics_storage: 'denied', ad_storage: 'denied', ad_user_data: 'denied', ad_personalization: 'denied', }); }, onConsentChange: (consent) => { gtag('consent', 'update', { analytics_storage: consent.analytics ? 'granted' : 'denied', ad_storage: consent.marketing ? 'granted' : 'denied', ad_user_data: consent.marketing ? 'granted' : 'denied', ad_personalization: consent.marketing ? 'granted' : 'denied', }); }, }).init();
Option 3 — Google Tag Manager dataLayer
Push consent events to the GTM dataLayer so your GTM tags and triggers can fire based on what the user chose. Works identically for CDN and npm — just swap ZerakiCookieBanner.init for new CookieBanner({...}).init().
ZerakiCookieBanner.init({ // or: new CookieBanner({...}).init() onAcceptAll: (consent) => { window.dataLayer = window.dataLayer || []; window.dataLayer.push({ event: 'cookie_consent_accepted', consent_analytics: 'granted', consent_marketing: 'granted', consent_preferences: 'granted', }); }, onRejectAll: () => { window.dataLayer = window.dataLayer || []; window.dataLayer.push({ event: 'cookie_consent_rejected', consent_analytics: 'denied', consent_marketing: 'denied', consent_preferences: 'denied', }); }, onConsentChange: (consent) => { window.dataLayer = window.dataLayer || []; window.dataLayer.push({ event: 'cookie_consent_update', consent_analytics: consent.analytics ? 'granted' : 'denied', consent_marketing: consent.marketing ? 'granted' : 'denied', consent_preferences: consent.preferences ? 'granted' : 'denied', }); }, });
In GTM, create a Custom Event trigger listening for cookie_consent_accepted, cookie_consent_rejected, and cookie_consent_update. Your tags can then read {{dlv - consent_analytics}} etc. as Data Layer Variables.
6. Custom Translations
Override any piece of UI text by passing a partial translations object. All keys are optional — unspecified keys fall back to the English defaults.
new CookieBanner({ translations: { bannerTitle: 'We value your privacy', bannerText: 'We use cookies to improve your experience.', acceptAll: 'Accept all', rejectAll: 'Reject all', customize: 'Manage preferences', savePreferences: 'Save preferences', privacyPolicy: 'Privacy Policy', cookiePolicy: 'Cookie Policy', categories: { analytics: { name: 'Analytics', description: 'Help us understand how visitors use our site.', }, marketing: { name: 'Marketing', description: 'Used to deliver personalised ads.', }, }, }, }).init();
| Key | Description |
|---|---|
bannerTitle | Heading in the consent banner. |
bannerText | Body text in the consent banner. |
acceptAll | Accept All button label. |
rejectAll | Reject All button label. |
customize | Manage Preferences button label. |
savePreferences | Save button label in the preferences modal. |
privacyPolicy | Privacy Policy link text. |
cookiePolicy | Cookie Policy link text. |
categories.necessary | Name and description for the Necessary category. |
categories.analytics | Name and description for the Analytics category. |
categories.marketing | Name and description for the Marketing category. |
categories.preferences | Name and description for the Preferences category. |
7. Programmatic API
new CookieBanner(config) returns an instance with the following methods. Every method except getConsent() returns this for chaining.
const banner = new CookieBanner({ ... }).init(); banner.show(); // Show the consent banner banner.hide(); // Hide the banner banner.showModal(); // Open the preferences modal banner.hideModal(); // Close the preferences modal banner.acceptAll(); // Accept all cookie categories banner.rejectAll(); // Reject all non-essential categories banner.getConsent(); // → ConsentState | null banner.destroy(); // Remove the widget from the DOM (cleanup)
8. Consent Object
The ConsentState object is passed to all callbacks and returned by getConsent().
interface ConsentState { necessary: boolean; // always true analytics: boolean; marketing: boolean; preferences: boolean; timestamp: string; // ISO 8601 version: string; // matches consentVersion config region?: RegionCode; }
9. Region & Regulation Codes
Used with forcedRegion and returned in the consent object's region field.
| Code | Regulation | Behaviour |
|---|---|---|
gdpr | GDPR / UK GDPR (EU/EEA/UK) | Full opt-in required. Banner shown on every first visit. |
ccpa | CCPA / CPRA (California) | Opt-out model. "Do Not Sell" notice shown. |
lgpd | LGPD (Brazil) | Opt-in consent required, similar to GDPR. |
pdpa | PDPA (Thailand / Singapore) | Informed consent model. |
pipeda | PIPEDA (Canada) | Implied consent with opt-out option. |
popia | POPIA (South Africa) | Informed consent, opt-in required. |
other | All other regions | Informational banner, no strict opt-in enforcement. |