use of com.optimizely.ab.android.sdk.OptimizelyClient in project android-sdk by optimizely.
the class APISamplesInJava method samplesForDoc_DecideForKeys.
public static void samplesForDoc_DecideForKeys(Context context) {
OptimizelyManager optimizelyManager = OptimizelyManager.builder().withSDKKey("FCnSegiEkRry9rhVMroit4").build(context);
OptimizelyClient optimizelyClient = optimizelyManager.initialize(context, R.raw.datafile);
Map<String, Object> attributes = new HashMap<>();
attributes.put("logged_in", true);
OptimizelyUserContext user = optimizelyClient.createUserContext("user123", attributes);
// -- sample starts here
// make decisions for specific flags
List<String> keys = Arrays.asList("flag-1", "flag-2");
Map<String, OptimizelyDecision> decisions = user.decideForKeys(keys);
OptimizelyDecision decision1 = decisions.get("flag-1");
OptimizelyDecision decision2 = decisions.get("flag-2");
}
use of com.optimizely.ab.android.sdk.OptimizelyClient in project android-sdk by optimizely.
the class APISamplesInJava method samplesForDoc_TrackEvent.
public static void samplesForDoc_TrackEvent(Context context) {
OptimizelyManager optimizelyManager = OptimizelyManager.builder().withSDKKey("FCnSegiEkRry9rhVMroit4").build(context);
OptimizelyClient optimizelyClient = optimizelyManager.initialize(context, R.raw.datafile);
// -- sample starts here
Map<String, Object> attributes = new HashMap<>();
attributes.put("logged_in", true);
OptimizelyUserContext user = optimizelyClient.createUserContext("user123", attributes);
Map<String, Object> tags = new HashMap<>();
tags.put("category", "shoes");
tags.put("purchase_count", 2);
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_DecideAll.
public static void samplesForDoc_DecideAll(Context context) {
OptimizelyManager optimizelyManager = OptimizelyManager.builder().withSDKKey("FCnSegiEkRry9rhVMroit4").build(context);
OptimizelyClient optimizelyClient = optimizelyManager.initialize(context, R.raw.datafile);
Map<String, Object> attributes = new HashMap<>();
attributes.put("logged_in", true);
OptimizelyUserContext user = optimizelyClient.createUserContext("user123", attributes);
// -- sample starts here
// make decisions for all active (unarchived) flags in the project for a user
List<OptimizelyDecideOption> options = Arrays.asList(OptimizelyDecideOption.ENABLED_FLAGS_ONLY);
Map<String, OptimizelyDecision> decisions = user.decideAll(options);
Set<String> allKeys = decisions.keySet();
OptimizelyDecision decisionForFlag1 = decisions.get("flag_1");
}
use of com.optimizely.ab.android.sdk.OptimizelyClient in project android-sdk by optimizely.
the class APISamplesInJava method samplesForInitialization.
public static void samplesForInitialization(Context context) {
OptimizelyManager optimizelyManager;
OptimizelyClient optimizelyClient;
OptimizelyUserContext user;
OptimizelyDecision decision;
// Here are more sample codes for synchronous and asynchronous SDK initializations with multiple options
// [Synchronous]
// [S1] Synchronous initialization
// 1. SDK is initialized instantly with a cached (or bundled) datafile
// 2. A new datafile can be downloaded in background and cached after the SDK is initialized.
// The cached datafile will be used only when the SDK re-starts in the next session.
optimizelyManager = OptimizelyManager.builder().withSDKKey("<Your_SDK_Key>").build(context);
optimizelyClient = optimizelyManager.initialize(context, R.raw.datafile);
user = optimizelyClient.createUserContext("<User_ID>");
decision = user.decide("<Flag_Key>");
// [S2] Synchronous initialization
// 1. SDK is initialized instantly with a cached (or bundled) datafile
// 2. A new datafile can be downloaded in background and cached after the SDK is initialized.
// The cached datafile is used immediately to update the SDK project config.
optimizelyManager = OptimizelyManager.builder().withSDKKey("<Your_SDK_Key>").build(context);
optimizelyClient = optimizelyManager.initialize(context, R.raw.datafile, true, true);
user = optimizelyClient.createUserContext("<User_ID>");
decision = user.decide("<Flag_Key>");
// [S3] Synchronous initialization
// 1. SDK is initialized instantly with a cached (or bundled) datafile
// 2. A new datafile can be downloaded in background and cached after the SDK is initialized.
// The cached datafile is used immediately to update the SDK project config.
// 3. Polling datafile periodically.
// The cached datafile is used immediately to update the SDK project config.
optimizelyManager = OptimizelyManager.builder().withSDKKey("<Your_SDK_Key>").withDatafileDownloadInterval(15, TimeUnit.MINUTES).build(context);
optimizelyClient = optimizelyManager.initialize(context, R.raw.datafile, true, true);
user = optimizelyClient.createUserContext("<User_ID>");
decision = user.decide("<Flag_Key>");
// [Asynchronous]
// [A1] Asynchronous initialization
// 1. A datafile is downloaded from the server and the SDK is initialized with the datafile
optimizelyManager = OptimizelyManager.builder().withSDKKey("<Your_SDK_Key>").build(context);
optimizelyManager.initialize(context, null, (OptimizelyClient client) -> {
OptimizelyUserContext userContext = client.createUserContext("<User_ID>");
OptimizelyDecision optDecision = userContext.decide("<Flag_Key>");
});
// [A2] Asynchronous initialization
// 1. A datafile is downloaded from the server and the SDK is initialized with the datafile
// 2. Polling datafile periodically.
// The cached datafile is used immediately to update the SDK project config.
optimizelyManager = OptimizelyManager.builder().withSDKKey("<Your_SDK_Key>").withDatafileDownloadInterval(15, TimeUnit.MINUTES).build(context);
optimizelyManager.initialize(context, null, (OptimizelyClient client) -> {
OptimizelyUserContext userContext = client.createUserContext("<User_ID>");
OptimizelyDecision optDecision = userContext.decide("<Flag_Key>");
});
}
use of com.optimizely.ab.android.sdk.OptimizelyClient in project android-sdk by optimizely.
the class APISamplesInJava method samplesForDoc_OptimizelyJSON.
public static void samplesForDoc_OptimizelyJSON(Context context) {
OptimizelyManager optimizelyManager = OptimizelyManager.builder().withSDKKey("FCnSegiEkRry9rhVMroit4").build(context);
OptimizelyClient optimizelyClient = optimizelyManager.initialize(context, R.raw.datafile);
OptimizelyUserContext user = optimizelyClient.createUserContext("user123");
OptimizelyDecision decision = user.decide("product_sort");
OptimizelyJSON optlyJSON = decision.getVariables();
// declare a schema object into which you want to unmarshal OptimizelyJson content:
class SSub {
String field;
}
class SObj {
int field1;
double field2;
String field3;
SSub field4;
}
try {
// parse all json key/value pairs into your schema, sObj
SObj robj = optlyJSON.getValue(null, SObj.class);
// or, parse the specified key/value pair with an integer value
Integer rint = optlyJSON.getValue("field1", Integer.class);
// or, parse the specified key/value pair with a string value
String rstr = optlyJSON.getValue("field4.field", String.class);
} catch (JsonParseException e) {
e.printStackTrace();
}
}
Aggregations