use of com.optimizely.ab.android.sdk.OptimizelyClient in project android-sdk by optimizely.
the class APISamplesInJava method samplesForDoc_DecideOptions.
public static void samplesForDoc_DecideOptions(Context context) {
// set global default decide options when initializing the client
List<OptimizelyDecideOption> options;
options = Arrays.asList(OptimizelyDecideOption.DISABLE_DECISION_EVENT);
OptimizelyManager optimizelyManager = OptimizelyManager.builder().withSDKKey("FCnSegiEkRry9rhVMroit4").withDefaultDecideOptions(options).build(context);
// set additional options in a decide call
OptimizelyClient optimizelyClient = optimizelyManager.initialize(context, R.raw.datafile);
OptimizelyUserContext user = optimizelyClient.createUserContext("user123");
options = Arrays.asList(OptimizelyDecideOption.DISABLE_DECISION_EVENT, OptimizelyDecideOption.DISABLE_DECISION_EVENT);
Map<String, OptimizelyDecision> decisions = user.decideAll(options);
}
use of com.optimizely.ab.android.sdk.OptimizelyClient in project android-sdk by optimizely.
the class APISamplesInJava method samplesForDoc_CreateUserContext.
public static void samplesForDoc_CreateUserContext(Context context) {
OptimizelyManager optimizelyManager = OptimizelyManager.builder().withSDKKey("FCnSegiEkRry9rhVMroit4").build(context);
OptimizelyClient optimizelyClient = optimizelyManager.initialize(context, R.raw.datafile);
// -- sample starts here
// option 1: create a user, then set attributes
OptimizelyUserContext user;
user = optimizelyClient.createUserContext("user123");
user.setAttribute("is_logged_in", false);
user.setAttribute("app_version", "1.3.2");
// option 2: pass attributes when creating the user
Map<String, Object> attributes = new HashMap<>();
attributes.put("is_logged_in", false);
attributes.put("app_version", "1.3.2");
user = optimizelyClient.createUserContext("user123", attributes);
}
use of com.optimizely.ab.android.sdk.OptimizelyClient in project android-sdk by optimizely.
the class APISamplesInJava method samplesForDoc_Decide.
public static void samplesForDoc_Decide(Context context) {
OptimizelyManager optimizelyManager = OptimizelyManager.builder().withSDKKey("FCnSegiEkRry9rhVMroit4").build(context);
OptimizelyClient optimizelyClient = optimizelyManager.initialize(context, R.raw.datafile);
// -- sample starts here
// create the user and decide which flag rule & variation they bucket into
Map<String, Object> attributes = new HashMap<>();
attributes.put("logged_in", true);
OptimizelyUserContext user = optimizelyClient.createUserContext("user123", attributes);
OptimizelyDecision decision = user.decide("product_sort");
// variation. if null, decision fail with a critical error
String variationKey = decision.getVariationKey();
// flag enabled state:
boolean enabled = decision.getEnabled();
// all variable values
OptimizelyJSON variables = decision.getVariables();
// String variable value
String varStr = null;
try {
varStr = variables.getValue("sort_method", String.class);
} catch (JsonParseException e) {
e.printStackTrace();
}
// Boolean variable value
Boolean varBool = (Boolean) variables.toMap().get("k_boolean");
// flag key for which decision was made
String flagKey = decision.getFlagKey();
// user for which the decision was made
OptimizelyUserContext userContext = decision.getUserContext();
// reasons for the decision
List<String> reasons = decision.getReasons();
}
use of com.optimizely.ab.android.sdk.OptimizelyClient in project android-sdk by optimizely.
the class APISamplesInJava method samplesForDoc_OlderVersions.
public static void samplesForDoc_OlderVersions(Context context) {
OptimizelyManager optimizelyManager = OptimizelyManager.builder().withSDKKey("FCnSegiEkRry9rhVMroit4").build(context);
OptimizelyClient optimizelyClient = optimizelyManager.initialize(context, R.raw.datafile);
// -- sample starts here
// 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);
}
use of com.optimizely.ab.android.sdk.OptimizelyClient in project android-sdk by optimizely.
the class APISamplesInJava method samplesForDoc_ForcedDecision.
public static void samplesForDoc_ForcedDecision(Context context) {
OptimizelyManager optimizelyManager = OptimizelyManager.builder().withSDKKey("FCnSegiEkRry9rhVMroit4").build(context);
OptimizelyClient optimizelyClient = optimizelyManager.initialize(context, R.raw.datafile);
// -- sample starts here
// Create the OptimizelyUserContext, passing in the UserId and Attributes
OptimizelyUserContext user = optimizelyClient.createUserContext("user-id");
OptimizelyDecisionContext flagContext = new OptimizelyDecisionContext("flag-1", null);
OptimizelyDecisionContext flagAndABTestContext = new OptimizelyDecisionContext("flag-1", "exp-1");
OptimizelyDecisionContext flagAndDeliveryRuleContext = new OptimizelyDecisionContext("flag-1", "delivery-1");
OptimizelyForcedDecision variationAForcedDecision = new OptimizelyForcedDecision("variation-a");
OptimizelyForcedDecision variationBForcedDecision = new OptimizelyForcedDecision("variation-b");
OptimizelyForcedDecision variationOnForcedDecision = new OptimizelyForcedDecision("on");
// set a forced decision for a flag
Boolean success = user.setForcedDecision(flagContext, variationAForcedDecision);
OptimizelyDecision decision = user.decide("flag-1");
// set a forced decision for an ab-test rule
success = user.setForcedDecision(flagAndABTestContext, variationBForcedDecision);
decision = user.decide("flag-1");
// set a forced variation for a delivery rule
success = user.setForcedDecision(flagAndDeliveryRuleContext, variationOnForcedDecision);
decision = user.decide("flag-1");
// get forced variations
OptimizelyForcedDecision forcedDecision = user.getForcedDecision(flagContext);
Log.d("Optimizely", "[ForcedDecision] variationKey = " + forcedDecision.getVariationKey());
// remove forced variations
success = user.removeForcedDecision(flagAndABTestContext);
success = user.removeAllForcedDecisions();
}
Aggregations