Public JavaScript API for CookieKit utility methods and global namespace usage.
CookieKit supports two usage styles:
window.CookieKit.Use module imports when your bundler supports package imports. Use window.CookieKit when loading CookieKit from an external script source.
import {
readConsent,
hasConsent,
onConsentChanged,
openConsentDialog,
onConsentDialogClosed,
getSubscriptionStatus,
} from 'cookiekit';const api = window.CookieKit;
if (api?.hasConsent('analytics')) {
// analytics consent granted
}
const unsubscribe = api?.onConsentChanged(consent => {
console.log('Consent changed', consent);
});readConsent(): {
analytics: boolean;
marketing: boolean;
preferences: boolean;
} | nullReturns null when no valid consent cookie exists.
hasConsent(expression?: string): boolean
Expression rules:
analytics, marketing, or preferences.analytics+marketing.analytics|marketing.onConsentChanged(callback: (consent: {
analytics: boolean;
marketing: boolean;
preferences: boolean;
} | null) => void): () => voidReturns an unsubscribe function.
openConsentDialog(): void
onConsentDialogClosed(callback: () => void): () => void
Returns an unsubscribe function.
getSubscriptionStatus(): {
status: string | null;
billingInterval: string | null;
subscriptionEnd: string | null;
trailingEnd: string | null;
} | nullwindow.CookieKit = {
readConsent,
hasConsent,
onConsentChanged,
openConsentDialog,
onConsentDialogClosed,
getSubscriptionStatus,
subscriptionStatus,
};subscriptionStatus is optional and may be null when unavailable.
cookiekit:consent-changedcookiekit:opencookiekit:dialog-closed'use client';
import { useEffect } from 'react';
export default function Example() {
useEffect(() => {
const stop = window.CookieKit?.onConsentChanged(consent => {
console.log(consent);
});
return () => {
stop?.();
};
}, []);
return null;
}