Loading...

Last updated: Nov 09 2016

Area: Episerver CMS Applies to versions: 10 and higher

Example: Creating custom criteria for defining visitor groups

(Thanks to Ted & Gustaf for this example.)

In this example, we develop a criteria for categorizing visitors according to whether or not a specific cookie exists in the session. One usage scenario could be to check for a cookie which says that the visitor has previously completed a purchase on the website, that is, is a returning customer.

The example goes through the following steps:

Creating the settings and criterion classes

All visitor group criteria require a settings class and a criterion class. The settings class is used to persist any settings available to editors when creating new visitor groups. The criterion class contains the logic to determine whether or not a visitor belongs to the visitor group.

Let us create our two classes in a suitable folder structure:

Implementing the settings class

The only setting required for this visitor group criterion is the cookie name we should look for.

First, we make our CookieExistsCriterionSettings class inherit the CriterionModelBase class:

public class CookieExistsCriterionSettings : CriterionModelBase

Next we add a public property to allow editors to specify the cookie name:

[Required]
public string CookieName { get; set; }

If you need custom validation logic when a criterion is saved, you can make your settings class implement the IValidateCriterionModel interface (this is optional). By implementing that interface’s Validate method you can customize how settings are validated before a criterion can be saved for a visitor group.

The abstract CriterionModelBase class requires us to implement the Copy() method. Since we are not using any complex reference types we can simply implement it by returning a shallow copy like so (see Developing custom visitor group criteria for more information on this):

public override ICriterionModel Copy()
{
   return ShallowCopy();
}

So, this is our complete CookieExistsCriterionSettings class:

public class CookieExistsCriterionSettings : CriterionModelBase
{
    [Required]
    public string CookieName { get; set; }

    public override ICriterionModel Copy()
    {        
         return ShallowCopy();
    }
}

Implementing the criterion class

To start us off we will make our CookieExistsCriterion class inherit the abstract CriterionBase class with our settings class as the type parameter:

public class CookieExistsCriterion : CriterionBase<CookieExistsCriterionSettings>

Next we add a VisitorGroupCriterion attribute to set the category, name, and description of our criterion (for more available VisitorGroupCriterion properties, see the Developing custom visitor group criteria):

[VisitorGroupCriterion(   
   Category = "Technical",
   DisplayName = "Cookie Exists",
   Description = "Checks if a specific cookie exists")]

The abstract CriterionBase class requires us to implement an IsMatch() method which determines whether or not the current user matches this visitor group criterion. In this case we should check if a specific cookie exists based on the criterion settings:

public override bool IsMatch(IPrincipal principal, HttpContextBase httpContext)
{ 
    return httpContext.Request.Cookies[Model.CookieName] != null;
}

So, our criterion class looks like this in its entirety:

[VisitorGroupCriterion(   
   Category = "Technical",
   DisplayName = "Cookie Exists",
   Description = "Checks if a specific cookie exists")]
public class CookieExistsCriterion : CriterionBase<CookieExistsCriterionSettings> { public override bool IsMatch(IPrincipal principal, HttpContextBase httpContext) { return httpContext.Request.Cookies[Model.CookieName] != null; } }

Note that we access the criterion settings instance through the Model property.

Testing the new criterion

To test our criterion, we create a new visitor group using the new cookie criterion. We set the cookie name to .EPiServerLogin, the name of the cookie created when a user logs in to Episerver:

Select some content in TinyMCE that should only be displayed to users currently logged in to Episerver (this is one way of doing it):

Next, click the Personalized Content button and select the new Episerver User visitor group:

This content will now only be displayed to users logged in to Episerver:

After publishing the page we see this:

However, if we log out of EPiServer, we’ll no longer see the personalized content:

 


Do you have feedback on this documentation? Send an email to documentation@episerver.com. For development-related questions and discussions, refer to our Forums on https://world.episerver.com/forum/