Sitecore Digital Marketing System Part 1: Creating personalized, custom content for site visitors
Apr 01, 2013
Sitecore's Digital Marketing System (DMS) can help you personalize the content your site displays to your users/visitors and can be easily customized to fit your specific requirements while using all the functionality that Sitecore already offers.
Let's suppose you want to show different content modules to your users depending on their profile attributes like age, gender, number of twitter followers, number of achievements earned in the site, visitors without an account, etc.
We will need to let the site editor choose and create new rules that may include one or more profile attributes; therefore we can't create a set of fixed rules to be used. Also, it is important to note that as result of our personalization, we will be required to:
- Show a different sublayout
- Hide the sublayout
- Change the datasource of the sublayout
First, here is my User Profile class:
namespace RulesEngine.Session
{
public class UserProfile
{
public int UserId { get; set; }
public DateTime RegistrationDate { get; set; }
public string Nationality { get; set; }
public UserMaritalStatus MaritalStatus { get; set; }
public int NumberAchievements { get; set; }
}
public enum UserMaritalStatus
{
Single,
Married,
Divorced
}
}
To keep this demo as simple as possible, I’ll also create a dummy user session manager using a query string value to simulate the current logged in user:
namespace RulesEngine.Session
{
public static class UserSession
{
public static UserProfile GetCurrentUser()
{
var userId = HttpContext.Current.Request.QueryString["UserId"];
if (string.IsNullOrEmpty(userId))
{
//User not authenticated
return null;
}
UserProfile user = null;
switch (userId)
{
case "1":
user = new UserProfile()
{
UserId = 1,
RegistrationDate = new DateTime(2009, 10, 6),
MaritalStatus = UserMaritalStatus.Married,
NumberAchievements = 10,
Nationality = "British"
};
break;
case "2":
user = new UserProfile()
{
UserId = 2,
RegistrationDate = new DateTime(2005, 1, 26),
MaritalStatus = UserMaritalStatus.Single,
NumberAchievements = 55,
Nationality = "American"
};
break;
case "3":
user = new UserProfile()
{
UserId = 3,
RegistrationDate = new DateTime(2011, 2, 14),
MaritalStatus = UserMaritalStatus.Divorced,
NumberAchievements = 3,
Nationality = "French"
};
break;
case "4":
user = new UserProfile()
{
UserId = 4,
RegistrationDate = new DateTime(1999, 8, 30),
MaritalStatus = UserMaritalStatus.Single,
NumberAchievements = 8,
Nationality = "Ecuadorian"
};
break;
}
return user;
}
}
}
Let's start with the user's nationality. All we need to do is create a new class with StringOperatorCondition
as the base:
using Sitecore.Rules;
using Sitecore.Rules.Conditions;
namespace RulesEngine.Rules
{
public class NationalityCondition<T> :
StringOperatorCondition<T> where T : RuleContext
{
//This is the value the editor will enter
public string Nationality { get; set; }
///
/// This function will be called to test the condition
///
protected override bool Execute(T ruleContext)
{
//Get the current user from our dummy user session manager
var user = UserSession.GetCurrentUser();
//Check if the user is logged in, otherwise return false
if (user == null)
return false;
//Use base function to compare the values
return this.Compare(user.Nationality, Nationality);
}
}
}
After a build of the solution, we need to register this new condition in Sitecore and copy the output DLL file to the website’s bin directory (if using a separate project). Open Sitecore’s content editor and navigate to the location: /sitecore/system/Settings/Rules/Conditional Renderings/Conditions
and create a new folder:
Then we create a new condition:
The Text field is the content that will be shown to the editor when selecting this condition, we put the following text:
where the user's nationality [operatorid,StringOperator,,compares to] [Nationality,Text,,nationality name]
Notice that it has some special content surrounded by square brackets. This tag has four parameters: the first is the name of the a property in the condition, the second parameter is the name of the sitecore's item also known as macro (located in /Sitecore/System/Settings/Rules/Common/Macros
), the third parameter is a parameter that will be sent to the macro item selected in the second parameter, and the fourth parameter is the text shown to the editor before he enters a value for the condition. In our case we have:
Parameter |
Object |
operatorid |
Sitecore.Rules.Conditions.StringOperatorCondition.OperatorId |
StringOperator |
/sitecore/system/Settings/Rules/Common/String Operators |
(empty) |
We don’t need a parameter |
compares to |
This text will be shown to the editor |
Parameter |
Object |
Nationality |
Our RulesEngine.Rules.NationalityCondition.Nationality |
Text |
It will request a single line of text |
(empty) |
We don’t need a parameter |
nationality name |
This text will be shown to the editor |
To apply this new rule to our site, we go to the item we want to personalize, then go to Presentation -> Layout Details:
Then on the new window, click on Edit:
Under Controls, select the sublayout and click on Personalize. Notice that this Personalize button only appears once you add DMS to your Sitecore installation:
Then click on New Condition:
Enter a name for the condition and then click Edit:
Now we can select our new condition:
See part two of this post for how to create conditions that will compare integer, dates and other custom values.
Related Insights
-
Wladimir Moyano
-
Trotsky Gonzalez
-
Yamilet Contreras
Docker and Sitecore
Scalability & Efficiency in Web Development
-
Mateo Recoba
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.