GuidesAPI Reference
Submit Documentation FeedbackJoin Developer CommunityOptimizely GitHubOptimizely NuGetLog In


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

OptimizelyUserContext version

OptimizelyUserContext is supported on SDK v3.7 and higher.

Forced decision methods version

setForcedDecision(), getForcedDecision(), removeForcedDecision() and removeAllForcedDecisions() methods are supported on v3.10.0 and higher.

OptimizelyUserContext definition

The following code shows the object definition for OptimizelyUserContext:

public class OptimizelyUserContext {
  public var userId: String
  public var attributes: [String: Any?]
  public init(optimizely: OptimizelyClient,
              userId: String, 
              attributes: [String: Any]? = nil)
  // set an attribute for the user
  public func setAttribute(key: String, value: Any) -> Bool    
  // make a decision about which flag variation the user buckets into for the flag key 
  public func decide(key: String, 
                     options: [OptimizelyDecideOption]? = nil) -> OptimizelyDecision
  // make decisions about which flag variations the user buckets into for flag keys
  public func decide(keys: [String],                
                    options: [OptimizelyDecideOption]? = nil) -> [String: OptimizelyDecision]
  // make decisions about which flag variations the user buckets into for all flags
  public func decideAll(options: [OptimizelyDecideOption]? = nil) -> [String: OptimizelyDecision]
  // track user event
  public func trackEvent(eventKey: String, eventTags:  [String: Any]? = nil)
  // OptimizelyDecisionContext
  public struct OptimizelyDecisionContext {
    public let flagKey: String
    public let ruleKey: String?
  // OptimizelyForcedDecision
  public struct OptimizelyForcedDecision {
    public let variationKey:String
  // sets a forced decision (variationKey) for a given decision context (flagKey and optional ruleKey)
  public func setForcedDecision(context: OptimizelyDecisionContext, 
                                decision: OptimizelyForcedDecision) -> Bool
  // returns the forced decision (variationKey) for a given decision context (flagKey and optional ruleKey)
  public func getForcedDecision(context: OptimizelyDecisionContext) -> OptimizelyForcedDecision?

  // removes the forced decision (variationKey) for a given decision context (flagKey and optional ruleKey)
  public func removeForcedDecision(context: OptimizelyDecisionContext) -> Bool

  // removes all the forced decisions (variationKeys) for the user context
  public func removeAllForcedDecisions() -> Bool


The following table shows attributes for the OptimizelyUserContext object:






The ID of the user

(optional) attributes


A 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.


The following table shows methods for the OptimizelyUserContext object:




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. The decision result is returned in an OptimizelyDecision object, and contains all data required to deliver the flag rule.
See Decide methods

decide for specified keys

Returns a map of flag decisions for specified flag keys.
See Decide methods


Returns decisions for all active (unarchived) flags for a user.
See Decide methods


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


Forces a user into a specific variation.
See Set Forced Decision


Returns what variation the user was forced into.
See Get Forced Decision


Removes a user from a specific forced variation.
See Remove Forced Decision


Removes 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 Swift is OptimizelyClient.swift.