Dev guideAPI Reference
Dev guideAPI ReferenceUser GuideGitHubNuGetDev CommunityDoc feedbackLog In
GitHubNuGetDev CommunityDoc feedback

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

Use the Optimizely Data Platform (ODP) 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.

šŸ“˜

Note

Advanced Audience Targeting and the segment qualification methods are currently beta. Contact your Customer Success Manager for more information or register now on Optimizely.com.

fetchQualifiedSegments

Minimum SDK Version

5.0.0-beta

Description

Fetches all qualified segments for the user context.

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

Parameters

The following table describes the parameters for the fetchQualifiedSegments method:

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

Returns

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

šŸ“˜

Qualified segments array

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 network calls between your application, the JavaScript (Browser) SDK, and the ODP server when calling fetchQualifiedSegments is demonstrated below:

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.

isQualifiedFor

Version

5.0.0-beta

Description

Check if the user is qualified for the given segment.

Parameters

The following table describes the parameters for the isQualified method:

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

Returns

true if the user is qualified.

Examples

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/platform source files containing the implementation for the JavaScript (Browser) SDK is optimizely/index.ts.