Don't miss out Virtual Happy Hour this Friday (April 26).

Try our conversational search powered by Generative AI!

Andreas Ylivainio
Apr 8, 2011
  3243
(0 votes)

Personalization outside of the box (...or HTML-editor)

Coinciding with the release of EPiServer 6 R2 the creative minds at Circuit came up with the idea that a client of ours should have the ability to time control the display of slideshow-like content on their start page, ensuring the slideshow content targeted the appropriate target audience during different times of the day.

Since I knew the personalization in R2 was just around the corner I suggested we use that feature instead of just restricting the editors to selecting a time of day.

Thinking of the editors experience with having to paste HTML and CSS (and JavaScript for older browsers) to make the slideshow work within the editor, we quickly realized we needed to create a method for the editor to pick a Visitor Group and that we as developers of the start page template would render the correct html.

There is however no built-in method to select an arbitrary Visitor Group or to evaluate a Visitor Group match outside of the HTML-editor.

Enter Circuit.PropertyTypes.PropertyVisitorGroup! This custom property renders a dropdown-list of all defined Visitor Groups (using the Dynamic Data Store API) and saves the selected group in a property on the page.

I then hacked together a class named VisitorGroupsHandler used for activating the criterias in the selected Visitor Group and evaluating them. The class has three methods:

MatchAllCriteriasInGroup(VisitorGroup …)
 
MatchAnyCriteriasInGroup(VisitorGroup …)

and

IsMatch(VisitorGroup …) 

which uses both the preceding methods. The functionality does not yet handle points matching.

Here is an example of the MatchAllCriteriasInGroup method, minus argument validation:

public static bool MatchAllCriteriasInGroup(VisitorGroup visitorGroup)
 
{
 
//...
 
            var httpContextBase = new HttpContextWrapper(HttpContext.Current);
 
            foreach (var criteria in visitorGroup.Criteria)
 
            {
 
                var type = Type.GetType(criteria.TypeName, true);
 
                if (typeof(ICriterion).IsAssignableFrom(type))
 
                {
 
                    var criterion = (ICriterion)Activator.CreateInstance(type);
 
                    criterion.Initialize(criteria);
 
                    if (!criterion.IsMatch(httpContextBase.User, httpContextBase))
 
                    {
 
                        return false;
 
                    }
 
                }
 
            }
 
            return true;
 
        }
 
The end result is that the template code can decide what content to display based on the call to:
bool isBusinessVisitor = VisitorGroupsHandler.IsMatch(CurrentPage.BusinessVisitorGroup);
 
// Set active slide based on the isBusinessVisitor value

or when not using Page Type Builder:

bool isBusinessVisitor = VisitorGroupsHandler.IsMatch((PropertyVisitorGroup)CurrentPage["BusinessVisitorGroup"]).
 
// Set active slide based on the isBusinessVisitor value

The source code is downloadable here.

One thing I haven't solved is how to activate the Visitor Group interface when in edit mode to be able to preview the page with the different Visitor Groups used in the property. I will update the post when that is solved.

Feedback is very welcome!

Apr 08, 2011

Comments

smithsson68@gmail.com
smithsson68@gmail.com Apr 8, 2011 05:06 PM

There is an API for checking if the current request matches against a visitor group role.

Add the EPiServer.Personalization.VisitorGroups namespace to your code file then you will get an new overload to the EPiServer.Security.PrincipalInfo.CurrentPrincipal.IsInRole method which takes an enum specifying what type of role to query. One of the enum values is for Visitor Groups.

Apr 8, 2011 05:54 PM

Thank you Paul! That also takes care of the impersonation issues. I also found that implementing the interface IPersonalizedRoles for my property made the Visitor Groups GUI appear.

My public IEnumerable GetRoles() just returns the selected Visitor Group guid.

Please login to comment.
Latest blogs
Optimizely Unit Testing Using CmsContentScaffolding Package

Introduction Unit tests shouldn't be created just for business logic, but also for the content and rules defined for content creation (available...

MilosR | Apr 26, 2024

Solving the mystery of high memory usage

Sometimes, my work is easy, the problem could be resolved with one look (when I’m lucky enough to look at where it needs to be looked, just like th...

Quan Mai | Apr 22, 2024 | Syndicated blog

Search & Navigation reporting improvements

From version 16.1.0 there are some updates on the statistics pages: Add pagination to search phrase list Allows choosing a custom date range to get...

Phong | Apr 22, 2024

Optimizely and the never-ending story of the missing globe!

I've worked with Optimizely CMS for 14 years, and there are two things I'm obsessed with: Link validation and the globe that keeps disappearing on...

Tomas Hensrud Gulla | Apr 18, 2024 | Syndicated blog

Visitor Groups Usage Report For Optimizely CMS 12

This add-on offers detailed information on how visitor groups are used and how effective they are within Optimizely CMS. Editors can monitor and...

Adnan Zameer | Apr 18, 2024 | Syndicated blog

Azure AI Language – Abstractive Summarisation in Optimizely CMS

In this article, I show how the abstraction summarisation feature provided by the Azure AI Language platform, can be used within Optimizely CMS to...

Anil Patel | Apr 18, 2024 | Syndicated blog