Monday, March 13, 2017

Sitecore personalization rules for custom contact facets

This post is part 5 of a series, the introduction is available here and includes a table of contents, along with the full source code.

The concept of contacts (and the experience profile) in Sitecore is a great way to store a lot of valuable information on visitors to a Sitecore web site. Out of the box features really do provide a complete overview of a given browser across many mediums (including print). But custom contact facets open the door to unique business logic and requirements of a given organisation. What better way to utilize this data than creating a custom rule to be used for personalization?

In this example we are going to go with a simple rule - User is an active customer. Extending on from the active customer flag that was added to the custom data contact facet. This will allow Sitecore web pages to change based on whether or not a user has been flagged as an active customer. The use case here would be an incentive to purchase for non-customers and perhaps an up-sell for existing customers. By combining with another rule that checks the product purchase history of the user, you can see how targeted you could make web pages with Sitecore and minor customization.

The custom rule code

This is a simple rule that doesn't make use of conditions (greater than, less than, etc.), you'll notice that it includes TrueCondition rather than something more complex such as StringOperatorCondition. It simple returns whether or not the current user has been marked as an active customer or not. Sitecore will handle the inverse if the user selects where not in the rule builder.

using ContactFacet.Custom;
using Sitecore.Rules;
using Sitecore.Rules.Conditions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace ContactFacet.Rules
{
    public class IsActiveCustomerRule<T> : TrueCondition<T> where T : RuleContext
    {
        protected override bool Execute(T ruleContext)
        {
            try
            {
                var contact = Sitecore.Analytics.Tracker.Current.Contact;
                ICustomDataFacet facet = contact.GetFacet<ICustomDataFacet>(CustomDataFacet.FacetName); // Get the facet

                return facet.ActiveCustomer;
            }
            catch (Exception)
            {
                return false;
            }
        }
    }
}

Registering the rule in Sitecore

This is the relatively easy part.
  1. Log into Sitecore
  2. Browse to the elements item (where rules definitions are defined) - /sitecore/system/Settings/Rules/Definitions/Elements/
  3. Insert a new Element Folder and call is Contact
  4. Under the elment folder expand Tags and edit the Default item
    1. Select conditional renderings on the tags field - this allows the rule to be used when building page components
    2. Save the item
  5. Under the Contact element folder, Insert a new Condition item and give it a name of Is active customer
  6. Set the text to Where the user is an active customer. Usually this would be built up using operators and would appear a bit more advanced.
  7. Set the type to your class followed by DLL - ContactFacet.Rules.IsActiveCustomerRule, ContactFacet for example
  8. The rule has been created

Using the rule

Now when you are using the personalize section of the rule editor the new rule will be visible.

Which can then be used to build up the page to appear differently to users who are/are not active customers.



This is just a simple example to get the ball rolling, but illiterates how data stored on a custom contact facet can be used to deliver different experiences to different users.

No comments:

Post a Comment