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
- Go to the Insights tab
- Click Audiences
- Click New Audience
- Click Code Your Audience
- Enter a descriptive Audience name
- Enter your audience’s JavaScript code
- 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.
- Go to the Insights tab
- Click Audiences
- Choose an audience to modify
- Make your desired changes
- 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;
})();