The availability of features may depend on your plan type. Contact your Customer Success Manager if you have any questions.

Dev guideRecipesAPI Reference
Dev guideAPI ReferenceUser GuideLegal TermsGitHubDev CommunityOptimizely AcademySubmit a ticketLog In
Dev guide

Example usage of the Android SDK

Code example of how to use the Optimizely Feature Experimentation Android SDK to evaluate feature flags, activate A/B tests, or feature tests.

Once you have installed an SDK, import the Optimizely Feature Experimentation library into your code, get your Optimizely Feature Experimentation project's datafile, and instantiate a client. Then, you can use the client to evaluate flag rules, including A/B tests and flag deliveries.

This example demonstrates the basic usage of each of these concepts:

  1. Evaluate a flag with the key product_sort using the Decide method. As a side effect, the Decide function also sends a decision event to Optimizely to record that the current user has been exposed to the experiment.

  2. Conditionally execute your feature code. You have a couple of options:

  • Fetch the flag enabled state, then check a configuration variable on the flag called sort_method. The SDK evaluates your flag rules and determines what flag variation the user is in and, therefore, which sort method variable they should see.
  • Fetch on the flag variation, then run 'control' or 'treatment' code.
  1. Use event tracking to track an event called purchased. This conversion event measures the impact of an experiment. Using the Track Event method, the purchase is automatically attributed back to the running A/B test for which we made a decision, and the SDK sends a network request to Optimizely through the customizable event dispatcher so Optimizely can count it in your results page.
// Build a manager
val optimizelyManager = OptimizelyManager.builder()
        .withSDKKey("<Your_SDK_Key>")
        .build(context)
// Instantiate a client synchronously with a bundled datafile
// copy datafile JSON from URL accessible in app>settings
val datafile = "REPLACE_WITH_YOUR_DATAFILE"
val optimizelyClient = optimizelyManager.initialize(context, datafile)

// Create a user-context
val attributes: MutableMap<String, Any> = HashMap()
attributes["logged_in"] = true
val user = optimizelyClient.createUserContext("user123", attributes)!!

// Call the decide method
val decision = user.decide("product_sort")

// did the decision fail with a critical error?
val variationKey = decision.variationKey
if (variationKey == null) {
   val reasons = decision.reasons
   Log.d("Optimizely", "decision error: $reasons")
}

// execute code based on flag enabled state
val enabled = decision.enabled
val variables = decision.variables
if (enabled) {
   var vs: String? = null
   try {
      vs = variables.getValue("sort_method", String::class.java)
   } catch (e: JsonParseException) {
      e.printStackTrace()
   }
}

// or execute code based on flag variation:
if (variationKey == "control") {
   // Execute code for control variation
} else if (variationKey == "treatment") {
   // Execute code for treatment variation
}

// Track an event
user.trackEvent("purchased")
// Build a manager
OptimizelyManager optimizelyManager = OptimizelyManager.builder()
       .withSDKKey("<Your_SDK_Key>")
       .build(context);
// Instantiate a client synchronously with a bundled datafile
// copy datafile JSON from URL accessible in app>settings
String datafile = "REPLACE_WITH_YOUR_DATAFILE";
OptimizelyClient optimizelyClient = optimizelyManager.initialize(context, datafile);

// Create a user-context
Map<String, Object> attributes = new HashMap<>();
attributes.put("logged_in", true);
OptimizelyUserContext user = optimizelyClient.createUserContext("user123", attributes);

// Call the decide method
OptimizelyDecision decision = user.decide("product_sort");

// did the decision fail with a critical error?
String variationKey = decision.getVariationKey();
if (variationKey == null) {
   List<String> reasons = decision.getReasons();
   Log.d("Optimizely", "decision error: " + reasons);
}

// execute code based on flag enabled state
boolean enabled = decision.getEnabled();
OptimizelyJSON variables = decision.getVariables();
if (enabled) {
   String vs = null;
   try {
      vs = variables.getValue("sort_method", String.class);
   } catch (JsonParseException e) {
      e.printStackTrace();
   }
}

// or execute code based on flag variation:
if (variationKey.equals("control")) {
   // Execute code for control variation
} else if (variationKey.equals("treatment")) {
   // Execute code for treatment variation
}

// Track an event
user.trackEvent("purchased");