Optimizely Data Platform audience segment qualification methods
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.
Note
Advanced Audience Targeting and audience segment qualification methods are currently in beta. Contact your Customer Success Manager for more information or register now on Optimizely.com.
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 ODP server. The Optimizely Feature Experimentation Java SDK provides a synchronous and asynchronous version of the Optimizely Data Platform (ODP) fetchQualifiedSegments
method.
- The SDK blocks the caller until it has completed the synchronous fetch.
- The SDK does not block the caller of asynchronous API.
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 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 SDK catches any fetched segments. This means that if the same user requests the 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. --comment still not sure if it's the SDK or "you" here
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 3 days ago