How to learn Sitecore from Basic to Advanced (Part 2)

Custom Rules – Implementing personalization rules

  1. Definition
    When you’re face to face with a customer, it’s easy to create a personalized experience. You ask and answer questions and form a dialogue with that customer, giving them information they want, need, or you think might interest them. But when that personalization experience relies on technology, you need to instruct that technology with rules to follow. We call this “rules-based personalization.” (available rule or we can create new Custom Rule)
  2. How it works?
    For example, you can tell Sitecore’s platform, “if condition A is met, then perform action B.” That might be, “Once a visitor views three web pages related to camping gear (condition A), display a tent on the home page the next time they visit the site” (action B). Based on the evaluation of the rule, Sitecore can alter:
    – Pieces of content or visuals (known as data sources)
    – The way the page is laid out (known as display components)
    – Both at the same time

    It’s in your control to define the conditions under which content is delivered to a customer. You can set rules based on the following elements to decide the content that will be displayed:
    – The IP address or physical location of your visitors
    – The keywords they use to reach your site
    – Their device
    – Their behavior on your website (pages viewed, number of times they visited, etc.)

    Sitecore’s digital experience platform is built to follow rules that are as granular and complex as you like. You might decide, “If condition A and B are met, but
    not condition C, then perform action D.” The more layers you include in your rules, the more deeply personalized the experiences will be that you can deliver.

Events – Event Handlers

  1. Definition
    Events in Sitecore are similar to events in other systems: something triggers an event and there are handlers that are configured to handle the event. Event handlers are similar to pipelines in how they are configured. An event handler is a .NET class that implements a method. When an event is triggered, the event handlers are run in order. Event handlers are defined in Sitecore patch files.
    The following is an example of the event handler that handles the item:deleted event.

2. How it works?
Adding an Event Handler:
While there is neither a class to extend nor an interface to implement, a convention must be followed in order for Sitecore to be in order for a class to be used as an event handler. The class must have a method that accepts two parameters and return void:
2.1 Object – represents the object that holds a collection of the various event listeners
2.2 EventArgs – holds the parameters being passed to the event handler

The following is an example of a custom processor.

public class MyEventHandlers
public void OnItemSaved(object sender, EventArgs args)
//do something

After the event handler is written, it must be added to the event definition. The event definition is located in a Sitecore patch file. The following is an example of how the custom event handler is added to the Sitecore patch file.

<event name="item:saved">
<handler type="Testing.MyEventHandler, Testing" method="OnItemSaved"/>

Accessing Values from EventArgs
Sitecore uses the .NET framework’s event model to handle its events. This means that unless you use a custom EventArgs class (described below) you must use the Event.ExtractParameter method to extract parameters from the EventArgs object.
The following is an example of extracting a parameter from the EventArgs object.

var item = Sitecore.Events.Event.ExtractParameter(args, 0) as Sitecore.Data.Items.Item;
Sitecore.Diagnostics.Assert.IsNotNull(item, "No item in parameters");
%d bloggers like this: