Dev guideAPI Reference
Dev guideAPI ReferenceUser GuideGitHubNuGetDev CommunityDoc feedbackLog In

Update Android API usage from older versions

How to update from older versions of the Optimizely Full Stack Android SDK to Optimizely Feature Experimentation.

Code examples

This section provides code examples for how we recommend leveraging our new Decide 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
val attributes: MutableMap<String, Any?> = HashMap()
attributes["is_logged_in"] = true
val user = optimizelyClient.createUserContext("user123", attributes)

// Is Feature Enabled

// old method
var enabled = optimizelyClient.isFeatureEnabled("flag_1", "user123", attributes)
// new method
val decision = user!!.decide("flag_1")
enabled = decision.enabled

// Activate & Get Variation

// old method
val variation = optimizelyClient.activate("experiment_1", "user123", attributes)
// new method
val variationKey = decision.variationKey

// Get All Feature Variables

// old method
var json = optimizelyClient.getAllFeatureVariables("flag_1", "user123", attributes)
// new method
json = decision.variables

// Get Enabled Features

// old method
val enabledFlags = optimizelyClient.getEnabledFeatures("user123", attributes)
// new method
val options = Arrays.asList(OptimizelyDecideOption.ENABLED_FLAGS_ONLY)
val decisions = user.decideAll(options)
val enabledFlagsSet: Set<String> = decisions.keys

// Track

// old method
val tags: Map<String, Any?> = HashMap()
attributes["purchase_count"] = 2
optimizelyClient.track("my_purchase_event_key", "user123", attributes, tags)
// new method
user.trackEvent("my_purchase_event_key", tags)
// Prereq for new methods: create a user
Map<String, Object> attributes = new HashMap<>();
attributes.put("is_logged_in", true);
OptimizelyUserContext user = optimizelyClient.createUserContext("user123", attributes);
        
// Is Feature Enabled
        
// old method
boolean enabled = optimizelyClient.isFeatureEnabled("flag_1", "user123", attributes);
// new method
OptimizelyDecision decision = user.decide("flag_1");
enabled = decision.getEnabled();
        
// Activate & Get Variation
        
// old method
Variation variation = optimizelyClient.activate("experiment_1", "user123", attributes);
// new method
String variationKey = decision.getVariationKey();
        
// Get All Feature Variables

// old method
OptimizelyJSON json = optimizelyClient.getAllFeatureVariables("flag_1", "user123", attributes);
// new method
json = decision.getVariables();
        
// Get Enabled Features

// old method
List<String> enabledFlags = optimizelyClient.getEnabledFeatures("user123", attributes);
// new method
List<OptimizelyDecideOption> options = Arrays.asList(OptimizelyDecideOption.ENABLED_FLAGS_ONLY);
Map<String, OptimizelyDecision> decisions = user.decideAll(options);
Set<String> enabledFlagsSet = decisions.keySet();
        
// Track

// old method
Map<String, Object> tags = new HashMap<>();
attributes.put("purchase_count", 2);
optimizelyClient.track("my_purchase_event_key", "user123", attributes, tags);
// new method
user.trackEvent("my_purchase_event_key", tags);

Activate method and A/B Tests

Like the existing IsFeatureEnabled 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.s