Search in sources :

Example 6 with OptimizelyClient

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);
}
Also used : OptimizelyDecision(com.optimizely.ab.optimizelydecision.OptimizelyDecision) OptimizelyDecideOption(com.optimizely.ab.optimizelydecision.OptimizelyDecideOption) OptimizelyClient(com.optimizely.ab.android.sdk.OptimizelyClient) OptimizelyUserContext(com.optimizely.ab.OptimizelyUserContext) OptimizelyManager(com.optimizely.ab.android.sdk.OptimizelyManager)

Example 7 with OptimizelyClient

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);
}
Also used : HashMap(java.util.HashMap) OptimizelyClient(com.optimizely.ab.android.sdk.OptimizelyClient) OptimizelyUserContext(com.optimizely.ab.OptimizelyUserContext) OptimizelyManager(com.optimizely.ab.android.sdk.OptimizelyManager)

Example 8 with OptimizelyClient

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();
}
Also used : HashMap(java.util.HashMap) OptimizelyDecision(com.optimizely.ab.optimizelydecision.OptimizelyDecision) JsonParseException(com.optimizely.ab.config.parser.JsonParseException) OptimizelyClient(com.optimizely.ab.android.sdk.OptimizelyClient) OptimizelyManager(com.optimizely.ab.android.sdk.OptimizelyManager) OptimizelyJSON(com.optimizely.ab.optimizelyjson.OptimizelyJSON) OptimizelyUserContext(com.optimizely.ab.OptimizelyUserContext)

Example 9 with OptimizelyClient

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);
}
Also used : HashMap(java.util.HashMap) OptimizelyDecision(com.optimizely.ab.optimizelydecision.OptimizelyDecision) OptimizelyDecideOption(com.optimizely.ab.optimizelydecision.OptimizelyDecideOption) OptimizelyClient(com.optimizely.ab.android.sdk.OptimizelyClient) OptimizelyManager(com.optimizely.ab.android.sdk.OptimizelyManager) OptimizelyJSON(com.optimizely.ab.optimizelyjson.OptimizelyJSON) OptimizelyVariation(com.optimizely.ab.optimizelyconfig.OptimizelyVariation) Variation(com.optimizely.ab.config.Variation) OptimizelyUserContext(com.optimizely.ab.OptimizelyUserContext)

Example 10 with OptimizelyClient

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();
}
Also used : OptimizelyDecision(com.optimizely.ab.optimizelydecision.OptimizelyDecision) OptimizelyDecisionContext(com.optimizely.ab.OptimizelyDecisionContext) OptimizelyClient(com.optimizely.ab.android.sdk.OptimizelyClient) OptimizelyUserContext(com.optimizely.ab.OptimizelyUserContext) OptimizelyManager(com.optimizely.ab.android.sdk.OptimizelyManager) OptimizelyForcedDecision(com.optimizely.ab.OptimizelyForcedDecision)

Aggregations

OptimizelyClient (com.optimizely.ab.android.sdk.OptimizelyClient)19 OptimizelyManager (com.optimizely.ab.android.sdk.OptimizelyManager)19 OptimizelyUserContext (com.optimizely.ab.OptimizelyUserContext)14 OptimizelyDecision (com.optimizely.ab.optimizelydecision.OptimizelyDecision)12 HashMap (java.util.HashMap)8 OptimizelyJSON (com.optimizely.ab.optimizelyjson.OptimizelyJSON)4 JsonParseException (com.optimizely.ab.config.parser.JsonParseException)3 OptimizelyDecideOption (com.optimizely.ab.optimizelydecision.OptimizelyDecideOption)3 OptimizelyDecisionContext (com.optimizely.ab.OptimizelyDecisionContext)1 OptimizelyForcedDecision (com.optimizely.ab.OptimizelyForcedDecision)1 DefaultEventHandler (com.optimizely.ab.android.event_handler.DefaultEventHandler)1 Variation (com.optimizely.ab.config.Variation)1 BatchEventProcessor (com.optimizely.ab.event.BatchEventProcessor)1 EventHandler (com.optimizely.ab.event.EventHandler)1 OptimizelyConfig (com.optimizely.ab.optimizelyconfig.OptimizelyConfig)1 OptimizelyVariation (com.optimizely.ab.optimizelyconfig.OptimizelyVariation)1