use of com.optimizely.ab.android.sdk.OptimizelyManager in project android-sdk by optimizely.
the class APISamplesInJava method samplesForDoc_CustomUserProfileService.
public static void samplesForDoc_CustomUserProfileService(Context context) {
class CustomUserProfileService implements UserProfileService {
public Map<String, Object> lookup(String userId) throws Exception {
return null;
}
public void save(Map<String, Object> userProfile) throws Exception {
}
}
CustomUserProfileService customUserProfileService = new CustomUserProfileService();
OptimizelyManager optimizelyManager = OptimizelyManager.builder().withSDKKey("<Your_SDK_Key>").withUserProfileService(customUserProfileService).build(context);
}
use of com.optimizely.ab.android.sdk.OptimizelyManager 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();
}
use of com.optimizely.ab.android.sdk.OptimizelyManager 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.OptimizelyManager 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.OptimizelyManager 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);
}
Aggregations