Exam Area: Content Area 1 – Product Knowledge (15%)
Reference: https://docs.developers.optimizely.com/content-management-system/docs/personalization
Visitor Groups allow editors to show different content to different groups of users based on criteria.
@* Using Personalized Content helper *@
@using EPiServer.Personalization.VisitorGroups
<div>
@* Content for everyone *@
<h1>Welcome!</h1>
@* Content only for logged-in users *@
@if (User.Identity.IsAuthenticated)
{
<p>Welcome back, @User.Identity.Name!</p>
}
</div>
using EPiServer.Personalization.VisitorGroups;
using System.ComponentModel.DataAnnotations;
// 1. Model for criterion
[VisitorGroupCriterion(
Category = "Custom",
DisplayName = "User Points",
Description = "Users with certain points")]
public class UserPointsCriterionModel : CriterionModelBase
{
[Display(Name = "Minimum Points")]
public int MinimumPoints { get; set; }
public override ICriterionModel Copy()
{
return ShallowCopy();
}
}
// 2. Criterion implementation
[VisitorGroupCriterion(
Category = "Custom",
DisplayName = "User Points",
Description = "Users with certain points")]
public class UserPointsCriterion : CriterionBase<UserPointsCriterionModel>
{
public override bool IsMatch(IPrincipal principal, HttpContext httpContext)
{
// Logic to check the user's points
var userPoints = GetUserPoints(principal);
return userPoints >= Model.MinimumPoints;
}
private int GetUserPoints(IPrincipal principal)
{
// Custom logic
return 100; // example
}
}
using EPiServer.Personalization.VisitorGroups;
public class PersonalizationService
{
private readonly IVisitorGroupRepository _visitorGroupRepository;
private readonly IVisitorGroupRoleRepository _roleRepository;
public bool IsUserInGroup(IPrincipal user, string groupName)
{
var group = _visitorGroupRepository.List()
.FirstOrDefault(vg => vg.Name == groupName);
if (group == null) return false;
return _roleRepository.IsInGroup(user, group.Id.ToString(), null);
}
}
@* In View, CMS handles the visitor group logic automatically *@
@Html.PropertyFor(m => m.MainContentArea)
Editors can set visitor groups on blocks within a ContentArea via Edit View.
Important note: When using personalization, be careful with output caching because personalized content must not be shared in a cache.
// Disable output cache for pages with personalized content
[ContentOutputCache(Duration = 0)]
public ActionResult Index(MyPage currentPage)
{
return View(currentPage);
}
Criteria available in CMS by default:
| Criteria | Description |
|---|---|
| Role | Check user role |
| User | Specific username |
| IP Address | IP range |
| Browser | Browser type |
| Query String | URL parameter |
| Landing URL | First visited URL |
| Number of visits | Visit count |
| Time of day | Hour of the day |
| Day of week | Day of the week |
| Geo Location | Country, city (requires MaxMind) |
CriterionBase<TModel>)IsMatch method in a custom criterion do? (Checks whether a user belongs to the group)[VisitorGroupCriterion] attribute do? (Registers the criterion with the CMS)