Dev guideAPI Reference
Dev guideAPI ReferenceUser GuideGitHubNuGetDev CommunitySubmit a ticketLog In
GitHubNuGetDev CommunitySubmit a ticket

OptimizelyUserContext for the Swift SDK

Describes the OptimizelyUserContext object for the Swift SDK, which allows you to make flag decisions and track events for a user context in Optimizely Feature Experimentation.

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

Additionally, if you have enabled Advanced Audience Targeting, you can evaluate if your user qualifies for an audience segment.

OptimizelyUserContext minimum SDK version

SDK version 3.7 and higher.

Forced decision methods minimum SDK version

setForcedDecision(), getForcedDecision(), removeForcedDecision() and removeAllForcedDecisions() methods are supported on SDK version 3.10.0 and higher.

Advanced Audience Targeting minimum SDK version

fetchQualifiedSegments() and isQualifiedFor() methods are supported on version 4.0.0 and higher.

πŸ‘

Beta

Advanced Audience Targeting is in beta. Apply on the Optimizely beta signup page or contact your Customer Success Manager.

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 methods require the Advanced Audience Targeting integration. 
  // See note below code sample. 
  //

  // An array of segment names that the user is qualified for. 
  // The result of **fetchQualifiedSegments()** will be saved here. 
  // Can be nil if not properly updated with fetchQualifiedSegments(). 
  // 
  //  You can read and write directly to the qualified segments array. 
  //  This allows for bypassing the remote fetching process from ODP 
  //  or for utilizing your own fetching service.   
  public var qualifiedSegments: [String]? 

  // Fetch all qualified segments for the user context. 
  //
  // The segments fetched will be saved in **qualifiedSegments** and can be accessed any time.
  // On failure, **qualifiedSegments** will be nil and one of these errors will be returned:
  // - OptimizelyError.invalidSegmentIdentifier
  // - OptimizelyError.fetchSegmentsFailed(String)
  //
  // - Parameters:
  //   - options: A set of options for fetching qualified segments (optional).
  //   - completionHandler: A completion handler to be called with the fetch result. 
  //     On success, it will pass a non-nil segments array (can be empty) with a nil error. 
  //     On failure, it will pass a non-nil error with a nil segments array.
  public func fetchQualifiedSegments(options: [OptimizelySegmentOption] = [],
                                     completionHandler: @escaping ([String]?, OptimizelyError?) -> Void)


  //  Check is the user qualified for the given segment. 
  // - Parameter segment: the segment name to check qualification for.
  // - Returns: true if qualified.
  public func isQualifiedFor(segment: String) -> Bool
}

πŸ“˜

Note

You must enable the Advanced Audience Targeting integration to access thequalifiedSegments variable and fetchQualifiedSegments() and isQualifiedFor() methods.

Properties

The following table shows attributes for the OptimizelyUserContext object:

AttributeTypeComment
userIdStringThe ID of the user
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
setAttributePass a custom user attribute as a key-value pair to the user context.
decideReturns a decision result for a flag key for a user in an OptimizelyDecision object containing all data required to deliver the flag rule.
See Decide methods.
decide for specified keysReturns a map of flag decisions for specified flag keys.
See Decide methods.
decideAllReturns decisions for all active (unarchived) flags for a user.
See Decide methods.
trackEventTracks 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.
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.
fetchQualifiedSegments **Fetch all Optimizely Data Platform (ODP) real-time segments that the user context qualified for. See Advanced Audience Targeting segment qualification methods.
isQualifiedFor **Check if the user context qualified for a given ODP real-time segment. See Advanced Audience Targeting segment qualification methods.

** Requires the Advanced Audience Targeting integration.

See also

Create User Context

Source files

The language and platform source files containing the implementation for Swift are available on GitHub.