Dev guideAPI Reference
Dev guideAPI ReferenceUser GuideGitHubNuGetDev CommunitySubmit a ticketLog In
GitHubNuGetDev CommunitySubmit a ticket

Update from older versions of the Java SDK

Describes how to update from older versions of the Optimizely Full Stack (Legacy) Java 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
// ------------------------------
Map<String, Object> attributes = new HashMap<>();
attributes.put("is_logged_in", true);
OptimizelyUserContext user = optimizely.createUserContext("user_123", attributes);


// -------------------------------
// Is Feature Enabled
// ------------------------------
// old, Full Stack  method

Boolean enabled = optimizely.isFeatureEnabled("flag_1", "user_123", attributes);

// new, Feature Experimentation method
OptimizelyDecision decision = user.decide("flag_1");
boolean enabled = decision.getEnabled();


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

// new, Feature Experimentation method
String variationKey = decision.getVariationKey();


// -------------------------------
// Get All Feature Variables
// ------------------------------
// old, Full Stack method
OptimizelyJSON json = optimizely.getAllFeatureVariables("flag_1","user_123", attributes);
 
// new, Feature Experimentation method
OptimizelyJSON json = decision.getVariables();


// -------------------------------
// Get Enabled Features
// ------------------------------
// old, Full Stack method
List<String> enabledFlags = optimizely.getEnabledFeatures("user_123", attributes);

// new, Feature Experimentation method
List<OptimizelyDecideOption> options = Arrays.asList(OptimizelyDecideOption.ENABLED_FLAGS_ONLY);
Map<String, OptimizelyDecision> decisions = user.decideAll(options);
Set<String> enabledFlags = decisions.keySet();


// -------------------------------
// Track
// ------------------------------
// old, Full Stack method
Map<String, Object> tags = new HashMap<>();
attributes.put("purchase_count", 2);
optimizely.track("my_purchase_event_key", "user_123", attributes, tags);

// new, Feature Experimentation method
user.trackEvent("my_purchase_event_key", tags);