Simple condition
A simple condition is a condition that can be evaluated by comparing one value to another without involving any additional parsing, conversion or other processing.
#
About this exampleImagine a case where a visitor has to click on a popup in order to opt into more extensive tracking. On certain sections of the site, visitors who have opted in have access to additional content unavailable to visitors who opted out.
A Sitecore developer implemented a custom personalization condition that allows content authors to apply a condition to components that display content that they want to limit to visitors who opted in.
When a visitor opts in, a cookie is set to a specific value. This is handled by logic written by a front-end developer:
- Cookie name:
WEBSITE_TRACKING_OPT_IN
- Cookie value (example):
YES
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_TRACKING_OPT_IN"]; if (cookie?.Value == null) { return false; } return cookie.Value == "YES"; } }}
In order for this custom condition to work after personalization is decoupled from the Sitecore CD server, the instructions in the custom condition must be converted into a format that is compatible with the personalization engine.
#
Edge-side personalizationThe component responsible for converting the instructions in a Sitecore personalization condition into a format that can be executed on the edge is called a Rule Condition Parser.
#
PrerequisitesBefore starting to implement support for the custom condition, make sure you have the following:
- Visual Studio
- Sitecore NuGet feed is a package source in Visual Studio
- Uniform NuGet feed is a package source in Visual Studio
- The custom condition is configured on your Sitecore instance
#
Create Visual Studio project- Create a new project using the following settings:
- Template: ASP.NET Web Application (.NET Framework)
- .NET Framework: 4.7.2
- Add references to the following NuGet packages:
Sitecore.Kernel
Uniform.Optimize.Personalization.Esi.Core
- Add a reference to the project (or assembly) that has the custom condition you want to support.
#
Implement rule condition parserThe 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 parserUniform extends the Sitecore page building process so that when a component with personalization rules assigned to it is rendered, the appropriate Uniform rule condition parsers are applied. Each rule 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 instanceDeploy the config file and dll files to the Sitecore instance.