Disclaimer: This website requires Please enable JavaScript in your browser settings for the best experience.

The availability of features may depend on your plan type. Contact your Customer Success Manager if you have any questions.

Dev guideRecipesAPI Reference
Dev guideAPI ReferenceUser GuideLegal TermsGitHubDev CommunityOptimizely AcademySubmit a ticketLog In
Dev guide

Real-Time Segments for Feature Experimentation segment qualification methods for the JavaScript (Node) SDK

Use the Real-Time Segments for Feature Experimentation segment methods to fetch external audience mapping for the user. You can fetch user segments with the user identifier of the user context.

Prerequisites

You must enable and configure Real-Time Segments for Feature Experimentation before fetching qualified segments and checking if the user is qualified for the given audience segment.

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
}

After the segments are 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.

The cache is used for the fetchQualifiedSegments call. This method is called on the user context (the user context is fixed, including the real-time segments the user qualifies for).

The cache only applies when calling the fetchQualifiedSegments call. If you set the cache timeout to 0, the cache is disabled. Optimizely uses the LRU algorithm, so the oldest record is bumped out when the maximum size is reached. If there is a cache miss upon the method call, Optimizely makes a network request.

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 the fetchQualifiedSegments method.
  2. JavaScript (Node) SDK makes a 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 the 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.