OptimizelyUserContext for the C# SDK
Describes the OptimizelyUserContext object for the Optimizely Feature Experimentation C# SDK, which allows you to make flag decisions and track events for a user context.
TheOptimizelyUserContext object lets you make flag decisions and track events for a user context that you have already created using theCreate User Context method.
OptimizelyUserContext minimum SDK version
SDK v3.7 and higher.
Forced decision methods minimum SDK version
SetForcedDecision(),GetForcedDecision(),RemoveForcedDecision() andRemoveAllForcedDecisions() methods are supported on v3.11.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 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 Real-Time Audiences for Feature Experimentation. * See the note following the code sample. */ // An array of segment names that the user is qualified for. // The result of **fetchQualifiedSegments()** is saved here. // Can be null 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 for utilizing your own fetching service. public List<string> GetQualifiedSegments(); // Set the qualified audiences that the user is qualified for. public void SetQualifiedSegments(List<string> qualifiedSegments); // Fetch all qualified segments for the user context. // The audiences fetched are saved in the **QualifiedSegments** array // and can be accessed any time. public bool FetchQualifiedSegments(List<OdpSegmentOption> segmentOptions = null); // Fetch all qualified audiences 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 Task FetchQualifiedSegments(Action<bool> callback, List<OdpSegmentOption> segmentOptions = null); // Check is the user is qualified for the given segment. public bool IsQualifiedFor(string segment);}📘
NoteYou must first enable and configure Real-Time Audiences for Feature Experimentation to use the
GetQualifiedSegments(),SetQualifiedSegments(),FetchQualifiedSegments(), andIsQualifiedFor()methods. SeeReal-Time Audiences for Feature Experimentation.
Properties
Attribute | Type | Comment |
|---|---|---|
userId | string | The ID of the user |
(optional)attributes | Dictionary | A dictionary of custom key-value pairs specifying attributes for the user 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 |
|---|---|
SetAttribute | Pass a custom user attribute as a key-value pair to the user context. |
GetAttributes | Get attributes for the user |
Decide | 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 |
DecideForKeys | Returns a map of flag decisions for specified flag keys.SeeDecide methods |
DecideAll | Returns decisions for all active (unarchived) flags for a user.SeeDecide methods |
TrackEvent | 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 |
SetForcedDecision | Forces a user into a specific variation.SeeSet Forced Decision |
GetForcedDecision | Returns what variation the user was forced into.SeeGet Forced Decision |
RemoveForcedDecision | Removes a user from a specific forced variation.SeeRemove Forced Decision |
RemoveAllForcedDecisions | Removes a user from all forced variations.SeeRemove All Forced Decisions |
FetchQualifiedSegments ** | Fetch all Optimizely Data Platform (ODP) real-time audiences for which the user context qualified. SeeReal-Time Audiences for Feature Experimentation segment qualification methods for the C# SDK. |
IsQualifiedFor ** | Check if the user context qualifies for a given ODP real-time segment. SeeReal-Time Audiences for Feature Experimentation segment qualification methods for the C# SDK. |
** RequiresReal-Time Audiences for Feature Experimentation.
See also
Create User ContextForced Decision methods
Source files
The language and platform source files containing the implementation for C# are available onGitHub.
Updated 17 days ago