Skip to content

Configure Targeting and Audiences

⏱ 20 minutes intermediate

Running an experiment on every visitor dilutes results and wastes traffic. Audience targeting lets you restrict experiments to the users who matter — paid subscribers, mobile visitors, specific geographies — so you get cleaner signals and protect irrelevant segments from unfinished experiences.

  1. Define user attributes that describe your audience
  2. Build audience conditions in Optimizely
  3. Apply the audience to an experiment
  4. Verify targeting resolves correctly

Attributes are key-value pairs that describe a user. Both products use attributes for targeting, but the mechanism differs.

Feature Experimentation: pass attributes in code

Section titled “Feature Experimentation: pass attributes in code”
Create a user context with targeting attributes
javascript
const user = optimizely.createUserContext('user-456', {
  plan: 'enterprise',
  country: 'DE',
  lifetime_value: 12500,
  is_beta_tester: true,
});

const decision = user.decide('redesigned_dashboard');
python
user = client.create_user_context('user-456', {
    'plan': 'enterprise',
    'country': 'DE',
    'lifetime_value': 12500,
    'is_beta_tester': True,
})

decision = user.decide('redesigned_dashboard')
csharp
var user = optimizely.CreateUserContext("user-456", new UserAttributes
{
    { "plan", "enterprise" },
    { "country", "DE" },
    { "lifetime_value", 12500 },
    { "is_beta_tester", true },
});

var decision = user.Decide("redesigned_dashboard");

Web Experimentation: set attributes via the JavaScript API

Section titled “Web Experimentation: set attributes via the JavaScript API”
Set custom attributes for Web Experimentation
javascript
window.optimizely = window.optimizely || [];

window.optimizely.push({
  type: 'user',
  attributes: {
    plan: 'enterprise',
    country: 'DE',
    lifetime_value: 12500,
  },
});
  1. Navigate to Audiences in the Optimizely application
  2. Click Create New Audience
  3. Name the audience descriptively (e.g., “Enterprise users in EMEA”)
  4. Add conditions using the condition builder:
ConditionSourceExample
Custom attributePassed via SDK or snippetplan equals premium
DeviceDetected automatically (Web)Device type is mobile
BrowserDetected automatically (Web)Browser is Chrome
LocationIP-based geolocation (Web)Country is United States
Third-party integrationPlatforms like Salesforce or BlueKaiSegment membership
  1. Combine conditions with AND / OR / NOT logic:
    • AND — All conditions must be true (narrows the audience)
    • OR — Any condition can be true (broadens the audience)
    • NOT — Negates a condition (excludes a segment)
    • Example: (country = "US" OR country = "CA") AND plan = "premium" targets premium users in North America
  2. Save the audience

Available operators depend on the attribute type: strings support is, is not, contains; numbers support greater than, less than, equals; booleans support is true / is false.

Step 3: Apply the audience to an experiment

Section titled “Step 3: Apply the audience to an experiment”
  1. Open your experiment and navigate to the Audiences section
  2. Click Add Audience and select the audience you created
  3. To combine multiple audiences, choose AND (user must match all) or OR (user must match any)
  4. Save the experiment

Traffic allocation applies only to users who pass the audience filter. If you allocate 50% traffic and apply an audience that matches 20% of visitors, roughly 10% of total visitors enter the experiment.

Verify audience targeting in code
javascript
const user = optimizely.createUserContext('test-user', {
  plan: 'enterprise',
  country: 'DE',
});

const decision = user.decide('redesigned_dashboard');
console.log('Enabled:', decision.enabled);
console.log('Variation:', decision.variationKey);
console.log('Reasons:', decision.reasons);
// Enable decision reasons for debugging:
// user.decide('redesigned_dashboard', ['INCLUDE_REASONS'])
python
user = client.create_user_context('test-user', {
    'plan': 'enterprise',
    'country': 'DE',
})

decision = user.decide('redesigned_dashboard')
print('Enabled:', decision.enabled)
print('Variation:', decision.variation_key)
print('Reasons:', decision.reasons)
csharp
var user = optimizely.CreateUserContext("test-user", new UserAttributes
{
    { "plan", "enterprise" },
    { "country", "DE" },
});

var decision = user.Decide("redesigned_dashboard");
Console.WriteLine($"Enabled: {decision.Enabled}");
Console.WriteLine($"Variation: {decision.VariationKey}");

Open your page with the Optimizely browser extension active. The extension displays which experiments are running, which audiences matched, and which variation was served.

IssueCauseFix
User not entering experimentAttribute value missing or wrong typeLog attributes before the decide call and compare with audience conditions
Audience matches nobodyCondition logic too restrictiveTest with a single condition first, then add constraints
Web attributes not recognizedAttributes pushed after snippet evaluatesPush attributes before the snippet loads or use the activate event