---
title: Example – Create audience criteria
slug: cms-13/docs/example-create-audience-criteria
docTags: 
createdAt: 2026-07-30T20:22:10.000Z
---

(Thanks to [Ted and Gustaf](https://tedgustaf.com/) 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](https://api.archbee.com/api/optimize/BrbvVORKJ5iIeSMZye5kI/N7HEOBU-H8rVIFNbf2Xnb-20260722-195707.png "create_classes.png")

## 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:

```csharp
public class CookieExistsCriterionSettings : CriterionModelBase
```

2. Add a `public` property to let editors specify the cookie name:

```csharp
[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](docId\:aH6XKafnX_qKLMYHbzUpw).

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

The following code shows the complete `CookieExistsCriterionSettings` class:

```csharp
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:

```csharp
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](docId\:aH6XKafnX_qKLMYHbzUpw):

```csharp
[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:

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

The complete criterion class:

```csharp
[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;
  }
}
```

:::hint{type="info"}
**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:

![](https://api.archbee.com/api/optimize/BrbvVORKJ5iIeSMZye5kI/8rmmQjN87xCRbEp20T416-20260722-184805.png)

2. Select content in the editor to restrict to logged-in users:

![](https://api.archbee.com/api/optimize/BrbvVORKJ5iIeSMZye5kI/VkiZXJb459hsrjQFR6dbA-20260722-184805.png)

3. Select **Personalized Content** > **Episerver User** audience.

![](https://api.archbee.com/api/optimize/BrbvVORKJ5iIeSMZye5kI/dDsjyl0cqAjxLHKeQ5_cK_9385a8798fbd1c1ad1a9f6be991bb64a84eea9f540c36986dc2b257b54caf8df-5b18ee4-selectvisitorgroup.png)

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

![](https://api.archbee.com/api/optimize/BrbvVORKJ5iIeSMZye5kI/NYADsxaSZLjhhJ5Y2ZQab-20260722-194635.png)

After publishing the page:

![](https://api.archbee.com/api/optimize/BrbvVORKJ5iIeSMZye5kI/btI9qecI0Y9oY9wE8hfEg-20260722-194635.png)

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

![](https://api.archbee.com/api/optimize/BrbvVORKJ5iIeSMZye5kI/X3AkJZ68ztE48Tg2fibu7-20260722-194635.png)
