Retrieving Consent Data Using getCkyConsent() API
Last updated on March 14, 2024
Overview
CookieYes has introduced a new API function, getCkyConsent()
allowing you to access consent data sorted by consent types easily. This function returns an object containing details about:
- Your consent choices for different cookie categories (necessary, functional, analytics, advertisement, etc.)
- Whether you have interacted with a cookie consent banner.
- Your unique consent ID.
- The active consent law (like GDPR) applicable to the website.
- The language code of the displayed banner
Using getCkyConsent()
Call getCkyConsent()
to retrieve the consent data:
getCkyConsent();
It returns an object containing the consent data:
{ "activeLaw": "gdpr", "categories": { "necessary": true, "functional": false, "analytics": false, "performance": false, "advertisement": false }, "isUserActionCompleted": true, "consentID": "bFl2eFJBN05VS21WNWk5enpRNTNEZ3hxR0dDc3VmcFM", "languageCode": "en" }
The key properties are:
Property | Type | Description |
activeLaw | string | The legal framework under which the consent is active. |
categories | object | User consent status for different categories. |
isUserActionCompleted | boolean | Indicates whether the user interacted with the banner. |
consentID | string | Unique identifier for the user’s consent. |
languageCode | string | ISO-639-1 code of the displayed banner language. |
The getCkyConsent() function will not retrieve consent data if the CookieYes banner is disabled or the website is suspended. In such cases, the API will not be available.
Usage Example
Check if the user has accepted analytics cookies:
if (window.getCkyConsent) { const consent = getCkyConsent(); if (consent.categories.analytics) { // Load analytics cookies } else { // User has not accepted analytics cookies } }
Check if the user has completed an action on the banner:
if (getCkyConsent().isUserActionCompleted) { // User has made a choice } else { // Show banner to get user consent }