Dev guideAPI Reference
Dev guideAPI ReferenceUser GuideGitHubNuGetDev CommunitySubmit a ticketLog In
GitHubNuGetDev CommunitySubmit a ticket

Advanced Audience Targeting segment qualification methods for the JavaScript (Browser) SDK

Use the Advanced Audience Targeting segment methods to fetch external audience mapping for the user. You can fetch user segments with the user identifier of the user context.



Advanced Audience Targeting is currently in beta. Apply on the Optimizely beta signup or contact your Customer Success Manager.


Minimum SDK Version



Fetches all qualified segments for the user context.

fetchQualifiedSegments is a method of the UserContext object. See OptimizelyUserContext for details.


The following table describes the parameters for the fetchQualifiedSegments method:

options (optional)OptimizelySegmentOption[]A set of options for fetching qualified segments from ODP.


This method returns a Promise<bool>, which will resolve with true if segments are fetched successfully, and false otherwise.



You can read and write directly to the qualified segments array. This allows for bypassing the remote fetching process from ODP or for utilizing your own fetching service. This can be helpful when testing or debugging.

Example fetchQualifiedSegments

The following is an example of calling the fetchQualifiedSegments method and accessing the returned completion object:

const attributes = { "app_version": "1.3.2" };

const user = optimizely.createUserContext("user123", attributes);

const asyncFunction = async () => {
  const fetched = await user.fetchQualifiedSegments()
Β  console.log(fetched) // true
Β  console.log(user.qualifiedSegments) // Updated qualified segments for target user

Once the segments have been fetched, they are cached. This means that if the same user is requesting the segments again (when new user contexts are created), the audience segment information can be retrieved from the cache instead of being fetched again from the remote ODP server.

If you would like to bypass caching, you can add the following options to your options array:

  • OptimizelySegmentOption.IGNORE_CACHE – bypass segments cache for lookup and save
  • OptimizelySegmentOption.RESET_CACHE – reset all segments cache

fetchQualifiedSegments network diagram

The following diagram shows the network calls between your application, the JavaScript (Browser) SDK, and the ODP server when calling fetchQualifiedSegments:

fetchQualifiedSegments network diagram between JavaScript (Browser) SDK and ODP server
  1. Call fetchQualifiedSegments method.
  2. JavaScript (Browser) SDK makes GraphQL call to ODP to fetch segments.
  3. ODP responds with segments.
  4. Fetched segments mapping user IDs to segments are cached.
  5. Appropriate variations are returned for user.





Check if the user is qualified for the given segment.


The following table describes the parameters for the isQualified method:

segmentStringThe ODP audience segment name to check if the user is qualified for


true if the user is qualified.


The following is an example of whether or not the user is qualified for an ODP segment:

const attributes = { "laptop_os": "mac" };
const user = optimizely.createUserContext("user123", attributes);

const isQualified = user.isQualifiedFor('segment1');
console.log(isQualified); // true

Source files

The language and platform source files containing the implementation for the JavaScript (Browser) SDK are available on Github.