HomeGuidesAPI Reference
Submit Documentation FeedbackJoin Developer CommunityOptimizely GitHubOptimizely NuGetLog In

OptimizelyUserContext

This topic describes the OptimizelyUserContext Object which allows you to make flag decisions and track events for a user context.

The OptimizelyUserContext object allows you to make flag decisions and track events for a user context you already created using the Create User Context method.

OptimizelyUserContext version

1.0.0-beta or higher

Forced decision methods version

setForcedDecision(), getForcedDecision(), removeForcedDecision() and removeAllForcedDecisions() methods are supported on version 1.0.0-beta and above.

OptimizelyUserContext definition

The following code shows the object definition for OptimizelyUserContext:

class OptimizelyUserContext {
  // TODO : Add constructor code

  // set attributes for the user
  Future<BaseResponse> setAttributes(Map<String, dynamic> attributes)

  // make a decision about which flag variation the user buckets into for the flag key
  Future<DecideResponse> decide(String key, [Set<OptimizelyDecideOption> options = const {}])

  // make decisions about which flag variations the user buckets into for flag keys 
  Future<DecideForKeysResponse> decideForKeys(List<String> keys, [Set<OptimizelyDecideOption> options = const {}])

  // make decisions about which flag variations the user buckets into for all flags
  Future<DecideForKeysResponse> decideAll([Set<OptimizelyDecideOption> options = const {}])

  // track user event
  Future<BaseResponse> trackEvent(String eventKey, [Map<String, dynamic> eventTags = const {}])

  // sets a forced decision (variationKey) for a given decision context
  Future<BaseResponse> setForcedDecision(OptimizelyDecisionContext context, OptimizelyForcedDecision decision)

  // returns the forced decision (variationKey) for a given decision context
  Future<GetForcedDecisionResponse> getForcedDecision(OptimizelyDecisionContext context)

  // removes the forced decision (variationKey) for a given decision context
  Future<BaseResponse> removeForcedDecision(OptimizelyDecisionContext context)

  // removes all the forced decisions (variationKeys) for the user context
  Future<BaseResponse> removeAllForcedDecisions()
}

Properties

The following table shows attributes for the OptimizelyUserContext object:

AttributeTypeComment
userIdStringThe ID of the user
(optional) attributesMapA map of custom key-value pairs specifying attributes for the user that are used for audience targeting. You can pass the map with the user ID when you create the user.

Methods

The following table shows methods for the OptimizelyUserContext object:

MethodComment
setAttributesPass the custom user attributes as key-value pairs in a map to the user context.
decideReturns a decide response which contains decision result for a flag key for a user. The system returns the decision result in a Decision object, and contains all data required to deliver the flag rule.
See Decide methods
decide for specified keysReturns the decide for keys response object, which contains map of flag decisions for specified flag keys.
See Decide methods
decideAllReturns the decide for keys response object, which contains decisions for all active (unarchived) flags for a user.
See Decide methods
trackEventReturns BaseResponse containing bool variable of success and string reason variable, which will be empty in case of event tracked successfully. Tracks a conversion event (in other words, an action a user takes) for a user. Logs an error message if the specified event key doesn't match any existing events.
See Track Event
setForcedDecisionForces a user into a specific variation.
See Set Forced Decision
getForcedDecisionReturns what variation the user was forced into.
See Get Forced Decision
removeForcedDecisionRemoves a user from a specific forced variation.
See Remove Forced Decision
removeAllForcedDecisionsRemoves a user from all forced variations.
See Remove All Forced Decisions

See Also

Create User Context

Source files

The language/platform source files containing the implementation for Flutter Android is at Optimizely.java and for Flutter iOS is at OptimizelyClient.swift.