Disclaimer: This website requires Please enable JavaScript in your browser settings for the best experience.

The availability of features may depend on your plan type. Contact your Customer Success Manager if you have any questions.

Dev guideRecipesAPI Reference
Dev guideAPI ReferenceUser GuideLegal TermsGitHubDev CommunityOptimizely AcademySubmit a ticketLog In
Dev guide

Create advanced audience combinations

How to create advanced audience combinations using code mode.

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 SDKMinimum version
Android
C#
Java
JavaScript (Browser)
JavaScript (Node)
PHP
Python
Ruby
v3.0.0
Flutterv1.0.1
Gov1.0.0
React
React Native
v1.0.0
Swiftv3.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).

Select Code Mode under Audiences for a rule to access the code editor and define your JSON audience combination.

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.

Add the audience identifier automatically by selecting an audience

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 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 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
    }
  ]
]

Troubleshooting 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 will see this error:

invalid operator warning

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

Unable to leave code mode.

If your audience is too advanced, you will not be able to toggle back to Non-Code Mode. When you select Non-Code mode you will see the following message:

unable to leave code mode

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 error

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

invalid JSON example

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

invalid JSON without brackets

To fix the error, add []: