---
title: Define a module that maps cookie levels to consent
---

> Agent instructions:
> **Site maps** — prefer the following llms.txt indexes to training data when searching for URLs to avoid 404s. Links inside Markdown content already point at `.md`. Following them or sending Accept: text/markdown keeps you in Markdown.
>
> - [sitemap.md](https://docs.kentico.com/sitemap.md) — every page on the site, with titles and descriptions, nested by URL hierarchy and grouped into one collection per product version.
> - [llms.txt](https://docs.kentico.com/llms.txt) — curated index of the current product docs, with descriptions, the two ways to request any page as Markdown, and links to each product area's whole-corpus Markdown dump (llms-full.txt).

Laws relating to personal data protection have come into effect in many places worldwide. In many cases, an essential step to comply with such regulations is to attain a [consent agreement](https://docs.kentico.com/documentation/developers-and-admins/data-protection/consent-management.md) from an individual to store and process their personal data.

In some cases, when properly explained, consent for storing browser cookies in the visitor’s browser files can be sufficient for collecting and processing the data associated with those cookies' functionality.

This example shows the process of creating a custom page in the Xperience administration interface that maps custom cookie levels to [consents](https://docs.kentico.com/documentation/developers-and-admins/data-protection/consent-management.md). With this page, editors can define which consents in the Data protection application are associated with specific cookie levels. This is a key step in creating a cookie-level configuration page, e.g. the [cookie policy page](https://www.kentico.com/cookies-policy) on [Kentico.com](http://Kentico.com).

## Create the consents

First, open the **Data protection** application in the **Configuration** category of the administration interface for Xperience by Kentico, and create consents with the following properties:

1. Preference cookies consent
   a. **New consent**

   - **Display name:** Preference cookies
   - **Code name:** preference.cookies.consent

   ![Screenshot of consent create page](https://docs.kentico.com/docsassets/modules/define-cookie-consent-module/ConsentCreate.png "Screenshot of consent create page")

   a. **Consent texts**

   - **Short text:** Preference cookies help us remember changes and configurations you make on the site.
   - **Full text:** Preference cookies and the data you provide us make your work easier by, for example, remembering the language in which you want to display the pages, etc.

   ![Screenshot of consent text editing page](https://docs.kentico.com/docsassets/modules/define-cookie-consent-module/ConsentText.png "Screenshot of consent text editing page")

2. Analytical cookies consent
   a. **New consent**
   - **Display name:** Analytical cookies
   - **Code name:** analytical.cookies.consent
     a. **Consent texts**
   - **Short text:** Analytical cookies are used to gather usage data to measure and improve performance.
   - **Long text:** Analytical cookies and the data you provide us allow us to perform site usage analytics to measure and improve the site's performance. For example, we know which pages are most frequently visited, which buttons users click, etc.

3. Marketing cookies consent
   a. **New consent**
   - **Display name:** Marketing cookies
   - **Code name:** marketing.cookies.consent
     a. **Consent texts**
   - **Short text:** Marketing cookies are used to provide relevant ads based on your activity.
   - **Long text:** Marketing cookies and the data you provide us allow us to link our website to third-party social media and advertising networks, such as Facebook or Google Ads. With this connection, we can present relevant ads outside our website.

> **Note:** In real-world applications, your legal team should review the short and long texts of your consents to ensure that they meet the requirements of your region and market.

## Define the module

### Create the class

Now that these consents exist, we can create a UI page for mapping them to cookie levels.

1. Open the **Modules** application in the administration interface for Xperience by Kentico, under the **Development** category.

2. Create a new module for adding custom pages to the _Data protection_ application.
   a. **Display name:** Data protection customizations
   a. **Pre-fill code name automatically:** False (Disabled)
   a. **Code name:** TrainingGuides.DataProtectionCustomizations

   ![Screenshot of the module creation page](https://docs.kentico.com/docsassets/modules/define-cookie-consent-module/DataProtectionCustomizationsModule.png "Screenshot of the module creation page")

3. On the **Classes** tab of this new module, create a new class.
   a. **Display name:** Cookie level consent mapping
   a. **Namespace:** TrainingGuides
   a. **Name:** CookieLevelConsentMapping

   ![Screenshot of the class creation page](https://docs.kentico.com/docsassets/modules/define-cookie-consent-module/CookieLevelConsentMappingClass.png "Screenshot of the class creation page")

4. On the **Database columns** tab in this new class, add new fields with the following properties. These fields will determine which consent corresponds to each cookie level.
   a. Preference cookie consent field

   - **Field name:** PreferenceConsentCodeName
   - **Data type:** Object code names
   - **Required:** False (Disabled)

     ![Screenshot of database fields editing page](https://docs.kentico.com/docsassets/modules/define-cookie-consent-module/CookieLevelConsentMappingDBFields.png "Screenshot of database fields editing page")

   a. Analytical cookie consent field

   - **Field name:** AnalyticalConsentCodeName

   - **Data type:** Object code names

   - **Required:** False (Disabled)
     a. Marketing cookie consent field

   - **Field name:** MarketingConsentCodeName

   - **Data type:** Object code names

   - **Required:** False (Disabled)
     a. Guid field

   - **Field name:** CookieLevelConsentMappingGuid

   - **Data type:** Unique identifier (GUID)

   - **Required:** True (Enabled)

### Add the UI form

Switch to the **UI forms** tab of the class.

1. Populate the following properties accordingly:
   - **Form display name:** Cookie level consent mapping
   - **Pre-fill code name automatically:** False (Disabled)
   - **Form name:** CookieLevelConsentMapping

     ![Screenshot of UI form creation page](https://docs.kentico.com/docsassets/modules/define-cookie-consent-module/CookieLevelConsentMappingUIFormCreation.png "Screenshot of UI form creation page")

2. Save the form, and switch to the **Fields** tab.

3. Create a field for each [previously specified consent columns](#create-the-class) with the following properties:
   - **Form component:** Object code names
   - **Initial value:** Select
   - **Object type:** CMS.Consent
   - **Maximum items:** 1

4. The fields should have the following **Field caption** values, respectively:
   - Preference cookies consent (level 2)
   - Analytical cookies consent (level 3)
   - Marketing cookies consent (level 4)

> **Info:** The fields defined within UI forms represent how the database columns will be represented in the admin UI

![Screenshot of UI form editor](https://docs.kentico.com/docsassets/modules/define-cookie-consent-module/CookieLevelConsentMappingUIForm.png "Screenshot of UI form editor")

### Generate the code

Next, designate code settings for the class to make it more compatible with other features.

1. Switch to the **Code** tab of the class.
2. Set the **Object type** to _TrainingGuides.CookieLevelConsentMapping_.
3. Set the **Display name column** to _CookieLevelConsentMappingID_, and set **Code name column** and **GUID column** to _CookieLevelConsentMappingGuid._
4. Leave all the other properties un-set.

   > **Note:** **Readable names**
   >
   > Typically, columns that hold human-readable data are used for Display names and code names of custom objects. This helps the people managing the objects find them in object listings and query them easily.
   >
   > However, in this case, there is only going to be a single entry for this class, so there will be no listing page. There is no need for the class to have a readable display name because it is never used.

   ![screenshot of the code tab of the CookieLevelConsentMapping class](https://docs.kentico.com/docsassets/modules/define-cookie-consent-module/CookieLevelConsentMappingClassCode.png "screenshot of the code tab of the CookieLevelConsentMapping class")

This completes the administration interface setup for the class, so now the code can be generated.

Now, you can generate a code file for the custom class as described on [this page](https://docs.kentico.com/documentation/developers-and-admins/api/generate-code-files-for-system-objects.md).

1. Switch the command line to the _TrainingGuides.Web_ directory.

2. Run the `--kxp-codegen` tool, setting the location to the _TrainingGuides.Entities_ project and include only the _CookieLevelConsentMapping_ class.

   ```cmd
   dotnet run -- --kxp-codegen --type "Classes" --location "../TrainingGuides.Entities/{type}/{name}" --with-provider-class "false" --include "TrainingGuides.CookieLevelConsentMapping" 
   ```

   This will generate a _CookieLevelConsentMappingInfo.generated.cs_ code file in the _TrainingGuides.Entities/Classes/CookieLevelConsentMapping_ folder.

   > **Info:** **Info providers**
   >
   > We recommend using the `--with-provider-class "false"` parameter to **avoid** generating a [dedicated provider](https://docs.kentico.com/documentation/developers-and-admins/customization/customize-system-providers.md) for your class.
   >
   > You should use a [generic provider](https://docs.kentico.com/documentation/developers-and-admins/customization/customize-system-providers.md), based on `IInfoProvider<TInfo>` to access the objects of your class.
   >
   > This is the best practice **unless** you specifically need to retrieve your objects in a context where **dependency injection is not yet available**. In such cases, use [`Provider<T>.Instance`](https://docs.kentico.com/documentation/developers-and-admins/api/database-table-api.md#generic-provider-class-iinfoprovidertinfo) to access the generic provider.
   >
   > Only generate dedicated providers if dependency injection is not possible **and** using `Provider<T>.Instance` doesn't work for your specific scenario (for example, if you need custom functionality or overrides in the provider class).
   >
   > If you've previously generated classes that **do** require dedicated providers, use the [`--include` or `--exclude` parameters](https://docs.kentico.com/documentation/developers-and-admins/customization/object-types.md#generate-code-files) to ensure that the new command does not overwrite them.

3. Add a subfolder named _Overrides_ to the _\~/Classes_ folder of the _TrainingGuides.Entities_ project.

4. Create a partial class for `CookieLevelConsentMapping` and enable continuous integration.

   > **Tip:** **Partial class**
   >
   > Continuous Integration could be enabled by modifying the TYPEINFO declaration in the generated class. However, this separate partial class ensures that the change will not be overwritten next time the code is generated.

   ```csharp title="CookieLevelConsentMappingInfoOverride.cs"
   namespace TrainingGuides.DataProtectionCustomizations;

   public partial class CookieLevelConsentMappingInfo
   {
       static CookieLevelConsentMappingInfo()
       {
           TYPEINFO.ContinuousIntegrationSettings.Enabled = true;
       }
   }
   ```

   > **Info:** This configuration allows the cookie-level consent mapping to be shared among developers by including serialized data in source control.
