Search in sources :

Example 6 with OptimizelyManager

use of com.optimizely.ab.android.sdk.OptimizelyManager in project android-sdk by optimizely.

the class APISamplesInJava method samplesForOptimizelyConfig.

public static void samplesForOptimizelyConfig(Context context) {
    OptimizelyManager optimizelyManager = OptimizelyManager.builder().withSDKKey("FCnSegiEkRry9rhVMroit4").build(context);
    optimizelyManager.initialize(context, R.raw.datafile, optimizelyClient -> {
        // -- sample starts here
        OptimizelyConfig config = optimizelyClient.getOptimizelyConfig();
        Log.d("Optimizely", "[OptimizelyConfig] revision = " + config.getRevision());
        Log.d("Optimizely", "[OptimizelyConfig] sdkKey = " + config.getSdkKey());
        Log.d("Optimizely", "[OptimizelyConfig] environmentKey = " + config.getEnvironmentKey());
        Log.d("Optimizely", "[OptimizelyConfig] attributes:");
        for (OptimizelyAttribute attribute : config.getAttributes()) {
            Log.d("Optimizely", "[OptimizelyAttribute]  -- (id, key) = " + attribute.getId() + ", " + attribute.getKey());
        }
        Log.d("Optimizely", "[OptimizelyConfig] audiences:");
        for (OptimizelyAudience audience : config.getAudiences()) {
            Log.d("Optimizely", "[OptimizelyAudience]  -- (id, name, conditions) = " + audience.getId() + ", " + audience.getName() + ", " + audience.getConditions());
        }
        Log.d("Optimizely", "[OptimizelyConfig] events:");
        for (OptimizelyEvent event : config.getEvents()) {
            Log.d("Optimizely", "[OptimizelyEvent]  -- (id, key, experimentIds) = " + event.getId() + ", " + event.getKey() + ", " + Arrays.toString(event.getExperimentIds().toArray()));
        }
        // all features
        for (String flagKey : config.getFeaturesMap().keySet()) {
            OptimizelyFeature flag = config.getFeaturesMap().get(flagKey);
            for (OptimizelyExperiment experiment : flag.getExperimentRules()) {
                Log.d("Optimizely", "[OptimizelyExperiment]  -- Experiment Rule Key: " + experiment.getKey());
                Log.d("Optimizely", "[OptimizelyExperiment]  -- Experiment Audiences: " + experiment.getAudiences());
                Map<String, OptimizelyVariation> variationsMap = experiment.getVariationsMap();
                for (String variationKey : variationsMap.keySet()) {
                    OptimizelyVariation variation = variationsMap.get(variationKey);
                    Log.d("Optimizely", "[OptimizelyVariation]    -- variation = { key: " + variationKey + ", id: " + variation.getId() + ", featureEnabled: " + variation.getFeatureEnabled() + " }");
                    // use variation data here...
                    Map<String, OptimizelyVariable> optimizelyVariableMap = variation.getVariablesMap();
                    for (String variableKey : optimizelyVariableMap.keySet()) {
                        OptimizelyVariable variable = optimizelyVariableMap.get(variableKey);
                        Log.d("Optimizely", "[OptimizelyVariable]      -- variable = key: " + variableKey + ", value: " + variable.getValue());
                    // use variable data here...
                    }
                }
            }
            for (OptimizelyExperiment delivery : flag.getDeliveryRules()) {
                Log.d("Optimizely", "[OptimizelyExperiment]  -- Delivery Rule Key: " + delivery.getKey());
                Log.d("Optimizely", "[OptimizelyExperiment]  -- Delivery Audiences: " + delivery.getAudiences());
            }
        // use experiments and other feature flag data here...
        }
        // listen to OPTIMIZELY_CONFIG_UPDATE to get updated data
        optimizelyClient.getNotificationCenter().addNotificationHandler(UpdateConfigNotification.class, handler -> {
            OptimizelyConfig newConfig = optimizelyClient.getOptimizelyConfig();
        });
    });
}
Also used : OptimizelyExperiment(com.optimizely.ab.optimizelyconfig.OptimizelyExperiment) OptimizelyVariable(com.optimizely.ab.optimizelyconfig.OptimizelyVariable) OptimizelyConfig(com.optimizely.ab.optimizelyconfig.OptimizelyConfig) OptimizelyAttribute(com.optimizely.ab.optimizelyconfig.OptimizelyAttribute) OptimizelyFeature(com.optimizely.ab.optimizelyconfig.OptimizelyFeature) OptimizelyVariation(com.optimizely.ab.optimizelyconfig.OptimizelyVariation) OptimizelyEvent(com.optimizely.ab.optimizelyconfig.OptimizelyEvent) OptimizelyAudience(com.optimizely.ab.optimizelyconfig.OptimizelyAudience) OptimizelyManager(com.optimizely.ab.android.sdk.OptimizelyManager)

Example 7 with OptimizelyManager

use of com.optimizely.ab.android.sdk.OptimizelyManager 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 8 with OptimizelyManager

use of com.optimizely.ab.android.sdk.OptimizelyManager 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 9 with OptimizelyManager

use of com.optimizely.ab.android.sdk.OptimizelyManager 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 10 with OptimizelyManager

use of com.optimizely.ab.android.sdk.OptimizelyManager 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)

Aggregations

OptimizelyManager (com.optimizely.ab.android.sdk.OptimizelyManager)25 OptimizelyClient (com.optimizely.ab.android.sdk.OptimizelyClient)19 OptimizelyUserContext (com.optimizely.ab.OptimizelyUserContext)15 OptimizelyDecision (com.optimizely.ab.optimizelydecision.OptimizelyDecision)13 HashMap (java.util.HashMap)10 OptimizelyJSON (com.optimizely.ab.optimizelyjson.OptimizelyJSON)5 JsonParseException (com.optimizely.ab.config.parser.JsonParseException)4 OptimizelyDecideOption (com.optimizely.ab.optimizelydecision.OptimizelyDecideOption)4 DefaultEventHandler (com.optimizely.ab.android.event_handler.DefaultEventHandler)2 EventHandler (com.optimizely.ab.event.EventHandler)2 OptimizelyConfig (com.optimizely.ab.optimizelyconfig.OptimizelyConfig)2 OptimizelyVariation (com.optimizely.ab.optimizelyconfig.OptimizelyVariation)2 Intent (android.content.Intent)1 IntentFilter (android.content.IntentFilter)1 NonNull (androidx.annotation.NonNull)1 OptimizelyExperimentsClient (com.kickstarter.libs.OptimizelyExperimentsClient)1 OptimizelyEnvironment (com.kickstarter.libs.models.OptimizelyEnvironment)1 OptimizelyDecisionContext (com.optimizely.ab.OptimizelyDecisionContext)1 OptimizelyForcedDecision (com.optimizely.ab.OptimizelyForcedDecision)1 EventRescheduler (com.optimizely.ab.android.event_handler.EventRescheduler)1