Dev GuideAPI Reference
Dev GuideAPI ReferenceUser GuideGitHubNuGetDev CommunitySumbit a ticketLog In
GitHubNuGetDev CommunitySumbit a ticket

Create advanced audience combinations

How to create advanced audience combinations with nested logical operators using JSON in Code Mode.

Using the Optimizely app, you can easily use "and", "or" operators (i.e., '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, 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

If an experiment or rollout 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 SDKMinimum version
Android
C#
Java
JavaScript (Browser)
JavaScript (Node)
PHP
Python
Ruby
v3.0.0
Flutterv1.0.0-beta
Gov1.0.0
React
React Native
v1.0.0
Swiftv3.1.0

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 find them by selecting Match any audience or Match all audiences, selecting the audience, and switching to Code Mode.

1009

Update Code Mode JSON

Select Code Mode to access the code editor and define your JSON audience combination.

The Evaluated Audiences field provides a summary of the defined conditions, which allows you to verify the audience combination's definition and accuracy.

550

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 allows for 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 example below 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 doesn't "Like both salads & sadwiches"
[
  "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
    }
  ]
]