Documentation

Complete reference for @zerakicreative/cookie-consent — installation, configuration, Google Analytics setup, translations, and the programmatic API.

1. Installation

npm

Terminal
npm install @zerakicreative/cookie-consent

CDN — jsDelivr

No build step required. Add this before your closing </body> tag:

HTML
<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.

HTML — paste before </body>
<!-- 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)

JavaScript / TypeScript
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:

React — app/layout.tsx or _app.tsx
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

OptionTypeDefaultDescription
companyNamestring"Our Website"Used in the modal heading and aria labels.
primaryColorstring"#2563EB"Accent colour for buttons and toggles. Accepts any CSS colour value.

Legal links

OptionTypeDefaultDescription
privacyPolicyUrlstring"#"Link shown in the banner and modal.
cookiePolicyUrlstring"#"Link shown in the banner and modal.

Appearance

OptionTypeDefaultDescription
themestring"light""light" · "dark" · "auto" (follows OS preference).
positionstring"bottom-bar"Where the banner appears. See Banner Positions.
showFloatingButtonbooleantrueShow a floating cookie icon after consent is given, so users can change preferences.
floatingButtonPositionstring"bottom-right""bottom-right" or "bottom-left".
customStylesstringInject additional CSS into the widget's shadow scope. Use to override colours, fonts, or spacing.

Behaviour

OptionTypeDefaultDescription
autoShowbooleantrueAutomatically show the banner on first visit. Set to false to trigger manually with .show().
autoDetectRegionbooleantrueDetect the user's regulation (GDPR, CCPA, etc.) from their timezone.
forcedRegionstringOverride auto-detection. See Region Codes.
cookieExpirynumber365Days before the saved consent expires and the banner reappears.
consentVersionstring"1"Bump this string whenever your cookie policy changes to force a re-prompt for all users.

Callbacks

OptionTypeDescription
onReady() => voidFires once the widget is mounted in the DOM.
onConsentChange(consent) => voidFires whenever the user saves a consent choice.
onAcceptAll(consent) => voidFires when the user clicks Accept All.
onRejectAll() => voidFires when the user clicks Reject All.

4. Banner Positions

ValueDescription
bottom-barFull-width sticky bar at the bottom of the viewport. Default.
top-barFull-width sticky bar at the top of the viewport.
bottom-leftFloating card, anchored to the bottom-left corner.
bottom-rightFloating card, anchored to the bottom-right corner.
bottom-centerFloating 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

HTML
ZerakiCookieBanner.init({
  googleAnalytics: {
    measurementId:      'G-XXXXXXXXXX',  // your GA4 Measurement ID
    consentModeVersion: 'v2',
    autoLoad:           true,              // injects the gtag.js script after consent
  },
});

GA4 — npm

JavaScript / TypeScript
new CookieBanner({
  googleAnalytics: {
    measurementId:      'G-XXXXXXXXXX',
    consentModeVersion: 'v2',
    autoLoad:           true,
  },
}).init();

Google Tag Manager — CDN

HTML
ZerakiCookieBanner.init({
  googleAnalytics: {
    tagManagerId:       'GTM-XXXXXXX',   // use tagManagerId instead of measurementId
    consentModeVersion: 'v2',
    autoLoad:           true,
  },
});

Google Tag Manager — npm

JavaScript / TypeScript
new CookieBanner({
  googleAnalytics: {
    tagManagerId:       'GTM-XXXXXXX',
    consentModeVersion: 'v2',
    autoLoad:           true,
  },
}).init();
OptionTypeDescription
measurementIdstringGA4 Measurement ID (e.g. G-XXXXXXXXXX).
tagManagerIdstringGTM Container ID (e.g. GTM-XXXXXXX). Use instead of measurementId.
consentModeVersionstring"v1" or "v2". v2 adds ad_user_data and ad_personalization signals.
autoLoadbooleanWhen 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

HTML
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

JavaScript / TypeScript
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().

CDN & npm — same pattern
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.

JavaScript
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();
KeyDescription
bannerTitleHeading in the consent banner.
bannerTextBody text in the consent banner.
acceptAllAccept All button label.
rejectAllReject All button label.
customizeManage Preferences button label.
savePreferencesSave button label in the preferences modal.
privacyPolicyPrivacy Policy link text.
cookiePolicyCookie Policy link text.
categories.necessaryName and description for the Necessary category.
categories.analyticsName and description for the Analytics category.
categories.marketingName and description for the Marketing category.
categories.preferencesName 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.

JavaScript
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)

The ConsentState object is passed to all callbacks and returned by getConsent().

TypeScript
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.

CodeRegulationBehaviour
gdprGDPR / UK GDPR (EU/EEA/UK)Full opt-in required. Banner shown on every first visit.
ccpaCCPA / CPRA (California)Opt-out model. "Do Not Sell" notice shown.
lgpdLGPD (Brazil)Opt-in consent required, similar to GDPR.
pdpaPDPA (Thailand / Singapore)Informed consent model.
pipedaPIPEDA (Canada)Implied consent with opt-out option.
popiaPOPIA (South Africa)Informed consent, opt-in required.
otherAll other regionsInformational banner, no strict opt-in enforcement.