HomeGuidesAPI Reference
Submit Documentation FeedbackJoin Developer CommunityLog In

Updating usage from older versions

This topic describes how to update from older versions of the Optimizely C# SDK.

Code examples

This section provides code examples for how we recommend leveraging our new Decision and Event Tracking APIs. All existing methods and implementation are still included and supported and will only be removed after deprecation marking and with a future Major version.

We recommend adopting the new Decide, Decide All and Track Event methods as more flexible and easier to use replacements for cases where you currently use isFeatureEnabled, getFeatureVariable, getAllFeatures or Track calls within your implementation.

Refer to an earlier version of the SDK reference guides for earlier methods.

The following are some examples of how to migrate older methods to newer methods.

// -------------------------------
// Prereq for new methods: create a user
// ------------------------------
UserAttributes attributes = new UserAttributes();
attributes.Add("is_logged_in", true);
OptimizelyUserContext user = optimizely.CreateUserContext("user_123", attributes);


// -------------------------------
// Is Feature Enabled
// ------------------------------
// old method

bool enabled = optimizely.IsFeatureEnabled("flag_1", "user_123", attributes);

// new method
OptimizelyDecision decision = user.Decide("flag_1");
bool enabled = decision.Enabled;


// -------------------------------
// Activate & Get Variation
// ------------------------------
// old method
Variation variation = optimizely.Activate("experiment_1", "user_123", attributes);

// new method
string variationKey = decision.VariationKey;


// -------------------------------
// Get All Feature Variables
// ------------------------------
// old method
OptimizelyJSON json = optimizely.GetAllFeatureVariables("flag_1",
                                                        "user_123",
                                                        attributes);
 
// new method
var json = decision.Variables;


// -------------------------------
// Get Enabled Features
// ------------------------------
// old method
var enabledFlags = optimizely.GetEnabledFeatures("user_123", 
                                                          attributes);

// new method
var options = new OptimizelyDecideOption[] { OptimizelyDecideOption.ENABLED_FLAGS_ONLY };
var decisions = user.DecideAll(options);
var enabledFlags = decisions.Keys;


// -------------------------------
// Track
// ------------------------------
// old method
var tags = new EventTags{
                { "revenue", 42 }
            };
attributes.Add("purchase_count", 2);
optimizely.Track("my_purchase_event_key", "user_123", attributes, tags);

// new method
user.TrackEvent("my_purchase_event_key", tags);

Activate method and A/B Tests

Like the existing Is Feature Enabled method, the Decide methods are based on feature flag keys, and do not support standalone A/B tests or Multi-armed Bandits. We are working to unify the data models and interfaces for our application longer term to mitigate the need to maintain multiple different access methods. In the meantime, you can still use the Activate and Get Variation methods for standalone A/B tests in your legacy projects alongside the Decide methods.


Did this page help you?