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

Advanced Audience Targeting segment qualification methods for the JavaScript (Node) 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.

👍

Beta

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

fetchQualifiedSegments

Minimum SDK version

5.0.0

Description

Fetches all qualified segments for the user context.

fetchQualfiedSegments 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

Returns

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

📘

Note

You can read and write directly to the qualified segments array. This allows for bypassing the remote fetching process from Optimizely Data Platform (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 (Node) SDK, and the ODP server when calling fetchQualifiedSegments:

JavaScript (Node) fetchQualifiedSegments network diagram to ODP
  1. Call fetchQualifiedSegments method.
  2. JavaScript (Node) 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

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 = fsUser.isQualifiedFor(’segment1');
console.log(isQualified); // true

Source files

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