In today's guide, you will learn how to check if a user matches a segment within Episerver CMS. In Episerver terminology, this means making sure the criteria of a visitor group has been met. In order to do this check, we will be using a combination of theIVisitorGroupRepository and the VisitorGroupHelper. If you haven't come across this API yet, I would recommend reading this first. If you are planning on adding personalisation to your Episerver website, this is the article for you 🔥🔥🔥


In order to check if the current request matches a visitor group, you can use the VisitorGroupHelper. Unfortunately, VisitorGroupHelper has not been built against an interface. Due to this limitation, VisitorGroupHelper can not be used with dependency injection. This means unit testing any code that uses this API will be a little bit harder. If you want to test your code, I would recommend creating a very basic wrapper for the class before you start using it in anger throughout your codebase. VisitorGroupHelper only ships with one method, IsPrincipalInGroup() which takes in an IPrincipleand a role. This is why wrapping it isn't too painful and can be done in less than 10 minutes.

When using VisitorGroupHelper the role needs to match the visitor group name, so for example if your visitor group is called Example Visitor Group that is what you would need to pass in.

How to Check If The Current Request matches an Episerver Visitor Group

The code is shown below:

Getting Visitor Groups From Episerver

To check if a user matches on a visitor group, you will need to get access to all the visitor groups contained within the CMS. This is where IVisitorGroupRepository comes into play:

On-Line 3, I access the API using the ServiceLocator anti-pattern. I only access the API this way to make this snippet easy to understand (use construction injection in production ✔️). On Line 6, we load the visitor group we are interested in from the CMS. In this example, I query the CMS for the visitor group using a GUID. You can get this information within the editor by loading the group in the editor and looking at the URL. A less hard-coded approach would be to get all of the groups defined with the CMS, using the List() method, and checking the user matches all of them. In my example, I'm using the code from How To Create A Visitor Group Selection Factory to store the Id.

That's all you need to know to start checking your site visitors matches a segment. Happy Coding 🤘