Programming Cookiebot

The Cookiebot script loads and constructs a client side JavaScript object named 'Cookiebot' which exposes the following public properties, methods, events and callback functions. See the tutorial on how to deploy the Cookiebot script to your website template.
Properties
Name Type Default Description
consent.necessary bool true True if current user has accepted necessary cookies.
The property is read only.
consent.preferences bool false True if current user has accepted preference cookies.
The property is read only.
consent.statistics bool false True if current user has accepted statistics cookies.
The property is read only.
consent.marketing bool false True if current user has accepted marketing cookies.
The property is read only.
consented bool false True if the user has accepted cookies.
The property is read only.
declined bool false True if the user has declined the use of cookies.
The property is read only.
hasResponse bool false True if the user has responded to the dialog with either 'accept' or 'decline'.
The property is read only.
doNotTrack bool false True if the user has enabled the web browser's 'Do not track' (DNT) setting. If DNT is enabled Cookiebot will not set the third party cookie CookieConsentBulkTicket used for bulk consent.
The property is read only.
Methods
Name Parameters Description
show None Forces the cookie consent dialog to show.
hide None Forces the cookie consent dialog to hide.
renew None Deletes any registered consent in the website user's browser and shows the cookie consent dialog to the website user to renew or change the user's consent state.
getScript string URL,
bool async,
func callback
Loads a JavaScript-file. URL: Absolute path to the JavaScript-file.
async (optional): Determines if the script should load asynchronously.
callback (optional): JavaScript function to execute when the script has finished loading.
runScripts None Evaluates all loaded script tags of type "text/plain" with the attribute "data-cookieconsent" and executes the scripts in accordance with the user's consent state. For use by websites that load content dynamically, e.g. in single page applications. Scripts are only executed once, so this function is safe to call several times, e.g. every time new content is loaded.
withdraw None Withdraw my consent for this website.
Example: Create link that makes it possible for the user to change or renew a cookie consent
<a href=”javascript: Cookiebot.renew()”>Renew or change your cookie consent</a>
Callbacks (Asynchronous event handling)
Name Description
CookiebotCallback_OnLoad The asynchronous callback is triggered when the cookie banner has loaded to get the user's consent.
CookiebotCallback_OnAccept The asynchronous callback is triggered when the user clicks the accept-button of the cookie consent dialog and whenever a consented user loads a page.
CookiebotCallback_OnDecline The asynchronous callback is triggered when the user declines the use of cookies by clicking the decline-button in the cookie consent dialog. The callback is also triggered whenever a user that has declined the use of cookies loads a page.
Example: Load Google Analytics if the user has accepted Cookies
<script type="text/javascript">

    function CookiebotCallback_OnAccept() {
        if (Cookiebot.consent.statistics)
        {
            enableGoogleAnalytics();
        }
    }

    var _gaq = [];
    function enableGoogleAnalytics() {
        _gaq.push(['_setAccount', 'UA-00000000-0']);
        _gaq.push(['_trackPageview']);
        (function () {
            var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
            ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
            var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
        })();
    }

</script>
Script tag data attributes
Name Type Mandatory Description
data-cbid string yes The unique ID for your Cookiebot subscription.
data-type string no Overrides the default dialog type with one of the following values:
"optin", "optout", "optinout", "leveloptin", "inlineoptin"
data-level string no Overrides the default consent method with one of the following values: "implied", "strict"
data-culture string no To force a specific language variant of the consent dialog, set the value of this attribute to a culture neutral ISO 639-1 language code, e.g. "EN" for English. Setting this attribute with a valid language overrides the "Auto-detect user language" setting in the Cookiebot manager.
Example: Force the consent dialog to show in English
<script id="Cookiebot" data-culture="EN" src="https://consent.cookiebot.com/uc.js" data-cbid="00000000-0000-0000-0000-000000000000" type="text/javascript"></script>

Server side usage

If your website is setting cookies server side, you can determine what type of cookies you are allowed to set by checking the users current consent state through standard cookie handling.

using System.Web.Script.Serialization;
...

HttpCookie CurrentUserConsent = Request.Cookies["CookieConsent"];

if (CurrentUserConsent != null)
{
    switch (CurrentUserConsent.Value)
    {
        case "0":
            //The user has not accepted cookies - set strictly necessary cookies only
            break;

        case "-1":
            //The user is not within a region that requires consent - all cookies are accepted
            break;

        default: //The user has accepted one or more type of cookies
            
            //Read current user consent in encoded JavaScript format
            JavaScriptSerializer CookieConsentSerializer = new JavaScriptSerializer();
            dynamic CookieConsent = CookieConsentSerializer.Deserialize<object>(HttpUtility.UrlDecode(CurrentUserConsent.Value));

            if (CookieConsent["preferences"])
            {
                //Current user accepts preference cookies
            }
            else
            {
                //Current user does NOT accept preference cookies
            }

            if (CookieConsent["statistics"])
            {
                //Current user accepts statistics cookies
            }
            else
            {
                //Current user does NOT accept statistics cookies
            }

            if (CookieConsent["marketing"])
            {
                //Current user accepts marketing cookies
            }
            else
            {
                //Current user does NOT accept marketing cookies
            }   
    }
}
else
{
    //The user has not accepted cookies - set strictly necessary cookies only
}
if (isset($_COOKIE["CookieConsent"]))
{
    switch ($_COOKIE["CookieConsent"])
    {
        case "0":
            //The user has not accepted cookies - set strictly necessary cookies only
            break;

        case "-1":
            //The user is not within a region that requires consent - all cookies are accepted
            break;

        default: //The user has accepted one or more type of cookies
            
            //Read current user consent in encoded JavaScript format
            $valid_php_json = preg_replace('/\s*:\s*([a-zA-Z0-9_]+?)([}\[,])/', ':"$1"$2', preg_replace('/([{\[,])\s*([a-zA-Z0-9_]+?):/', '$1"$2":', str_replace("'", '"',stripslashes($_COOKIE["CookieConsent"]))));
            $CookieConsent = json_decode($valid_php_json);

            if (filter_var($CookieConsent->preferences, FILTER_VALIDATE_BOOLEAN))
            {
                //Current user accepts preference cookies
            }
            else
            {
                //Current user does NOT accept preference cookies
            }

            if (filter_var($CookieConsent->statistics, FILTER_VALIDATE_BOOLEAN))
            {
                //Current user accepts statistics cookies
            }
            else
            {
                //Current user does NOT accept statistics cookies
            }

            if (filter_var($CookieConsent->marketing, FILTER_VALIDATE_BOOLEAN))
            {
                //Current user accepts marketing cookies
            }
            else
            {
                //Current user does NOT accept marketing cookies
            }   
    }
}
else
{
    //The user has not accepted cookies - set strictly necessary cookies only
}
Imports System.Web.Script.Serialization
...

Dim CurrentUserConsent As HttpCookie = Request.Cookies("CookieConsent")

If CurrentUserConsent IsNot Nothing Then
    Select Case CurrentUserConsent.Value
        Case "0"
            REM The user has not accepted cookies - set strictly necessary cookies only
            Exit Select

        Case "-1"
            REM The user is not within a region that requires consent - all cookies are accepted
            Exit Select

        Case Else REM The user has accepted one or more type of cookies
            
            REM Read current user consent in encoded JavaScript format
            Dim CookieConsentSerializer As New JavaScriptSerializer()
            Dim CookieConsent As dynamic = CookieConsentSerializer.Deserialize(Of Object)(HttpUtility.UrlDecode(CurrentUserConsent.Value))

            If CookieConsent("preferences") Then
                REM Current user accepts preference cookies
            Else
                REM Current user does NOT accept preference cookies
            End If

            If CookieConsent("statistics") Then
                REM Current user accepts statistics cookies
            Else
                REM Current user does NOT accept statistics cookies
            End If

            If CookieConsent("marketing") Then
                REM Current user accepts marketing cookies
            Else
                REM Current user does NOT accept marketing cookies
            End If
    End Select
Else
    REM The user has not accepted cookies - set strictly necessary cookies only
End If


Loading...
Cookie Declaration preview
The final declaration will be shown as embedded content in your own website. The content is automatically formatted with your website's overall style sheet definitions (CSS), including fonts and colors.
Austria20.00 %
Belgium21.00 %
Bulgaria20.00 %
Croatia25.00 %
Cyprus19.00 %
Czech Republic21.00 %
Denmark25.00 %
Estonia20.00 %
Finland24.00 %
France20.00 %
Germany19.00 %
Greece24.00 %
Hungary27.00 %
Ireland23.00 %
Italy22.00 %
Latvia21.00 %
Lithuania21.00 %
Luxembourg17.00 %
Malta18.00 %
Netherlands21.00 %
Poland23.00 %
Portugal23.00 %
Romania20.00 %
Slovakia20.00 %
Slovenia22.00 %
Spain21.00 %
Sweden25.00 %
United Kingdom20.00 %
Top