Updating usage from older versions
This topic describes how to update from older versions of the Optimizely Feature Experimentation JavaScript (Browser) SDK to Optimizely Feature Experimentation.
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
// ------------------------------
var attributes = {
is_loggin_in: true
};
var user = optimizelyClient.createUserContext('user123', attributes);
// -------------------------------
// Is Feature Enabled
// ------------------------------
// old method
var enabled = optimizelyClient.isFeatureEnabled('flag_1', 'user_123', attributes);
// new method
var decision = user.decide('flag_1');
var enabled = decision['enabled'];
// -------------------------------
// Activate & Get Variation
// ------------------------------
// old method
var variationKey = optimizelyClient.activate('experiment_1','user_123', attributes);
// new method
var variationKey = decision['variationKey'];
// -------------------------------
// Get All Feature Variables
// ------------------------------
// old method
var allVarValues = optimizelyClient.getAllFeatureVariables('flag_1', 'user_123');
// new method
var allVarValues = decision['variables'];
// -------------------------------
// Get Enabled Features
// ------------------------------
// old method
var enabledFlags = optimizelyClient.getEnabledFeatures('user_123');
// new method
var decisions = user.decideAll([OptimizelyDecideOption.ENABLED_FLAGS_ONLY]);
var enabledFlags = Object.keys(decisions);
// -------------------------------
// Track
// ------------------------------
// old method
var eventTags = { purchase_count: 2 };
optimizelyClient.track('my_purchase_event_key', 'user_123', attributes, eventTags);
// new method
user.trackEvent('my_purchase_event_key', eventTags);
Activate method and A/B Tests
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.
Updated about 1 month ago