HomeGuidesAPI Reference
Submit Documentation FeedbackJoin Developer CommunityOptimizely GitHubOptimizely NuGetLog In

DELAY PUBLISHING Create advanced audience combinations

❗️

publish this topic when velociraptor aka flags-first UI adds support for advanced audience combos

📘

Advanced audiences created in Code Mode are not currently available in non-legacy Full Stack projects created after February 2021, but will be re-enabled in a future release.

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)

📘

Only the v3.0 SDKs (released after February 2019) are compatible with audience combinations other than "Match any audience."
If a flag rule using the "Match all audiences" or "Custom" audience type is evaluated with a 1.x or 2.x SDK, targeting won't pass and conversion events and decision events will be lost.

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.

10091009

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.

550550

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

Optional: Use the REST API to save the audience combination

As an optional final step, you can HTTP PUT the audience combination as a serialized string (for example, by using JSON.stringify(data) in JavaScript) via the audience_conditions key and /features REST API endpoint.

We'll return it in the same format, so to traverse it you'll need to parse it as an object (for example, by using JSON.parse(string) in JavaScript).

For more information about the REST API endpoints, see:

📘

Note

You can use the same audience_conditions format for the /features REST API endpoints.

For the REST API, the only difference is that you must use a value of "everyone" for audience_conditions to target the rollout accordingly (i.e., everyone should be allowed).


Did this page help you?