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

Real-Time Segments for Feature Experimentation segment qualification methods for the Java SDK

Use the fetchQualifiedSegments method to retrieve the external audience mapping for the user from the Optimizely Data Platform (ODP) server. Use the isQualifiedFor method to check if the user qualifies for the specified segment.

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

4.0.0

Description

You can use the fetchQualifiedSegments method to retrieve the external audience mapping for a specific user from the Optimizely Data Platform (ODP) server. The Optimizely Feature Experimentation Java SDK provides a synchronous and asynchronous version of the fetchQualifiedSegments method.

  • The caller is blocked until the synchronous fetch is completed.
  • The caller of asynchronous API is not blocked.

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)StringA set of options for fetching qualified segments from ODP.
completion (for asynchronous calls)Callback functionA completion handler to be called with the fetch result.

Returns - Synchronous call

The fetchQualifiedSegments synchronous method returns true if the qualified segments array in the user context was updated.

Returns - Asynchronous call

  • If the fetch completes successfully, the Java SDK updates the qualified segments array in the user context and then calls the completion handler with a success status.
  • If the fetch fails, the SDK calls the handler with a failure status.

If the Java SDK does not find an ODP audience in the datafile, it returns an empty qualified segments array without sending a request to the ODP server.

πŸ“˜

Note

You can read and write directly to the qualified segments array instead of calling fetchQualifiedSegments.

This lets you bypass the remote fetching process from Optimizely Data Platform ODP or utilize your own fetching service. This can be helpful when testing or debugging.

Example fetchQualifiedSegments call

The code creates a user context by instantiating an OptimizelyUserContext object with a user ID "user123" and a set of attributes, which includes an "app_version" attribute with a value of "1.3.2".

The next section of the code shows the use of thefetchQualifiedSegments method to fetch all qualified segments with and without segment options.

After that, the code uses the decide method to make a decision on whether to show the feature flag with the key "flag1" to the user. Finally, the SDK calls the trackEvent method to track a custom event called "myevent".

Map<String, Object> attributes = new HashMap<>();
attributes.put("app_version", "1.3.2");
OptimizelyUserContext user = optimizely.createUserContext("user123", attributes);

// Without segment option 
Boolean response = user.fetchQualifiedSegments((Boolean isFetchSuccessful) -> {
  System.out.println(isFetchSuccessful);
            
  OptimizelyDecision decision = user.decide("flag1");
  user.trackEvent("purchase_event");
});

// With segment options
List<ODPSegmentOption> odpSegmentOptions = new ArrayList<>();
odpSegmentOptions.add(ODPSegmentOption.IGNORE_CACHE);
odpSegmentOptions.add(ODPSegmentOption.RESET_CACHE);

user.fetchQualifiedSegments((Boolean isFetchSuccessful) -> {
  System.out.println(isFetchSuccessful);
            
  OptimizelyDecision decision = user.decide("flag1");
  user.trackEvent("purchase_event");
}, odpSegmentOptions);
Map<String, Object> attributes = new HashMap<>();
attributes.put("app_version", "1.3.2");
OptimizelyUserContext user = optimizely.createUserContext("user123", attributes);

// Without segment options 
Boolean response = user.fetchQualifiedSegments();

// With segment options
List<ODPSegmentOption> odpSegmentOptions = new ArrayList<>();
odpSegmentOptions.add(ODPSegmentOption.IGNORE_CACHE);
odpSegmentOptions.add(ODPSegmentOption.RESET_CACHE);

Boolean response = user.fetchQualifiedSegments(odpSegmentOptions);

OptimizelyDecision decision = user.decide("flag1");
user.trackEvent("myevent");

The following diagram shows the network calls between your application, the Java SDK, and the ODP server when calling fetchQualifiedSegments:

Java SDK network call to ODP
  1. Call the fetchQualifiedSegments method.
  2. Java 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. See the following.
  5. Appropriate variations are returned for the user.

The SDK caches any fetched segments. This means that if you request the same user segments again (when creating new user contexts), the SDK can retrieve the audience segment information from the cache rather than 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.

You can bypass caching using the following options with your odpSegmentOptions array:

  • IGNORE_CACHE – Bypass segments cache for lookup and save.
  • RESET_CACHE – Reset all segments cache.

isQualifiedFor

Minimum SDK Version

4.0.0

Description

Check if the user is qualified for the given audience segment.

Parameters

The following table describes the parameters for the isQualifiedFor 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:

Map<String, Object> attributes = new HashMap<>();
attributes.put("laptop_os", "mac");
OptimizelyUserContext user = optimizely.createUserContext("user123", attributes);

Boolean response = user.fetchQualifiedSegments();
Boolean isQualified = user.isQualifiedFor("segment1");

Source files

The language and platform source files containing the implementation for the Java SDK are available on GitHub.