📚 Product Knowledge
Personalization Visitor Groups
📖 Docs

Personalization and Visitor Groups - Optimizely CMS 12

Exam Area: Content Area 1 – Product Knowledge (15%)
Reference: https://docs.developers.optimizely.com/content-management-system/docs/personalization


1. What are Visitor Groups?

Visitor Groups allow editors to show different content to different groups of users based on criteria.

Example criteria:


2. Using Visitor Groups (Editor workflow)

  1. AdminVisitor Groups → Create a new group
  2. Add criteria to the group
  3. In Edit View, select content → "Personalized content"
  4. Assign content to a visitor group

3. Personalized Content in Views

@* 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>

4. Custom Visitor Group Criteria

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
    }
}

5. Checking Visitor Group Membership in Code

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);
    }
}

6. Personalized Content in ContentArea

@* 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.


7. Caching and Personalization

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);
}

8. Built-in Criteria (Out-of-the-box)

Criteria available in CMS by default:

CriteriaDescription
RoleCheck user role
UserSpecific username
IP AddressIP range
BrowserBrowser type
Query StringURL parameter
Landing URLFirst visited URL
Number of visitsVisit count
Time of dayHour of the day
Day of weekDay of the week
Geo LocationCountry, city (requires MaxMind)

Review Questions

  1. What do Visitor Groups enable? (Show different content to different groups of users)
  2. Which class must a custom criterion inherit from? (CriterionBase<TModel>)
  3. Why must you be careful with caching when using personalization? (Personalized content must not be shared in cache)
  4. What does the IsMatch method in a custom criterion do? (Checks whether a user belongs to the group)
  5. What does the [VisitorGroupCriterion] attribute do? (Registers the criterion with the CMS)