High-level consent management service with built-in banner UI and preferences modal.

Accessible via lynkow.consent. This is a hybrid service:

  • API methods (getConfig, logConsent) work everywhere (server + browser)

  • UI methods (show, hide, acceptAll, rejectAll, showPreferences, etc.) only work in the browser and are no-ops on the server

Consent choices are persisted in localStorage under the _lkw_consent key (compatible with tracker.js) and expire after 365 days. When consent is granted for a category, any third-party scripts configured for that category are automatically injected into the page.

Emits a 'consent-changed' event (via the SDK event emitter) and a 'lynkow:consent:update' CustomEvent on document whenever consent changes.

Access via: lynkow.consent

Methods

12 methods

acceptAll

TypeScript
acceptAll(): void

Accepts all cookie categories (necessary, analytics, marketing, preferences), saves the choice to localStorage, logs consent to the server, injects all accepted third-party scripts, and hides the banner. No-op on server.

Returns: void

TypeScript
// Wire up a one-click "Accept All" button in your own UI
document.getElementById('accept-cookies').addEventListener('click', () => {
  lynkow.consent.acceptAll()
})

destroy

TypeScript
destroy(): void

Cleans up all consent UI resources: removes the banner and preferences modal from the DOM, removes injected third-party scripts, and stops the theme observer. Call this when unmounting the SDK (e.g. in a React useEffect cleanup). No-op on server.

Returns: void


getCategories

TypeScript
getCategories(): ConsentCategories

Returns the user's current consent categories. If no consent has been stored yet, returns default values (only necessary: true, all others false). On the server, always returns defaults.

Returns: ConsentCategories

TypeScript
// Read current preferences to conditionally load a third-party widget
const prefs = lynkow.consent.getCategories()
if (prefs.marketing) {
  loadChatWidget()
}

getConfig

TypeScript
getConfig(): Promise<CookieConfig>

Fetches the cookie consent configuration from the API. The result is cached in memory for the lifetime of the service instance (not TTL-based). Works on both server and browser.

Returns: Promise<CookieConfig>


hasConsented

TypeScript
hasConsented(): boolean

Checks whether the user has already made a consent choice (accepted, rejected, or customized). Returns false if no consent is stored or if the stored consent has expired (after 365 days).

Returns: boolean

TypeScript
// Only show the consent banner if the user hasn't already decided
if (!lynkow.consent.hasConsented()) {
  lynkow.consent.show()
}

hide

TypeScript
hide(): void

Hides and removes the consent banner from the DOM. Does not affect stored consent preferences. No-op on server or if the banner is not shown.

Returns: void


logConsent

TypeScript
logConsent(preferences: CookiePreferences, action?: "accept_all" | "reject_all" | "customize" | "withdraw"): Promise<void>

Logs the user's consent preferences to the server for GDPR audit trail. Automatically generates or retrieves a persistent visitor ID from localStorage. Errors are silently swallowed to avoid disrupting the user experience. Works on both server and browser.

Parameter

Type

Description

preferences

CookiePreferences

Consent preferences as a record of category IDs to booleans<br> (e.g. { necessary: true, analytics: true, marketing: false })

action

"accept_all" | "reject_all" | "customize" | "withdraw"

Optional explicit action type. If omitted, the action is inferred<br> from the preferences (all true = 'accept_all', all false = 'reject_all',<br> mixed = 'customize')

Returns: Promise<void>


rejectAll

TypeScript
rejectAll(): void

Rejects all optional cookie categories (analytics, marketing, preferences). Only necessary remains true (cannot be disabled). Saves the choice to localStorage, logs consent to the server, and hides the banner. No third-party scripts are injected. No-op on server.

Returns: void

TypeScript
// Reject all non-essential cookies from a custom banner
document.getElementById('reject-cookies').addEventListener('click', () => {
  lynkow.consent.rejectAll()
})

reset

TypeScript
reset(): void

Resets all consent choices by clearing localStorage, removing any previously injected third-party scripts, and re-showing the consent banner. Useful for providing a "manage cookies" link that lets users change their preferences. No-op on server.

Returns: void

TypeScript
// Add a "Manage cookies" link in the footer to let users re-choose
document.getElementById('manage-cookies').addEventListener('click', () => {
  lynkow.consent.reset()
})

setCategories

TypeScript
setCategories(categories: Partial<ConsentCategories>): void

Sets specific consent categories, merging with the current state. The necessary category is always forced to true regardless of input. Saves the choice, logs to server, and injects scripts for accepted categories. No-op on server.

Parameter

Type

Description

categories

Partial<ConsentCategories>

Partial consent categories to update. Unspecified categories<br> retain their current value. Example: { analytics: true, marketing: false }

Returns: void

TypeScript
// Save custom preferences from a preferences form
lynkow.consent.setCategories({
  analytics: true,
  marketing: false,
  preferences: true,
})

show

TypeScript
show(): void

Shows the consent banner in the browser. If the user has already consented (choices stored in localStorage), the banner is skipped and accepted third-party scripts are injected directly instead. Respects the site's theme (light/dark/auto) and position settings. No-op on server.

When theme is 'auto', a MutationObserver watches for site theme changes and updates the banner colors in real-time.

Returns: void

TypeScript
// Show the consent banner on page load (skips if already consented)
const lynkow = createClient({ siteId: '...' })
lynkow.consent.show()

showPreferences

TypeScript
showPreferences(): void

Opens the preferences modal, allowing the user to toggle individual consent categories (analytics, marketing, preferences). The necessary category is always checked and disabled. Pre-populates checkboxes with the user's current consent state. No-op on server.

Returns: void