Build code-based audiences

Updated

Define an audience with JavaScript to target subsets of your website visitors with variations.

An audience is a subset of your website visitors (e.g., visitors in New York). You can use audiences to refine who is eligible to see variations by targeting or excluding visitors. Code-based audiences let you define your own targeting logic using custom JavaScript — great for advanced use cases that go beyond what rules-based audiences support.

You can also create rules-based audiences to target visitors using built-in criteria like traits, behavior, or integration data.

Important

JavaScript conditions must evaluate to true or false. A visitor matches if conditions evaluate to true.

How to create a code-based audience

  1. Go to the Insights tab
  2. Click Audiences
  3. Click New Audience
  4. Click Code Your Audience
  5. Enter a descriptive Audience name
  6. Enter your audience’s JavaScript code
  7. Click Save

How to edit a code-based audience

Important

If the audience is already in use through variations, editing the audience will impact who sees those variations.

  1. Go to the Insights tab
  2. Click Audiences
  3. Choose an audience to modify
  4. Make your desired changes
  5. Click Save

Example of a code-based audience

You can use JavaScript to define the minimum screen height so that a large image in a variation is only shown to visitors who can see the full image on page load. In this example, the audience must have a screen height of 760px or more.

(function() {
  return window.innerHeight >= 760;
})();