---
title: Example - Personalization condition type
related:
  - https://docs.kentico.com/documentation/developers-and-admins/digital-marketing-setup/content-personalization/develop-personalization-condition-types.md
---

> 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).

> **License:** Advanced license required.
>
> Features described on this page require the Xperience by Kentico **Advanced** license tier.

This page contains a full code sample that demonstrates how to develop a personalization condition type. To learn more about the development process in general, visit our [Develop personalization condition types](https://docs.kentico.com/documentation/developers-and-admins/digital-marketing-setup/content-personalization/develop-personalization-condition-types.md) page.

> **Note:** **Contact tracking**
>
> We recommend setting up [tracking of contacts](https://docs.kentico.com/documentation/developers-and-admins/digital-marketing-setup/contact-configuration.md) on your website. The tracking is required for the example to work correctly (and for most types of conditions that utilize Xperience digital marketing features and data).

When finished, this condition type allows you to display different live site content to visitors based on [consent agreements](https://docs.kentico.com/documentation/developers-and-admins/data-protection/consent-management.md) they have given. Content editors are able to define different variants of widget content for any number of consents.

Create a **HasGivenConsentConditionType** class in your project. Place the class file into a component folder for the condition type, for example: _\~/Components/PageBuilder/PersonalizationConditions/HasGivenConsent/_

> **Tip:** We recommend using [dependency injection](https://docs.kentico.com/documentation/developers-and-admins/development/website-development-basics/dependency-injection.md) to initialize service instances.

```csharp title="HasGivenConsentConditionType.cs"
using System.Collections.Generic;
using System.Linq;

using CMS.ContactManagement;
using CMS.DataEngine;
using CMS.DataProtection;

using Kentico.PageBuilder.Web.Mvc.Personalization;
using Kentico.Xperience.Admin.Base.FormAnnotations;
using Kentico.Xperience.Admin.Base.Forms;

using MyProject.Components.PageBuilder.PersonalizationConditions.HasGivenConsent;

[assembly: RegisterPersonalizationConditionType(
    identifier: "MyProject.Personalization.HasGivenConsentConditionType",
    type: typeof(HasGivenConsentConditionType),
    name: "Has given consent agreement",
    Description = "Evaluates whether the contact has given an agreement with a selected consent declaration.",
    IconClass = "icon-clipboard-checklist",
    Hint = "Select a consent. The condition is fulfilled for visitors who have given an agreement with the given consent.")]

namespace MyProject.Components.PageBuilder.PersonalizationConditions.HasGivenConsent
{
    public class HasGivenConsentConditionType(
        IInfoProvider<ConsentInfo> consentInfoProvider,
        IConsentAgreementService consentAgreementService) : ConditionType
    {
        // Parameter: Consent for which visitors need to give an agreement to fulfill the condition
        // Assigns the Xperience object selector component to the property, which allows users to select a consent in the configuration dialog
        [ObjectSelectorComponent(ConsentInfo.OBJECT_TYPE, Order = 0, Label = "Consent", MaximumItems = 1)]
        public IEnumerable<ObjectRelatedItem> Consent { get; set; } = Enumerable.Empty<ObjectRelatedItem>();

        /// <summary>
        /// Default property representing the name of the personalization variant.
        /// </summary>
        public override string VariantName
        {
            get
            {
                // Uses the code name of the selected consent as the name of the variant
                return Consent?.FirstOrDefault()?.ObjectCodeName;
            }
            set
            {
                // No need to set the variant name property
            }
        }

        public override bool Evaluate()
        {
            // Gets the contact object of the current visitor
            ContactInfo currentContact = ContactManagementContext.GetCurrentContact(false);

            // Gets the consent object based on the code name of the selected consent
            var consentCodeName = Consent?.FirstOrDefault()?.ObjectCodeName;
            if (string.IsNullOrEmpty(consentCodeName))
            {
                return false;
            }

            ConsentInfo consent = consentInfoProvider.Get(consentCodeName);
            if (consent == null || currentContact == null)
            {
                return false;
            }

            // Checks if the contact has given a consent agreement
            return consentAgreementService.IsAgreed(currentContact, consent);
        }
    }
}
```
