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();
});
});
}
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);
}
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);
}
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();
}
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);
}
Aggregations