Create advanced audience combinations in Feature Experimentation

  • Updated
  • Optimizely Feature Experimentation

Using the Optimizely Feature Experimentation UI, you can use the and or or operators (that is, any or all) to create an audience combination composed of other audiences.

If you want to use more complex nested logical operators with and, or, or not, you can do so in JSON in Code Mode. Each audience is a rule like User likes salads, and an audience combination is a boolean combination of these rules, like User likes pizza NOT (User likes salads AND User likes soup)

SDK version compatibility

SDK versions released after February 2019 are compatible with the advanced audience combinations, Match all audiences or Custom.

If a flag rule using the Match all audiences or Custom audience type is evaluated with an older SDK, targeting will not pass and conversion events and decision events will be lost.

Minimum SDK version

The table describes the minimum SDK version required for advanced audience combinations. For the current SDK versions, refer to the SDK compatibility matrix.

Optimizely Feature Experimentation SDK Minimum version
Android
C#
Java
JavaScript (Browser)
JavaScript (Node)
PHP
Python
Ruby
v3.0.0
Flutter v1.0.1
Go v1.0.0
React
React Native
v1.0.0
Swift v3.1.0

Create an advanced audience using Code Mode

Create or select a flag with at least one rule (targeted delivery, A/B test, or Multi-armed bandit optimization). Then click Code Mode on a rule to access the code editor and define your JSON audience combination. 136a31b-image.png

Get the audience identifiers

Each individual condition is a JSON object with an audience_id. You can add these identifiers directly in Code Mode or add the audience identifier automatically by selecting the search box labeled Search and add audiences and selecting an audience.

Click the Audiences

Define the conditions

Conditions are joined together in lists:

  • The first element in each list is an operator, "and", "or", or "not", and the rest of the conditions are combined using that operator.
  • You can replace any individual condition with another list, which lets you create a nested structure of "and" and "or" conditions.
  • A "not" list should only have one condition or list, which will be negated. A "not" with a list of other conditions like ["not", ["and", {...}, {...}]] can negate the entire result of the child condition list.

The following example shows how you could define audience combination conditions. You can also create a feature with an audience combination in Optimizely and look at the Code Mode view.

// "User who loves salads"
// or "User who loves sandwiches"
[
  "or",
  {
    "audience_id": 1038980040
  },
  {
    "audience_id": 1033280055
  }
]

// "User who loves salads"
// or "User who loves sandwiches"
// or does not "Like both salads & sandwiches"
[
  "or",
  {
    "audience_id": 1038980040
  },
  {
    "audience_id": 1033280055
  },
  [
    "not",
    {
      "audience_id": 1120870079
    }
  ]
]

// Is not "User who loves salads"
// AND is not "User who loves sandwiches"
[
  "not",
  [
    "and",
    {
      "audience_id": 1038980040
    },
    {
      "audience_id": 1033280055
    }
  ]
]

Troubleshoot code mode

The following may be helpful to troubleshoot errors that arise while using Code Mode.

Incorrect operator

If you use an invalid operator, you see this error.

Incorrect operator error message

Refer to Define the conditions for a list of the acceptable operators.

Unable to leave code mode

If your audience is too advanced, you are not be able to toggle back to Non-Code Mode. Instead, when you click Non-Code Mode you see the following message: 

Non-Code Mode message

This does not mean that your audience is incorrect. Instead, it is alerting you that your audience cannot be represented in the simplified, non-code view. You can continue to edit your audience in Code Mode.

Audience conditions must be valid JSON

Your audience must be written in valid JSON. If you have a syntax error, you see the following error: 

Invalid JSON error message

Additionally, when returning to Non-Code Mode, without defining an audience, you must have an opening and closing bracket to do so. If you select Non-Code Mode with an empty audience condition, you get the following error: 

Empty Non-Code mode error message

To fix the error, add [], like the following image: