contact@skillcore.net

Sitecore page level rules engine personalization

Posted 4 Mar 2019 by Marek Musielak

sitecore rules engine

If you've been working with Sitecore for a while, I'm sure you're familiar with Sitecore Rules Engine. And I'm sure you've used it plenty of times. I bet you used it with Conditional Renderings, maybe with Insert Options Rules or with EXM. But have you ever wanted to execute your custom rules for every page your visitors browse? This article explains how I achieved it and provides a link to Sitecore content package which will allow you do to the same in your Sitecore application.

TL;DR download Sitecore Page Rules content package here.

How Sitecore Page Rules work?

I've created a simple Sitecore template with 1 field only. Field is called Page Rules and it's using Rules field type.

page with rules - sitecore template

I added this template as a base template for my page templates. Every time a new request comes, code checks if the field exist and if it's not empty, and then starts Sitecore Rules Engine magic:

public void Process(PipelineArgs args)
{
    try
    {
        var rulesField = Context.Item?.Fields[PageRulesFieldName];

        if (rulesField == null || string.IsNullOrWhiteSpace(rulesField.Value))
            return;

        var rules = RuleFactory.GetRules<PageRulesRuleContext>(rulesField);

        if (rules == null || rules.Count == 0)
            return;

        var ruleContext = new PageRulesRuleContext();
        rules.Run(ruleContext);
    }
    catch (Exception exc)
    {
        Log.Error("Exception while running page rules", exc, this);
    }
}

I registered my processor in renderLayout pipeline, right after StartAnalytics processor. It could be added much earlier, in httpRequestBegin pipeline after ItemResolver, but then you would not have access to all the data available for the Contact that visits your site.

I wrote few Sitecore Rules Engine Actions which could be useful:

And finally I added a custom button to Experience Editor ribbon to make it easier for content authors to manage rules assigned to pages:

sitecore - edit page rules button

How can I start to personalize users experience on page level?

The simplest way is easy like 1, 2, 3:

  1. Download Sitecore Page Rules content package
  2. Install the package in your Sitecore application
  3. Add Page With Rules template to base templates of your pages, publish your site and voilà.

You can also download the code from github repo and modify it if needed.

Ok, ok, but what can I use page level Sitecore rules for?

I can think of countless scenarios, e.g.:

With Sitecore rules engine sky is the limit. If you have any ideas of how Sitecore Page Rules can be used, do not hesitate to let me know in the comments below. Thank you for reading. You can find more Sitecore related articles here.

Comments? Find me on or Sitecore Chat