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
acceptAll(): voidAccepts 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
// Wire up a one-click "Accept All" button in your own UI
document.getElementById('accept-cookies').addEventListener('click', () => {
lynkow.consent.acceptAll()
})destroy
destroy(): voidClean up every DOM resource this service owns: removes the banner
and preferences modal, removes injected third-party scripts, and
stops the theme observer. Call when unmounting the SDK (e.g. inside
a React useEffect cleanup) so the page can be navigated without
leaking listeners. No-op on server.
Returns: void
useEffect(() => {
lynkow.consent.show()
return () => lynkow.consent.destroy()
}, [])getCategories
getCategories(): ConsentCategoriesReturns 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
// Read current preferences to conditionally load a third-party widget
const prefs = lynkow.consent.getCategories()
if (prefs.marketing) {
loadChatWidget()
}getConfig
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>
const config = await lynkow.consent.getConfig()
if (config.enabled) {
console.log('Categories:', config.categories.map((c) => c.id))
}hasConsented
hasConsented(): booleanChecks 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
// Only show the consent banner if the user hasn't already decided
if (!lynkow.consent.hasConsented()) {
lynkow.consent.show()
}hide
hide(): voidHide and remove the consent banner from the DOM. Does not affect stored consent preferences (the user will not be re-prompted on the next page load if they already chose). No-op on server or if the banner is not currently shown.
Returns: void
// Close the banner after the user clicked "Accept all" via custom UI:
lynkow.consent.hide()logConsent
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 |
|---|---|---|
|
| Consent preferences as a record of category IDs to booleans<br> (e.g. |
|
| Optional explicit action type. If omitted, the action is inferred<br> from the preferences (all true = |
Returns: Promise<void>
await lynkow.consent.logConsent(
{ necessary: true, analytics: true, marketing: false },
'customize'
)rejectAll
rejectAll(): voidRejects 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
// Reject all non-essential cookies from a custom banner
document.getElementById('reject-cookies').addEventListener('click', () => {
lynkow.consent.rejectAll()
})reset
reset(): voidResets 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
// Add a "Manage cookies" link in the footer to let users re-choose
document.getElementById('manage-cookies').addEventListener('click', () => {
lynkow.consent.reset()
})setCategories
setCategories(categories: Partial<ConsentCategories>): voidSets 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 |
|---|---|---|
|
| Partial consent categories to update. Unspecified categories<br> retain their current value. Example: |
Returns: void
// Save custom preferences from a preferences form
lynkow.consent.setCategories({
analytics: true,
marketing: false,
preferences: true,
})show
show(): voidShows 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
// Show the consent banner on page load (skips if already consented)
const lynkow = createClient({ siteId: '...' })
lynkow.consent.show()showPreferences
showPreferences(): voidOpen the preferences modal so the user can 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 or if the
modal is already mounted.
Returns: void
<button onClick={() => lynkow.consent.showPreferences()}>
Cookie settings
</button>