Creating Custom Personalization Rules in Sitecore: A Step-by-Step Guide

Sitecore, a leading digital experience platform, offers a robust personalization feature that allows you to tailor your website’s content to your visitors’ needs. While Sitecore provides several out-of-the-box rules for personalization, there may be scenarios where you need to create custom rules to meet specific business requirements. This blog post will guide you through the process of creating a custom personalization rule in Sitecore.

Understanding Personalization in Sitecore

Personalization in Sitecore is the method of displaying targeted and relevant content to website visitors based on different characteristics like location, age, visits, etc. The concept of personalization is based on rules. For example, you can instruct Sitecore’s platform, "if condition A is met, then perform action B.".

Let’s dive into how you can do that:

Create a Custom Tag

  • Start by creating a new tag for your custom rule under and navigate to the following path /sitecore/system/Settings/Rules/Definitions/Tags. Use the template /sitecore/templates/System/Rules/Taxonomy/Tag.

  • Define Custom Elements and Conditions:

  • Next, create a custom element and then personalization condition under /sitecore/system/Settings/Rules/Definitions/Elements.
  • Provide descriptive name to the condition
    • In the personalization condition, populate the following fields:
      • Text: This text will be presented to content editors when using the condition in the Rules Engine. Refer below sample as per my requirement and this condition need to change as per your requirement.
      • Type: Specify the fully qualified class and assembly names where you want to implement the business rule for the custom personalization condition.

    Assign the Custom Tag:

    • Assign the custom tag (created in step 1) to the default element tag definition.

    Add the Tag to Conditional Rendering:

    • To make the rule appear in the Rule Set Editor under Conditional Renderings, navigate to /sitecore/system/Settings/Rules/Conditional Renderings/Tags/Default and assign the new tag to the Conditional Renderings tag.

    Code Implementation of Bussiness Rule:

    • Ensure that the namespace and class name match the value specified in the Type field above. The .NET class needs to inherit from one of the following classes.
      • To implement Boolean conditions, use Sitecore.Rules.Conditions.WhenCondition.

      • To compare string values, use Sitecore.Rules.Conditions.StringOperatorCondition.

      • To compare values of other types, use Sitecore.Rules.Conditions.OperatorCondition.

    • I have attached the code sample for cookie rule in the below snap and also in GIST. Based on the requirement, rule logic can be implemented under execute method.

    Configure the Rule:

    After completing all the aforementioned changes, content and business editors can configure this rule to personalize components using either the experience editor or the content editor in the presentation details and then click on Personalize. Choose the relevant rule and update the values as per the rule condition.
    • After setting the rule, the rule will appear according to it and set the datasource based on the condition or show/hide the component.

    Conclusion:

    Creating custom personalization rules in Sitecore is a powerful way to meet specific business requirements and enhance the user experience on your website. By following these steps, you can extend Sitecore’s capabilities and deliver more personalized content to your site visitors.

    Happy Personalization 😀😀😀

    Comments