Sitecore Cookbook for Developers
上QQ阅读APP看书,第一时间看更新

Personalizing components

Sitecore components provide you with a facility to personalize placed components on a page, which is processed by Sitecore Rule Engine. We can do personalization based on many settings related to the field, item, security, user, system, analytics, and so on.

In this recipe, we will cover a very basic example to personalize rendering. On our website, we are showing sale or discounts on products in the form of a carousel. Now, at the end of the month or on any selected date, we want to show these carousel slides. After that, it should get hidden automatically.

Getting ready

We will personalize carousel rendering that we created in the previous chapter. If you are using Sitecore 8, you should enable analytics to enable personalization by doing the following setting in the \App_Config\Include\Sitecore.Analytics.config file. For older versions, personalization will be available by default.

<setting name="Analytics.Enabled" value="true" />

How to do it…

Let's see how we can create rules and change renderings conditionally:

  1. From the Content Editor, select the standard values of a template, for example, Site Root, where we added the Carousel controller rendering. Open Device Editor, and select the Carousel rendering from the Controls tab. Click on the Personalize button found on the right-hand side, as shown in the following image:
    How to do it…
  2. This will open the Personalize the Component dialog. Tick the Hide Component checkbox in the Default condition so that, by default, the Carousel rendering will be hidden on the page. Additionally, create New ConditionShow Carousel, as shown in the following image:
    How to do it…
  3. Click on the Edit button to open the Rule Set Editor dialog. Select conditions from the list. In our case, go to the Date section, and select when the current day of the month compares to number and when the current month is month:
    How to do it…
  4. At the bottom of the Rule description section, you will find the selected rule condition. Select compares to and set its value to is equal to. Select number and set its value to 25. Select month and set its value to December.
  5. To test this, set today's date and month in this condition. Make sure that you publish all the modified items and view pages in Normal mode or Explore mode to check how personalization works.

How it works…

When a request is made to the Sitecore web page, the layout engine first invokes the rules engine. The rules engine applies the given rules to the item and performs the actions accordingly before the actual rendering is done. So, it regenerates the whole layout structure before the actual execution starts.

Note

Heavier usage of rules and personalization may affect performance.

If you have cached components, personalization on them will not work properly.

Here, we applied personalization just to show or hide renderings. We can also Personalize Components (replace components) or Personalize Content (change data source of components). To view these options, just tick the Enable personalization of component design checkbox, as shown in the following image:

How it works…

You can also personalize renderings from the Experience Editor in the floating bar of rendering:

How it works…

There's more…

Here, you learned a very basic example of using Component Properties to achieve personalization. In Chapter 10, Experience Personalization and Analytics Using xDB, you will learn more realistic personalization techniques.