OptimizelyUserContext for the Java SDK
Explains theOptimizelyUserContext object for the Java SDK, which lets you make flag decisions and track events for a user context in Optimizely Feature Experimentation.
TheOptimizelyUserContext object lets you make flag decisions and track events for a user context you have already created using theCreate User Context method.
Also, if you haveReal-Time Audiences for Feature Experimentation configured, you can evaluate if the user would qualify for an Optimizely Data Platform (ODP) real-time audience.
OptimizelyUserContext minimum SDK version
SDK v3.7 and higher.
Forced decision methods minimum SDK version
setForcedDecision(),getForcedDecision(),removeForcedDecision() andremoveAllForcedDecisions() methods are supported on version 3.10.0 and higher.
Real-Time Audiences for Feature Experimentation minimum SDK version
fetchQualifiedSegments() andisQualifiedFor() methods are supported on version 4.0.0 and higher.
OptimizelyUserContext definition
The following code shows the object definition forOptimizelyUserContext:
public class OptimizelyUserContext { public String getUserId() public Map<String, Object> getAttributes() public OptimizelyUserContext(Optimizely optimizely, String userId, Map<String, Object> attributes) // set an attribute for the user public void setAttribute(String key, Object value) // make a decision about which flag variation the user buckets into for the flag key public OptimizelyDecision decide(String key, List<OptimizelyDecideOption> options) // make decisions about which flag variations the user buckets into for flag keys public Map<String, OptimizelyDecision> decideForKeys(List<String> keys, List<OptimizelyDecideOption> options) // make decisions about which flag variations the user buckets into for all flags public Map<String, OptimizelyDecision> decideAll(List<OptimizelyDecideOption> options) // track user event public void trackEvent(String eventName, Map<String, ?> eventTags) // sets a forced decision (variationKey) for a given decision context public Boolean setForcedDecision(OptimizelyDecisionContext context, OptimizelyForcedDecision decision) // returns the forced decision (variationKey) for a given decision context public OptimizelyForcedDecision getForcedDecision(OptimizelyDecisionContext context) // removes the forced decision (variationKey) for a given decision context public Boolean removeForcedDecision(OptimizelyDecisionContext context) // removes all the forced decisions (variationKeys) for the user context public Boolean removeAllForcedDecisions() // // The following methods require Real-Time Audiences for Feature Experimentation. // See the note following the code sample. // // An array of audience segment names that the user is qualified for. // The result of **fetchQualifiedSegments()** is saved here. // Can be nil if not properly updated with fetchQualifiedSegments(). // // You can read and write directly to the qualified segments array. // This lets you bypass the remote fetching process from ODP // or utilize your own fetching service. private List<String> qualifiedSegments() // Fetch all qualified segments for the user context. // The segments fetched are saved in the **qualifiedSegments** array // and can be accessed any time. public Boolean fetchQualifiedSegments(List<ODPSegmentOption> segmentOptions) // Fetch qualified segments for the user context in a non-blocking manner. // This method fetches segments in a separate thread and invokes the provided // callback when results are available. public void fetchQualifiedSegments(ODPSegmentCallback callback, List<ODPSegmentOption> segmentOptions) // Check is the user qualified for the given segment. public Boolean isQualifiedFor(String segment)}📘
NoteYou must first configureReal-Time Audiences for Feature Experimentation to use the
qualifiedSegments(),fetchQualifiedSegments(), andisQualifiedFor()methods.
Properties
The following table shows the attributes for theOptimizelyUserContext object:
Attribute | Type | Comment |
|---|---|---|
| String | The ID of the user |
| Map | A map of custom key-value pairs specifying attributes for the user that are used foraudience targeting. You can pass the map with the user ID when you create the user. |
Methods
The following table shows methods for theOptimizelyUserContext object:
Method | Comment |
|---|---|
| Pass a custom user attribute as a key-value pair to the user context. |
| Returns a decision result for a flag key for a user in an OptimizelyDecision object containing all data required to deliver the flag rule.SeeDecide methods. |
| Returns decisions for all active (unarchived) flags for a user.SeeDecide methods. |
| Returns a map of flag decisions for specified flag keys.SeeDecide methods. |
| Tracks a conversion event for a user (an action a user takes) and logs an error message if the specified event key does not match any existing events.SeeTrack Event. |
| Forces a user into a specific variation.SeeSet Forced Decision. |
| Returns what variation the user was forced into.SeeGet Forced Decision. |
| Removes a user from a specific forced variation.SeeRemove Forced Decision. |
| Removes a user from all forced variations.SeeRemove All Forced Decisions. |
| Fetch all OD real-time audiences that the user context qualified for.SeeReal-Time Audiences for Feature Experimentation segment qualification methods for the Java SDK. |
| Check if the user context qualifies for a given ODP real-time audience.SeeReal-Time Audiences for Feature Experimentation segment qualification methods for the Java SDK. |
** RequiresReal-Time Audiences for Feature Experimentation.
See also
Source files
The language and platform source files containing the implementation for Java are available onGitHub.
Updated 17 days ago