Dev guideAPI Reference
Dev guideAPI ReferenceUser GuideGitHubNuGetDev CommunityDoc feedbackLog In


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 that you have already created using the Create User Context method.

OptimizelyUserContext minimum SDK version

SDK v3.7 and higher.

Forced decision methods minimum SDK version

SetForcedDecision(), GetForcedDecision(), RemoveForcedDecision() and RemoveAllForcedDecisions() methods are supported on v3.11.0 and higher.

Advanced Audience Targeting minimum SDK version



Advanced Audience Targeting is currently in beta. Contact your Customer Success Manager for more information or register now on

FetchQualifiedSegments() and IsQualifiedFor() methods are supported on version 4.0.0-beta and higher. You must also have the Advanced Audience Targeting integration enabled.

OptimizelyUserContext Updates

public class OptimizelyUserContext {
    public OptimizelyUserContext(Optimizely optimizely, 
                               string userId,
                               UserAttributes userAttributes, 
                               ForcedDecisionsStore forcedDecisionsStore, 
                               IErrorHandler errorHandler, ILogger logger)
    // set an attribute for the user
    public void SetAttribute(string key, object value)

    // get attributes for the user
    public UserAttributes GetAttributes()

    // make a decision about which flag variation the user buckets into for the flag key 
    public OptimizelyDecision Decide(string key, OptimizelyDecideOption[] options)

    // make decisions about which flag variations the user buckets into for flag keys
    public Dictionary<string, OptimizelyDecision> DecideForKeys(string[] keys, OptimizelyDecideOption[] options)

    // make decisions about which flag variations the user buckets into for all flags
    public Dictionary<string, OptimizelyDecision> DecideAll(OptimizelyDecideOption[] options)

    // track user event
    public void TrackEvent(string eventName, EventTags eventTags)

    // sets a forced decision (variationKey) for a given decision context
    public bool 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 bool RemoveForcedDecision(OptimizelyDecisionContext context)

    // removes all the forced decisions (variationKeys) for the user context
    public bool RemoveAllForcedDecisions()

    // The following methods require the Audience targeting with Optimizely Data Platform 
    // integration enabled. 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 null 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 List<string> GetQualifiedSegments()
    public void SetQualifiedSegments(List<string> qualifiedSegments)

    // Fetch all qualified segments for the user context. 
    // The segments fetched will be saved in the **QualifiedSegments** array
    // and can be accessed any time. 
    public bool FetchQualifiedSegments(List<OdpSegmentOption> segmentOptions = null)

    // Fetch all qualified segments for the user context in a non-blocking manner. 
    // This method will fetch segments in a separate thread and invoke the provided 
    // callback when results are available.
    public Task FetchQualifiedSegments(Action<bool> callback, List<OdpSegmentOption> segmentOptions = null) 

    //  Check is the user qualified for the given segment. 
    public bool IsQualifiedFor(string segment)



You must first enable the Optimizely Data Platform Advanced Audience Targeting integration to access theGetQualifiedSegments(), SetQualifiedSegments(), FetchQualifiedSegments(), and IsQualifiedFor() methods. Refer to Advanced Audience Targeting with Optimizely Data Platform (ODP) for more information.


userIdstringThe ID of the user
DictionaryA dictionary 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:

SetAttributePass a custom user attribute as a key-value pair to the user context.
GetAttributesGet attributes for the user
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
DecideForKeysReturns 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
GetQualifiedSegments **Returns a copy of the current list of qualified segments. See ODP Audience Segment Methods.
SetQualifiedSegments **Clears and adds a collection of qualified segments. See ODP Audience Segment Methods.
FetchQualifiedSegments **Fetch all ODP real-time segments that the user context qualified for. See ODP Audience Segment Methods.
IsQualifiedFor **Check if the user context qualified for a given ODP real-time segment. ODP Audience Segment Methods.

** Requires the Optimizely Data Platform Audience targeting integration.

See also

Create User Context
Forced Decision methods

Source files

The language/platform source files containing the implementation for C# are at Optimizely.cs.