Skip to main content

Microsoft

Getting Started with Sitecore Cookie-Based Personalization Rules

While working on my current project we ran into a requirement to personalize content based on a cookie that is generated on certain actions within the site. Sitecore out-of-the-box personalization rules do not contain a rule to check for cookies and personalize content based on the existence of a cookie or a value contained within it. Although there are a few helpful blog posts that outline how to get started with something like this, unfortunately the most helpful one was a little outdated and the location of the Sitecore items that need updated to support a cookie rule are in a different location than in Sitecore 8.1 update 2 (rev. 160302).
Here is what it takes to get started with cookie-based personalization rules in the newer versions of Sitecore.
First, a new tag needs to be created in /sitecore/system/Settings/Rules/Definitions/Tags. I called this tag ‘Cookie’ in the screenshot below.
SitecoreRuleTag
Next, create a new ‘Element Folder’ under /sitecore/system/Settings/Rules/Definitions/Elements and name it ‘Cookie’.
Element1
Element2
Set the Default Tag Definition’s selected value to the ‘Cookie’ tag created earlier.
Element3
Add a new condition to the ‘Tags’ item under the element and give it a name that corresponds to what it will do.
Condition1
Condition2
In the ‘Text’ field insert the value ‘where the [CookieName,,,specific] cookie exists’ to allow the user to specify the name of the Cookie to use for personalization.
Condition3
To wire everything up, you’ll need to create a class to support the cookie existence check. Below is the code snippet that the ‘Type’ value of the condition will use. You’ll need to include using statements for Sitecore.Rules and Sitecore.Rules.Conditions.

using Sitecore.Rules;
using Sitecore.Rules.Conditions;
using System.Web;
namespace Domain.Infrastructure.Conditions.Cookie
{
  public class CookieExists<T> : WhenCondition<T> where T : RuleContext
{
    public string CookieName { get; set; }
    protected override bool Execute(T ruleContext)
    {
      if (string.IsNullOrEmpty(CookieName))
        return false;
      return HttpContext.Current.Request.Cookies[CookieName] != null;
    }
  }
}

The last part of the wire up is to include the new ‘Cookie’ item in the Default Tags of Conditional Renderings. This item is located in /sitecore/system/Settings/Rules/Conditional Rendering/Tags/Default.
Condition4
Now, the Rule Set Editor will contain a section for Cookie with the rule to check for the existence of a specified cookie.
Condition5
Now I have everything I need to support personalizing content based on the existence of a specified cookie. Additional rules can be added to check for specific values within a cookie, as needed. You’ll just need to add a new condition and support class to handle the back-end processing.
If you have any questions or suggestions for other topics you would like to see covered, please feel free to comment here or reach out to me on Twitter @bill_cacy.
Thanks!

Thoughts on “Getting Started with Sitecore Cookie-Based Personalization Rules”

  1. I followed the steps but my custom condition rule doesn’t show up in the rule set editor(sitecore 8.1)

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

William Cacy

More from this Author

Categories
Follow Us