Sitecore page level rules engine personalization
Posted 4 Mar 2019 by Marek Musielak
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.
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:
- Change language of current page
- Redirect to another page
- Redirect to another page and change language
- Return chosen HTTP Status Code
And finally I added a custom button to Experience Editor ribbon to make it easier for content authors to manage rules assigned to pages:
How can I start to personalize users experience on page level?
The simplest way is easy like 1, 2, 3:
- Download Sitecore Page Rules content package
- Install the package in your Sitecore application
- 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.:
-
User hasn't read T&C page and wants to see a contest page, let's use
except where the T&C page has been visited during the current visit, redirect user to step T&C
rule. -
Page should be accessible only after a certain date? We can set the rule
except when the date has passed, end response with 404 Page Not Found status code
- Page should be accessible only for users who triggered specific campaign, who triggered specific goal or for those who are enrolled in certain plan? Just use the condition you want and return 401 Forbidden HTTP Status code.
- You want to use GEO IP data to change the language of your home page automatically? You click 3 times and it's done.
- You don't have translation ready for your page? Just redirect user to a "Work in progress" page and when your content is ready, clear the rule.
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.