Cooking with Sitecore DMS: Custom Conditions and Rules

It’s your favorite chef, back again. Today we’re going to review one of the more challenging recipes out there – the Sitecore DMS. The Sitecore DMS engine can be a scary thing. With so many pieces to work with, even the smallest challenges can have a lot of work involved. The DMS comes with a lot of functionality right out of the box, but also leaves plenty of room for extensibility. Let’s talk about custom conditions and rules you can use for content personalization within the DMS.

Start with Rules-Based Personalization

The rules-based personalization is the first place to begin with the DMS. The default conditions that are provided are useful for accessing users’ behaviors such as goals they have completed, campaigns they are a part of, or geolocation information (provided by MaxMind subscription of course). But what about doing anything that is specifically about your website’s content or workflow?

Custom conditions can be developed to check anything. Something I found useful was a check for page events that were completed on pages with a certain item template. One great part of this idea is that Sitecore gives us a big head start with the code, since they’ve already created a goal check condition that we can decompile and reuse.

Take a look at the Personalization editor in the Item Presentation Details.

You’ll see a condition check for whether the current visit has triggered a specific goal. Maybe you want to use this to hide content that doesn’t make sense to show after the user has done something, or maybe you want to show new content to the user after they’ve done something. Use this condition to return a basic true/false and show/hide a presentation detail item or change the data source for that presentation item.

To do so, first, we need to make a new Sitecore item for our custom condition. Find the Sitecore item representing the existing condition in the /sitecore/system/settings/rules/conditional renderings/conditions/events folder, as the Has Goal Condition item. Do a simple copy/paste of the existing “Has Goal Condition” item, but put it in a new Custom Conditions folder to avoid confusion when adding this condition.

Here you’ll see the text for setting the condition (which you saw when editing the Presentation Details). Clicking on the text “specific” brings up a tree picker. From there, choose a goal that you want to check to see if it’s been completed when a user visits this page. We’ll be checking a Template ID as well, but this will be a text string of the Template ID. We will also change the driving class and assembly to our own class. So, we’re going to need a decompiler next, like .NET Reflector.

Once you get the decompiled code, you can see the Engagement Analytics API that is at work here. The condition pulls all the page events for the current visit that are for the goal in question. Next, there is a check for any of those page events that were completed by the current Visit. The only thing we need to add is a check against the Template ID that we enter, and our condition is complete:

 				namespace DelphicDigital.Sitecore.RulesEngine.Conditions 				{ 				public class GoalCompletedOnPageTemplateCondition : StringOperatorCondition where T : RuleContext 				{ 				public string TemplateID { get; set; }  				public string GoalId { get; set; }  				protected override bool Execute(T ruleContext) 				{ 				return Tracker.DataContext.PageEvents.Any(new Func(me => IsGoal(me, TemplateID))); 				}  				private bool IsGoal(VisitorDataSet.PageEventsRow pageEventsRow, string TemplateID) 				{ 				if (pageEventsRow.PageEventDefinitionId != new Guid(GoalId)) 				{ 				return false; 				}  				VisitorDataSet.PagesRow page = pageEventsRow.Page; 				if (page == null) 				{ 				return false; 				}  				return (page.Visit == Tracker.CurrentVisit && NodeHelper.GetItemByID(new ID(page.ItemId)).TemplateID == new ID(TemplateID)); 				} 				} 				} 				Now that the code class is written, and you have added your Sitecore item, you can use this condition on your Presentation Details to personalize content for your users.  Next, I'll show how you can use these conditions in your sublayout's code-behind for even more personalization with the Sitecore DMS. Happy cooking! 				
« Prev Article
Next Article »