# Audiences

URL: https://docs.ablyft.com/guides/audiences-and-delivery/audiences/

> Limit an experiment to certain visitors, for example by device, visit history, language, cookies or data layer events.



An **audience** describes a group of visitors, for example "mobile visitors from Germany" or "returning visitors who
came from Google". When you attach an audience to an experiment, only visitors who match it can take part.

If you attach **no audience**, every visitor can take part. The **Audiences** card then shows
**All users are currently included**. You do not need to create an "all users" audience.

## Create an audience

There are two ways:

* From an experiment: in the card **Audiences** ("WHO should participate?"), click **+** to create an audience that is
  attached to the experiment immediately.
* From the navigation: go to **Targeting & Goals → Audiences** and create a new audience. Then attach it to an experiment
  with **Select audience** in the **Audiences** card.

The form contains:

| Field           | Description                                                                      |
| --------------- | -------------------------------------------------------------------------------- |
| **Name**        | A clear, descriptive name, for example `Mobile visitors`. Unique in the project. |
| **API Name**    | Only needed for advanced users. Filled in from the name.                         |
| **Description** | Optional note.                                                                   |
| **Targeting**   | The criteria that define the audience. At least one is required.                 |

To add criteria, click **Add criteria**, choose a criterion from the list (you can search it) and fill in its fields.

## How criteria are combined

You build audiences from **criteria**. Two simple rules explain how they work together:

* **AND between criteria.** Click **Add AND criteria** to add another criterion. The visitor must fulfil **all** criteria.
* **OR inside a criterion.** Many criteria let you add several rows with **Add OR criteria**. The visitor must fulfil
  **at least one** row.

Example: *Device type is mobile* **AND** *User language contains `de`* **OR** *User language contains `en`* describes
mobile visitors whose browser language is German or English.

If you attach **several audiences** to an experiment, the selection above the audience list decides how they work
together:

| Setting            | Meaning                                               |
| ------------------ | ----------------------------------------------------- |
| **ANY must match** | The visitor must match at least one of the audiences. |
| **ALL must match** | The visitor must match every audience.                |

> **Audiences are not checked in preview:** When you test an experiment with the status **Preview**, audiences are ignored so that you can always see the
> variation. To find out whether an audience matches, use debug mode and look at the browser console, see
> [Debugging](https://docs.ablyft.com/developers/debugging/).

## Criteria

### Device and visit

| Criterion                       | Options                                                                                                          |
| ------------------------------- | ---------------------------------------------------------------------------------------------------------------- |
| **Device type**                 | **is** / **is not** `mobile`, `tablet` or `desktop`. A device that is both mobile and tablet counts as `tablet`. |
| **Visit type**                  | `new` or `returning`. A visitor is `returning` after their first session.                                        |
| **Total sessions**              | **is less than**, **is greater than** or **equals** a number.                                                    |
| **Total pageviews**             | **is less than**, **is greater than** or **equals** a number.                                                    |
| **Pageviews (current session)** | **is less than**, **is greater than** or **equals** a number.                                                    |
| **First visit**                 | **is before** or **is on or after** a date.                                                                      |
| **Last visit**                  | **is before** or **is on or after** a date.                                                                      |

The counters are stored in the visitor's browser (see [Storage & privacy](https://docs.ablyft.com/developers/consent-and-security/storage-and-privacy/)). A visitor who uses another browser or deletes the website data
counts as a new visitor. A new session starts when your website is opened in a new browser session, for example in a new
tab or after the browser was closed.

### Origin and language

| Criterion         | Options                                                                                                                                                                      |
| ----------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| **URL parameter** | A parameter **name** and a check: **exists**, **does not exists**, **is (exactly)**, **is not (exactly)**, **contains**, **does not contain**, plus a value.                 |
| **Cookie**        | A cookie **name** and the same checks as for URL parameters.                                                                                                                 |
| **User language** | **is (exactly)**, **is not (exactly)**, **contains**, **does not contain**, plus a value such as `de-DE`. It is compared with the language setting of the visitor's browser. |
| **Referrer**      | **contains** or **does not contain** a text, for example `google.com`. It is compared with the address of the page from which the visitor came.                              |

The URL parameter is read from the current address when the audience is checked. For a cookie, the cookie must exist
in the visitor's browser and be readable by JavaScript.

### Time

| Criterion         | Options                                                                                                                                                       |
| ----------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| **Time of visit** | A time window with **From** and **To**, in the visitor's local time. If **From** is later than **To**, the window spans midnight, for example 22:00 to 06:00. |
| **Day of week**   | One or more **Days**, in the visitor's local time.                                                                                                            |

### Events and code

| Criterion                | Description                                                                          |
| ------------------------ | ------------------------------------------------------------------------------------ |
| **dataLayer Event**      | Matches when an event arrives in your data layer. See below.                         |
| **JavaScript Condition** | Your own JavaScript. The audience matches if it returns `true`.                      |
| **JavaScript Trigger**   | Your own JavaScript that calls `activate()` when the audience should start matching. |

#### dataLayer Event

Use this criterion for visitors who triggered an event in your data layer, for example Google Tag Manager. You enter the
**Event name**, a **Check** (for example **is (exactly)** or **contains**), and, depending on the check, a **Property**
and a **Value**. All fields and checks are described in
[Data layer events](https://docs.ablyft.com/developers/tracking-and-events/datalayer-events/#use-data-layer-events-in-audiences). How ABlyft detects
data layer events is set in the [snippet settings](https://docs.ablyft.com/developers/installation/snippet-settings/#data-layer-watching).

#### JavaScript Condition

The code is the body of a function and must return `true` for visitors who belong to the audience. Besides the standard
browser objects you can use `User` and `Tools` (or `ablyftTools`). Example:

```js
// Visitors with at least 10 pageviews in the current session
return User.visits.pageviewsSession >= 10;
```

If the code throws an error, the audience does not match.

##### Device, browser and operating system

In a JavaScript Condition, `User.browser` tells you which device, browser and operating system the visitor uses. Each
property is `true` when detected. Use it for targeting such as "desktop only" or "Safari on iOS 15 or newer".

| Group                          | Properties                                                                                                           |
| ------------------------------ | -------------------------------------------------------------------------------------------------------------------- |
| Device (at most one is `true`) | `mobile` (without tablets), `tablet`, `desktop`                                                                      |
| Browser                        | `msedge`, `opera`, `samsungBrowser`, `firefox`, `chromium`, `chrome`, `msie`, `safari`. The version is in `version`. |
| Operating system               | `windows`, `mac`, `android`, `ios`, `linux`. An iOS device also sets one of `iphone`, `ipad` or `ipod`.              |
| OS version                     | `osversion`, if the user agent contains it (Android, iOS, macOS, Windows, Windows Phone, WebOS, Bada, Tizen).        |

```js
// Firefox on macOS
return User.browser.firefox && User.browser.mac;

// Tablets with iOS newer than 9, or any Android tablet
return User.browser.tablet && ((User.browser.ios && User.browser.osversion > 9) || User.browser.android);

// Internet Explorer 9 and newer
return User.browser.msie && User.browser.version >= 9;
```

#### JavaScript Trigger and trigger-based criteria

A **JavaScript Trigger** and a **dataLayer Event** are different from the other criteria: they are fulfilled only after
something happened on the current page. The trigger code runs once per page view on every page of your project. It calls
`activate()` when the audience should start matching, exactly like the code of the
[Callback trigger of a page](https://docs.ablyft.com/guides/audiences-and-delivery/pages/#callback-trigger).

As soon as the criterion is activated, ABlyft checks the affected experiments again. The state is not remembered across
page views, so the event has to happen on the page again after a page reload. To trigger a new check from your own code,
see [`reevaluateAudiences`](https://docs.ablyft.com/developers/reference/javascript-api/push/#reevaluateaudiences).

## When audiences are checked

ABlyft checks the audiences after the page and environment of an experiment matched, and before the visitor is assigned
to a variation. The check is repeated for a short time after the page has loaded, so content and cookies that appear a
moment later can still qualify the visitor.

Once a visitor has been assigned to a variation, they stay in it. See
[Traffic allocation](https://docs.ablyft.com/guides/audiences-and-delivery/traffic-allocation/).

## Manage audiences

In **Targeting & Goals → Audiences** you can edit, clone and delete audiences. You cannot delete an audience that is used
by an experiment. Open **Edit** and look at **Used in Experiments** to see which experiments use it.

> **Audiences are shared:** If you change an audience that is used in several experiments, all of them are affected. If the audience is used by a
> running experiment, the change is published right away. Clone the audience if you want to change it for one experiment
> only.

## Next steps

- [Traffic allocation](https://docs.ablyft.com/guides/audiences-and-delivery/traffic-allocation/): Distribute visitors across variations.
- [Pages & URL targeting](https://docs.ablyft.com/guides/audiences-and-delivery/pages/): Define where an experiment runs.

