HomeDev GuideRecipesAPI Reference
Dev GuideAPI ReferenceUser GuideGitHubNuGetDev CommunityOptimizely AcademySubmit a ticketLog In
Dev Guide

Example – Create audience criteria

Build a custom Cookie Exists criterion that checks for a specific cookie in the visitor's browser.

(Thanks to Ted and Gustaf for this example.)

This example builds a custom audience criterion that checks whether a specific cookie exists in the visitor's browser. A common use case is identifying returning customers who have previously completed a purchase.

The example follows these steps:

  1. Create the settings and criterion classes
  2. Implement the settings class
  3. Implement the criterion class
  4. Test the criterion

Create the settings and criterion classes

Audience criteria require a settings class and a criterion class. The settings class persists editor-facing configuration. The criterion class contains the logic that determines whether a visitor belongs to the audience.

Create the two classes in a suitable folder structure:

Screenshot of Visual Studio folder structure with settings and criterion classes

Implement the settings class

The settings class stores the cookie name that the criterion evaluates. The only required configuration for this criterion is the name of the cookie to detect.

  1. Make the CookieExistsCriterionSettings class inherit the CriterionModelBase class:

    public class CookieExistsCriterionSettings : CriterionModelBase
  2. Add a public property to let editors specify the cookie name:

    [Required]
    public string CookieName {
      get;
      set;
    }
  3. (Optional) Implement the IValidateCriterionModel interface for custom validation logic when a criterion is saved. The Validate method customizes validation before saving a criterion for an audience.

    The abstract CriterionModelBase class requires a Copy() method implementation. For simple value types, return a shallow copy as shown. For details, go to Create custom audience criteria.

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

The following code shows the complete CookieExistsCriterionSettings class:

public class CookieExistsCriterionSettings: CriterionModelBase {
  [Required]
  public string CookieName {
    get;
    set;
  }
  public override ICriterionModel Copy() {
    return ShallowCopy();
  }
}

Implement the criterion class

The criterion class evaluates the HTTP request to determine whether the specified cookie exists.

  1. Make the CookieExistsCriterion class inherit the abstract CriterionBase class with the settings class as the type parameter:

    public class CookieExistsCriterion : CriterionBase<CookieExistsCriterionSettings>
  2. Add a VisitorGroupCriterion attribute to define the category, name, and description. For additional attribute properties, go to Create custom audience criteria:

    [VisitorGroupCriterion(   
      Category = "Technical",
      DisplayName = "Cookie Exists",
      Description = "Checks if a specific cookie exists")]
  3. Implement the IsMatch() method to check whether the specified cookie exists in the request. Access the criterion configuration through the Model property:

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

    The complete criterion class:

    [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

Access the criterion settings instance through the Model property.

Test the criterion

Test the criterion by creating an audience that uses the cookie criterion.

  1. Set the cookie name to .EPiServerLogin, the cookie created when a user logs in to Episerver:

    Screenshot of the Cookie Exists criterion editor with cookie name set to .EPiServerLogin
  2. Select content in the editor to restrict to logged-in users:

    Screenshot of the content editor with text selected for personalization
  3. Select Personalized Content > Episerver User audience.

    Screenshot of the audience selector with Episerver User audience selected

This content now displays only to users logged in to Episerver:

Screenshot of the TinyMCE editor showing personalized content

After publishing the page:

Screenshot of the published page showing personalized content for logged-in users

After logging out of Episerver, the personalized content no longer displays:

Screenshot of the published page without personalization after logout