AI agents: the content index is at /llms.txt. Every page is also available as Markdown — send the request header Accept: text/markdown, or append .md to the page URL.
Xperience Docs

Cookie consent

We use necessary cookies (opens in a new tab) to run our website and improve your experience while browsing. Additional cookies are only used with your consent. You may revoke your consent on the Cookies Policy (opens in a new tab) page or in your browser at any time.

Configure (opens in a new tab)
  • Documentation
    InstallationUsing the Xperience interfaceDeveloping websitesConfiguring XperienceManaging usersSecuring websitesDeploying websitesManaging website contentMultilingual websitesMacro expressionsExternal utilitiesIntegrating 3rd party systemsCustom developmentE-commerce featuresOn-line marketing featuresRelease notes - Xperience 13Xperience terminology glossary
  • Tutorial
    Using the Xperience interfaceDeveloper tutorialBusiness tutorialResource hub for marketers
  • API Examples
    ConfigurationContent managementDevelopmentE-commerceOn-line marketingSocial media
  • Community Questions & Answers (opens in a new tab)
  • Contact support (opens in a new tab)
  1. Home
  2. On-line marketing features
  3. Configuring and customizing your on-line marketing features
  4. Content personalization
  5. Example - Developing a personalization condition type

Example - Developing a personalization condition type


Was this page helpful?

Send us feedback Go back to top

Related pages

  • Developing personalization condition types

Enterprise license required

Features described on this page require the Kentico Xperience Enterprise license.

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 Developing personalization condition types page.

Contact tracking

We recommend setting up Tracking of contacts on your MVC website, as it is needed for the example to work correctly (and for most types of conditions that utilize Xperience on-line marketing features and data).

When finished, this condition type allows you to display different live site content to visitors based on consent agreements they have given. Content editors are able to define different variants of widget content for any number of consents.

Create a HasGivenConsentConditionType.cs file in your MVC project's ~/Personalization/ConditionTypes folder.

Note: The following example is based on the LearningKit project⁠ (opens in a new tab). To use the code sample in your own project, you need to modify the namespaces, identifiers and other occurrences where LearningKit is mentioned to match your project's name.

We recommend using a dependency injection container to initialize service instances.

C#HasGivenConsentConditionType.cs
using CMS.ContactManagement;
using CMS.Core;
using CMS.DataProtection;

using Kentico.Forms.Web.Mvc;
using Kentico.PageBuilder.Web.Mvc.Personalization;

using LearningKit.Personalization.ConditionTypes;

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

namespace LearningKit.Personalization.ConditionTypes
{
    public class HasGivenConsentConditionType : ConditionType
    {
        // Parameter: Code name that identifies the consent for which visitors need to give an agreement to fulfill the condition
        // Assigns the default Xperience text input component to the property, which allows users to enter a text value in the configuration dialog
        [EditingComponent(TextInputComponent.IDENTIFIER, Order = 0, Label = "Consent code name")]        
        public string ConsentCodeName { get; set; }

        /// <summary>
        /// Default property representing the name of the personalization variant.
        /// </summary>
        public override string VariantName
        {
            get
            {
                // Uses the specified consent code name as the name of the variant
                return ConsentCodeName;
            }
            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);

            // Creates an instance of the consent agreement service
            // For real-world projects, we recommend using a dependency injection container to initialize service instances
            var consentAgreementService = Service.Resolve<IConsentAgreementService>();

            // Gets the consent object based on its code name
            ConsentInfo consent = ConsentInfo.Provider.Get(ConsentCodeName);
            if (consent == null || currentContact == null)
            {
                return false;
            }

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