Skip to main content

Complex condition

A personalization condition always involves comparing two or more values. For example, a condition that determines whether a specific date has passed compares a specific date to the current date.

A complex condition is a condition where at least one of the values being compared must be parsed, converted or processed in some way before the comparison can be made.

About this example#

This example is a variation on the simple condition example. Like with that example, when the visitor opts into more extensive tracking, a cookie is set. The difference in this example, however, is that the cookie that is set represents a list of features the visitor has opted into:

  • Cookie name: WEBSITE_OPT_INS
  • Cookie value (example): TRACKING,NAVIGATION,PREFERENCES

The following represents the implementation of the custom condition:

using Sitecore.Rules;using Sitecore.Rules.Conditions;using System.Linq;using System.Web;
namespace MySite{    public class TrackingOptInCondition<T> : WhenCondition<T> where T : RuleContext    {        protected override bool Execute(T ruleContext)        {            var cookie = HttpContext.Current.Request.Cookies["WEBSITE_OPT_INS"];            if (cookie?.Value == null)            {                return false;            }            return cookie.Value.Split(',').Contains("TRACKING");        }    }}

Edge-side personalization#

Like in the simple condition example, a Rule Condition Parser is needed to convert the instructions from the Sitecore personalization condition into a format that can be executed on the edge.

However, in this example, the cookie value may contain multiple values. This means the cookie value must be parsed before it can be used. This use case requires the use of the edge context.

Prerequisites#

Before starting to implement support for the custom condition, make sure you have the following:

  1. Visual Studio
  2. Sitecore NuGet feed is a package source in Visual Studio
  3. Uniform NuGet feed is a package source in Visual Studio
  4. The custom condition is configured on your Sitecore instance

Create Visual Studio project#

  1. Create a new project using the following settings:
    • Template: ASP.NET Web Application (.NET Framework)
    • .NET Framework: 4.7.2
  2. Add references to the following NuGet packages:
    • Sitecore.Kernel
    • Uniform.Optimize.Personalization.Esi.Core
  3. Add a reference to the project (or assembly) that has the custom condition you want to support.

Implement edge context extension#

The Edge Context extension performs the following logic:

  1. Reads the cookie value.
  2. Splits the cookie value into an array.
  3. Determines whether the array contains a specific value.

Implement rule condition parser#

The condition parser is implemented in C#. Uniform extends the Sitecore page building process to use the parser when a component is rendered.

Add the following class to your Visual Studio project:

using Sitecore.Rules.Conditions;using System;using System.Collections.Generic;using System.Linq;using System.Web;
using Uniform.Optimize;using Uniform.Optimize.Query;using Uniform.Optimize.Rules;
namespace MySite{    public class TrackingOptInConditionParser : BaseRuleConditionParser    {        public override EsiQuery BuildQuery<T>(RuleCondition<T> ruleCondition, IEsiQueryContext context)        {            var expression = EsiExpression                                .FromCookie("WEBSITE_TRACKING_OPT_IN")                                .IsEqualTo("YES");            return new EsiQuery(expression);        }
        public override IEnumerable<Type> GetSupportedConditionTypes()        {            return new Type[] { typeof(TrackingOptInCondition<>) };        }    }}

Register rule condition parser#

Uniform extends the Sitecore page building process so that when a component with personalization rules assigned to it is rendered, the appropriate Uniform condition parsers are applied. Each condition parser must be registered via a Sitecore config file.

Add the following config file to your Visual Studio project:

<?xml version="1.0" encoding="utf-8"?><configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">  <sitecore>    <uniform>      <personalization>        <esi>          <ruleParser>            <conditions>              <parser type="MySite.TrackingOptInConditionParser, MySite" />            </conditions>          </ruleParser>        </esi>      </personalization>    </uniform>  </sitecore></configuration>

Deploy files to Sitecore#

Deploy the config file and dll files to the Sitecore instance.