Update from older versions of the Go SDK
Describes how to update from older versions of the Optimizely Full Stack (Legacy) Go SDK to Optimizely Feature Experimentation.
This section provides code examples for how Optimizely recommends leveraging the new Decision and Event Tracking APIs. All existing Full Stack (Legacy) methods and implementation are still supported. See Optimizely Feature Experimentation - application & migration documentation for the latest updates.
Optimizely recommends adopting the new Decide
, Decide All
, and Track Event
methods as a more flexible and easier-to-use replacement where you currently use isFeatureEnabled
, getFeatureVariable
, getAllFeatures
, or Track
calls within your implementation.
The following are examples of how to migrate the Full Stack (Legacy) methods to Feature Experimentation methods:
// -------------------------------
// Prereq for new methods: create a user
// ------------------------------
user := optlyClient.CreateUserContext("user_123", map[string]interface{}{"is_logged_in": true})
// -------------------------------
// Is Feature Enabled
// ------------------------------
// old method
enabled, _ := optlyClient.IsFeatureEnabled("flag_1", entities.UserContext{ID: "user_123", Attributes: map[string]interface{}{"is_logged_in": true}})
// new method
decision := user.Decide("flag_1", []decide.OptimizelyDecideOptions{})
enabled = decision.Enabled
// -------------------------------
// Activate & Get Variation
// ------------------------------
// old method
variationKey, _ := optlyClient.Activate("experiment_1", entities.UserContext{ID: "user_123"})
// new method
variationKey = decision.VariationKey
// -------------------------------
// Get All Feature Variables
// ------------------------------
// old method
allVarValues, _ := optlyClient.GetAllFeatureVariables("flag_1", entities.UserContext{ID: "user_123"})
// new method
allVarValues = decision.Variables
// -------------------------------
// Get Enabled Features
// ------------------------------
// old method
enabledFlags, _ := optlyClient.GetEnabledFeatures(entities.UserContext{ID: "user_123"})
// new method
decisions := user.DecideAll([]decide.OptimizelyDecideOptions{decide.EnabledFlagsOnly})
enabledFlags = []string{}
for k := range decisions {
enabledFlags = append(enabledFlags, k)
}
// -------------------------------
// Track
// ------------------------------
// old method
optlyClient.Track("my_purchase_event_key", entities.UserContext{ID: "user_123", Attributes: attributes}, map[string]interface{}{"purchase_count": 2})
// new method
user.TrackEvent("my_purchase_event_key", map[string]interface{}{"purchase_count": 2})
Updated about 1 year ago