consent

Ask for cookie and tracking consent once, and hold analytics and error reports until people agree.

Analytics & errorsClient

Every export of kitr/consent, kitr/consent/react, kitr/consent/provider, read from the published type definitions. The guide shows them in use.

kitr/consent

createConsent

function createConsent(config?: ConsentConfig, app?: KitrApp): Consent;

useConsent

function useConsent(app: KitrApp): Consent;

defineConfig

const defineConfig: <const C extends ConsentConfig>(config: C) => C;
interface Consent {
  /** The record, with `decidedAt` unset while the banner should show. */
  get(): ConsentRecord;
  /** Has the visitor decided (for this version)? */
  decided(): boolean;
  has(category: ConsentCategory): boolean;
  /** Grant exactly these categories (plus `necessary`). */
  grant(categories: ConsentCategory[]): void;
  acceptAll(): void;
  /** Decline everything but `necessary`. */
  rejectAll(): void;
  /** Forget the choice so the banner shows again. */
  reset(): void;
  subscribe(listener: (record: ConsentRecord) => void): () => void;
  readonly categories: ConsentCategory[];
  readonly cookie: CookieItem<ConsentRecord>;
}

ConsentConfig

interface ConsentConfig {
  /** What the banner offers. Default `['analytics', 'marketing']`. */
  categories?: ConsentCategory[];
  /** Re-ask when this changes. Default 1. */
  version?: number;
  /** Cookie name. Default `consent`. */
  cookie?: string;
  /** Days the choice is kept. Default 365. */
  maxAge?: number;
  /** Which category analytics waits for. Default `analytics`; false never gates it. */
  analytics?: ConsentCategory | false;
  /** Which category error monitoring waits for. Default: none (monitoring runs; set `'analytics'` to gate it). */
  monitoring?: ConsentCategory | false;
}

ConsentRecord

interface ConsentRecord {
  /** Categories the visitor agreed to. `necessary` is always in. */
  granted: ConsentCategory[];
  /** ISO time of the choice; undefined until they chose. */
  decidedAt?: string;
  /** Bump `config.version` to ask everyone again. */
  version: number;
}

ConsentCategory

type ConsentCategory = 'necessary' | 'analytics' | 'marketing' | 'preferences' | (string & {});

kitr/consent/react

useConsent

function useConsent(): {
  record: ConsentRecord;
  decided: boolean;
  has: (category: ConsentCategory) => boolean;
  categories: ConsentCategory[];
  grant: Consent['grant'];
  acceptAll: () => void;
  rejectAll: () => void;
  reset: () => void;
};

The consent record and the actions a banner needs. Hydration-safe: the server and the first render see "undecided", so the banner renders once on the client after mount.

kitr/consent/provider

consentProvider

const consentProvider: import("@kitr/core").KitrProvider;

Binds consent and gates the trackers: analytics stays queued until its category is granted (and is disabled again when it is withdrawn); monitoring is gated only when config.monitoring names a category. Put the provider after the analytics and monitoring ones in kitrrc.ts.