Example - Developing a personalization condition type

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 Kentico's 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.

Kentico EMS required

Features described on this page require the Kentico EMS license.

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

Note: The following example is based on the LearningKit project. 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.

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 Kentico 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 = ConsentInfoProvider.GetConsentInfo(ConsentCodeName);
            if (consent == null || currentContact == null)
            {
                return false;
            }

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


Was this page helpful?