Write a consent adapter
A consent adapter connects the script to your cookie banner. Use this page when your banner is not Cookiebot, CookieYes or OneTrust, and does not push Google Consent Mode v2.
Purposes
The script asks for consent for two purposes. Each purpose has its own state: unknown, granted or denied.
| Purpose | Data that it controls | Usual banner group |
|---|---|---|
analytics | The visitor ID and the identifiers of each event | Statistics, analytics or performance |
ads | Data for ad platforms, for example ad click IDs | Marketing or advertisement |
The script sends ad data only when analytics and ads are both granted.
Rules for a consent adapter
- Call
ctx.consentone time when the adapter starts, with the stored choice of the visitor. - Call
ctx.consentagain after each change of the choice. - Before the visitor makes a choice, do not call
ctx.consent. The state staysunknown. - Give the state
grantedto a purpose only when all its groups aregranted. - Give the state
deniedto a purpose when one of its groups isdenied. - Read the group names from
ctx.config.analyticsandctx.config.ads. Use a default when the site does not set them. - Do not send events or identifiers from a consent adapter.
Procedure
Write an object of the type
Adapter. This example reads a banner with the nameBannerKit:tsimport type { Adapter, ConsentState, Context, Purpose } from '@quickpipeaiab/lead-scoring/adapters'; type Choice = Record<string, boolean>; declare global { interface Window { BannerKit?: { choice(): Choice | null }; } } const DEFAULTS: Record<Purpose, string> = { analytics: 'statistics', ads: 'marketing' }; function groups(ctx: Context, purpose: Purpose): string[] { const value = ctx.config[purpose] ?? DEFAULTS[purpose]; return String(value).split(',').map((group) => group.trim()); } function state(choice: Choice, names: string[]): ConsentState { if (names.some((name) => choice[name] === false)) return 'denied'; if (names.every((name) => choice[name] === true)) return 'granted'; return 'unknown'; } export const bannerKit: Adapter = { name: 'bannerkit', detect: (win) => 'BannerKit' in win, start(ctx) { const analytics = groups(ctx, 'analytics'); const ads = groups(ctx, 'ads'); const report = () => { const choice = window.BannerKit?.choice(); if (!choice) return; ctx.consent({ analytics: state(choice, analytics), ads: state(choice, ads) }); }; report(); window.addEventListener('bannerkit:change', report); return () => window.removeEventListener('bannerkit:change', report); }, };Add the adapter to the client:
tslg.use(bannerKit);With the script tag, call
lg('use', bannerKit).Keep the default
consent: auto. Inmanualmode, the script ignores eachctx.consentcall.If your banner uses different group names, set them on the script tag:
html<script async src="…/lg.js" data-key="lg-pub-live-…" data-bannerkit-analytics="stats" data-bannerkit-ads="ads,remarketing"></script>
Result
Start debug mode with lg.debug(true). Accept and refuse the groups in your banner. The console shows each change of the state of a purpose.
Related pages
- Set consent tells how the script uses consent, and how to map the groups of a built-in adapter.
- Adapters gives the types of
AdapterandContext.