In this tutorial, you will learn how to create custom visitor group criteria within Episerver 7. Out-of-the-box, Episerver ships with lots of useful visitor group criteria. You can use these criteria to introduce personalisation within your website. In a lot of scenarios, the default criteria won't be enough to meet all your business requirements. It is highly likely you will want to personalise based on something not possible out-of-the-box. If you find yourself in this situation, read on to learn how to fix it 🔥🔥🔥

In this article, you will get access to all the code that you need to create your own personalisation segments within Episerver CMS. This article does not cover everything about visitor groups. If you need some extra information about visitor groups I recommend reading these two tutorials:

To create your own segment you will need to write some code. You will need to create two classes. One class will be used within the CMS to store the visitor group data/settings. The other one will be used to apply the segments rules. So let us look at how to create these classes 💥

The Settings Class

The first step is to create a new class that inherits from CriterionModelBase:

On Line1, the class inherits from CriterionModelBase. When you inherit fromCriterionModelBase, you will need to implement a method called Copy() (as can be seen on Line5). In this class, you will also need to add any properties that you want to be displayed to the content editor. Notice on Line 3, a property called MyCustomProperty is defined. When the visitor group is loaded within the CMS this property will appear to the content editor and they will be able;e to add data into this property You can add as many properties as you want. This is how you can get dynamic data to use within the criteria and segmentation process later!

On Line1, I also implement the IValidateCriterionModel interface. This is optional and when you create your own custom criterion you do not need to implement it. When you add IValidateCriterionModel to your settings class you are adding editor level validation that will be called whenever any visitor group of that type is saved. The IValidateCriterionModel supplies the Validate() method as can be seen on Line 10. This method will be called whenever a content editor tried to save a new visitor group that contains this criterion. So if you want to add custom validation use the IValidateCriterionModel interface, otherwise omit it!

The Criterion

The next step is to add the rules. When the visitor group is run on the website, you need to add some logic to apply your visitor groups purpose. The code to do this is shown below:

Within IsMatch() add the custom logic to determine if the visitor group should be applied or not. In my example I simply return true, obviously, you will want to do something more complex in your production version 😉. To see this visitor group in action, after adding the classes, build the project. Next, open the editor and go to the visitor groups tab. Create a new visitor group and in the criteria section your new criteria should now appear:

How To Create A Custom Visitor Group Criteria In Episerver 7

That is all you need to do to create a custom segment within Episerver CMS. Happy Coding 🤘