use of com.optimizely.ab.android.sdk.OptimizelyClient in project android-sdk by optimizely.
the class APISamplesInJava method samplesForDoc_ExampleUsage.
public static void samplesForDoc_ExampleUsage(Context context) {
// Build a manager
OptimizelyManager optimizelyManager = OptimizelyManager.builder().withSDKKey("<Your_SDK_Key>").build(context);
// Instantiate a client synchronously with a bundled datafile
// copy datafile JSON from URL accessible in app>settings
String datafile = "REPLACE_WITH_YOUR_DATAFILE";
OptimizelyClient optimizelyClient = optimizelyManager.initialize(context, datafile);
// Create a user-context
Map<String, Object> attributes = new HashMap<>();
attributes.put("logged_in", true);
OptimizelyUserContext user = optimizelyClient.createUserContext("user123", attributes);
// Call the decide method
OptimizelyDecision decision = user.decide("product_sort");
// did the decision fail with a critical error?
String variationKey = decision.getVariationKey();
if (variationKey == null) {
List<String> reasons = decision.getReasons();
Log.d("Optimizely", "decision error: " + reasons);
return;
}
// execute code based on flag enabled state
boolean enabled = decision.getEnabled();
OptimizelyJSON variables = decision.getVariables();
if (enabled) {
String varStr = null;
try {
varStr = variables.getValue("sort_method", String.class);
} catch (JsonParseException e) {
e.printStackTrace();
}
}
// or execute code based on flag variation:
if (variationKey.equals("control")) {
// Execute code for control variation
} else if (variationKey.equals("treatment")) {
// Execute code for treatment variation
}
// Track an event
user.trackEvent("purchased");
}
use of com.optimizely.ab.android.sdk.OptimizelyClient in project android-sdk by optimizely.
the class APISamplesInJava method samplesForDoc_EventBatchingAdvanced.
public static void samplesForDoc_EventBatchingAdvanced(Context context) {
// -- sample starts here
EventHandler eventHandler = DefaultEventHandler.getInstance(context);
// Here we are using the builder options to set batch size
// to 5 events and flush interval to a minute.
BatchEventProcessor batchProcessor = BatchEventProcessor.builder().withBatchSize(5).withEventHandler(eventHandler).withFlushInterval(TimeUnit.MINUTES.toMillis(1L)).build();
OptimizelyManager optimizelyManager = OptimizelyManager.builder().withSDKKey("<Your_SDK_Key>").withEventHandler(eventHandler).withDatafileDownloadInterval(15, TimeUnit.MINUTES).withEventProcessor(batchProcessor).build(context);
OptimizelyClient optimizely = optimizelyManager.initialize(context, R.raw.datafile);
// log event
// -- sample starts here
optimizely.addLogEventNotificationHandler(logEvent -> {
Log.d("Optimizely", "event dispatched: " + logEvent);
});
}
use of com.optimizely.ab.android.sdk.OptimizelyClient in project android-sdk by optimizely.
the class APISamplesInJava method samplesForDoc_BundledDatafile.
public static void samplesForDoc_BundledDatafile(Context context) {
OptimizelyManager optimizelyManager = OptimizelyManager.builder().withSDKKey("FCnSegiEkRry9rhVMroit4").build(context);
// -- sample starts here
// Initialize Optimizely asynchronously with a datafile.
// If it is not able to download a new datafile, it will
// initialize an OptimizelyClient with the one provided.
optimizelyManager.initialize(context, R.raw.datafile, (OptimizelyClient optimizelyClient) -> {
OptimizelyUserContext user = optimizelyClient.createUserContext("<User_ID>");
OptimizelyDecision decision = user.decide("<Flag_Key>");
});
// Initialize Optimizely synchronously
// This will immediately instantiate and return an
// OptimizelyClient with the datafile that was passed in.
// It'll also download a new datafile from the CDN and
// persist it to local storage.
// The newly downloaded datafile will be used the next
// time the SDK is initialized.
optimizelyManager.initialize(context, R.raw.datafile);
}
use of com.optimizely.ab.android.sdk.OptimizelyClient in project android-sdk by optimizely.
the class APISamplesInJava method samplesForDoc_AudienceAttributes.
public static void samplesForDoc_AudienceAttributes(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("device", "iPhone");
attributes.put("lifetime", 24738388);
attributes.put("is_logged_in", true);
attributes.put("application_version", "4.3.0-beta");
OptimizelyUserContext user = optimizelyClient.createUserContext("user123", attributes);
OptimizelyDecision decision = user.decide("<Flag_Key>");
}
use of com.optimizely.ab.android.sdk.OptimizelyClient in project android-sdk by optimizely.
the class APISamplesInJava method samplesForDoc_GetClient.
public static void samplesForDoc_GetClient(Context context) {
OptimizelyManager optimizelyManager = OptimizelyManager.builder().withSDKKey("FCnSegiEkRry9rhVMroit4").build(context);
// -- sample starts here
OptimizelyClient optimizelyClient = optimizelyManager.getOptimizely();
}
Aggregations