This article explains how to implement CookieBot with Umbraco Engage to comply with GDPR.
Integrating a cookie consent banner service such as CookieBot allows you to configure parts of Umbraco Engage based on user consent.
This article gives you a working implementation to use with CookieBot.
Code Example
The code example below shows how to create the backend code to read the CookieBot consent cookie from the end user. Based on that, decide which features of Umbraco Engageit should enable or disable.
Create a class that implements the Umbraco.Engage.Business.Permissions.ModulePermissions.IModulePermissions interface.
Check the current HTTPContext Request Cookies for the CookieBot cookie which is named CookieConsent.
From some of the documentation from CookieBot, implement the same logic to check if the value of the cookie is -1 or another value. If it is set to -1, CookieBot is indicating to us that this is a user within a region that does not require consent.
The rest of the code is deserializing the JSON string stored inside the cookie from CookieBot. It maps to the relevant cookie permission used for turning Umbraco Engage features on or off.
CookieBotModulePermissions.cs
usingMicrosoft.AspNetCore.Http;usingNewtonsoft.Json;usingSystem.Web;usingUmbraco.Engage.Business.Permissions.ModulePermissions;namespaceUmbraco.Engage.StarterKit.CookieBot{publicclassCookieBotModulePermissions:IModulePermissions {publicboolAbTestingIsAllowed(HttpContext context) { // Need to check CookieBot consent cookie // Did they consent to AB testingreturnIsAllowed(context,"marketing"); }publicboolAnalyticsIsAllowed(HttpContext context) { // Need to check CookieBot consent cookie // Did they consent to AnalyticsreturnIsAllowed(context,"statistics"); }publicboolPersonalizationIsAllowed(HttpContext context) { // Need to check CookieBot consent cookie // Did they consent to PersonalizationreturnIsAllowed(context,"preferences"); }publicboolIsAllowed(HttpContext context,string cookiePermission) { // C# Code from CookieBot to check for their cookie // https://www.cookiebot.com/en/developer/#h-server-side-usagevar rawCookieBotConsentValues =context.Request.Cookies["CookieConsent"];if (rawCookieBotConsentValues !=null) {switch (rawCookieBotConsentValues) {case"-1": // The user is not within a region that requires consent - all cookies are accepted // Then we can mark Umbraco Engage features as allowedreturntrue;default: // The user has given their consentreturnCheckCookieBotValue(rawCookieBotConsentValues, cookiePermission); } } //The user has not accepted cookies - set strictly necessary cookies only returnfalse; }publicboolCheckCookieBotValue(string rawCookieBotConsentValues,string cookiePermissionToCheck) { // Read current user consent in encoded JSON // Sample JSON cookie payload/* * { * stamp:'Ov4gD1JVnDnBaJv8K2wYQlyWlnNlT/AKO768tibZYdQGNj/EolraLw==', * necessary:true, * preferences:false, * statistics:true, * marketing:false, * method:'explicit', * ver:1, * utc:1698057791350, * region:'gb' * } */ // Decode the consent stringvar decodedConsent =HttpUtility.UrlDecode(rawCookieBotConsentValues);if(decodedConsent ==null) {returnfalse; } // Deserialize the consent to a dynamic objectvar cookieBotConsentValues =JsonConvert.DeserializeObject(decodedConsent);if (cookieBotConsentValues ==null) { // Something went wrong with the cookieConsent deserializationreturnfalse; }switch (cookiePermissionToCheck) {case"necessary":returncookieBotConsentValues.Necessary;case"preferences":returncookieBotConsentValues.Preferences;case"statistics":returncookieBotConsentValues.Statistics;case"marketing":returncookieBotConsentValues.Marketing;default:break; }returnfalse; } }publicclassCookieBotConsent { [JsonProperty("necessary")]publicbool Necessary { get; set; } [JsonProperty("preferences")]publicbool Preferences { get; set; } [JsonProperty("statistics")]publicbool Statistics { get; set; } [JsonProperty("marketing")]publicbool Marketing { get; set; } }}
The existing CookieBot cookie Keys are mapped to the following Umbraco Engage features:
CookieBot Key
Umbraco Engage Features
Preferences
Personalization
Statistics
Analytics
Marketing
A/B Testing
Configuring CookieBot
For information on setting up and configuring your Cookie Consent Banner, see the Cookiebot Documentation. It contains information on changing the wording and the look and feel of the cookie consent banner.
Installing CookieBot
To install CookieBot, insert the JavaScript tag provided by CookieBot into the <head> of your HTML template:
Umbraco Engage does not actively track visitors until they have given their consent to the Cookiebot configuration. After the visitor consents, you need to reload the page to track the visit. If no reload is performed the visitor's referrer and/or campaign information will not be tracked.
Use JavaScript to reload the page when consent is given by handling the CookiebotOnAccept event:
window.location.reload();
Calling the above method will preserve any referrers and query strings supplied in the current request. It results in Umbraco Engage processing the current page visit and visitor correctly.