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.
The OptimizelyUserContext
object lets you 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.
Real-Time Segments for Feature Experimentation minimum SDK version
FetchQualifiedSegments()
and IsQualifiedFor()
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 Segments for Feature Experimentation.
* 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();
// Set the qualified segments that the user is qualified for.
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 is qualified for the given segment.
public bool IsQualifiedFor(string segment);
}
Note
You must first enable and configure Real-Time Segments for Feature Experimentation to use the
GetQualifiedSegments()
,SetQualifiedSegments()
,FetchQualifiedSegments()
, andIsQualifiedFor()
methods. See Real-Time Segments 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 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:
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. See Decide methods |
DecideForKeys | Returns a map of flag decisions for specified flag keys. See Decide methods |
DecideAll | Returns decisions for all active (unarchived) flags for a user. See Decide 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. See Track Event |
SetForcedDecision | Forces a user into a specific variation. See Set Forced Decision |
GetForcedDecision | Returns what variation the user was forced into. See Get Forced Decision |
RemoveForcedDecision | Removes a user from a specific forced variation. See Remove Forced Decision |
RemoveAllForcedDecisions | Removes a user from all forced variations. See Remove All Forced Decisions |
FetchQualifiedSegments ** | Fetch all Optimizely Data Platform (ODP) real-time segments for which the user context qualified. See Real-Time Segments for Feature Experimentation segment qualification methods for the C# SDK. |
IsQualifiedFor ** | Check if the user context qualifies for a given ODP real-time segment. See Real-Time Segments for Feature Experimentation segment qualification methods for the C# SDK. |
** Requires Real-Time Segments for Feature Experimentation.
See also
Create User Context
Forced Decision methods
Source files
The language and platform source files containing the implementation for C# are available on GitHub.
Updated 8 months ago