Advanced Audience 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.
Beta
Advanced Audience Targeting segment qualification methods are in beta. Apply on the Optimizely beta signup page or contact your Customer Success Manager.
fetchQualifiedSegments
Minimum SDK Version
4.0.0-beta
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 will be blocked until the synchronous fetch has been completed.
- The caller of asynchronous API will not be blocked.
fetchQualfiedSegments
is a method of the UserContext
object. See OptimizelyUserContext for details.
Parameters
The following table describes the parameters for the fetchQualifiedSegments
method:
Parameter | Type | Description |
---|---|---|
options (optional) | String | A set of options for fetching qualified segments from ODP. |
completion (for asynchronous calls) | Callback function | A 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 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
call
fetchQualifiedSegments
callThe 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 network calls between your application, the Java SDK, and the ODP server when calling fetchQualifiedSegments
is demonstrated below:

- Call
fetchQualifiedSegments
method. - Java SDK makes GraphQL call to ODP to fetch segments.
- ODP responds with segments.
- Fetched segments mapping user IDs to segments are cached, see below.
- Appropriate variations are returned for user.
The SDK catches 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.
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-beta
Description
Check if the user is qualified for the given audience segment.
Parameters
The following table describes the parameters for the isQualifiedFor
method:
Parameter | Type | Description |
---|---|---|
segment | String | The 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/platform source files containing the implementation for Java are available on GitHub.
Updated 19 days ago