Dev guideAPI Reference
Dev guideAPI ReferenceUser GuideGitHubNuGetDev CommunityDoc feedbackLog In

Advanced Audience Targeting segment qualification methods

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 (asynchronous)

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)StringA set of options for fetching qualified segments

Returns

This method does not provide a return value. On failure, one of these errors will be thrown:

  • On success, it will pass a nil error.
  • On failure, it will pass the error message:ERROR_MESSAGES.FETCH_SEGMENTS_FAILED_INVALID_IDENTIFIER or hard-coded in the local file to be Audience segments fetch failed, <ERROR_TYPE> where <ERROR_TYPE> can be:
    • Parameters apiKey or apiHost invalid
    • Network error
    • Decode error

📘

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 (asynchronous)

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 = optimizelyClient.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 odpSegmentOptions array:

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

fetchQualifiedSegments (synchronous)

Minimum SDK Version

5.0.0-beta

Description

This is a synchronous version of the fetchQualifiedSegments asynchronous version above.

📘

Note

This call will block the calling thread until fetching is completed.

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 does not provide a return value. On failure, one of these errors will be thrown:

  • OptimizelyError.invalidSegmentIdentifier
  • OptimizelyErcor-fetchSegmentsFailed(String)

Example fetchQualifiedSegments (synchronous)

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 = optimizelyClient.createUserContext("user123", attributes);

const syncFunction = () => {
  const fetched = user.fetchQualifiedSegments()
  console.log(fetched) // Promise (Pending)
  fetched.then(result => {
    console.log(result) // true
    console.log(user.qualifiedSegments) // Updated qualified segments for target 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 = optimizelyClient.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.