use of com.optimizely.ab.android.sdk.OptimizelyManager 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.OptimizelyManager 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();
}
use of com.optimizely.ab.android.sdk.OptimizelyManager in project android-sdk by optimizely.
the class APISamplesInJava method samplesForDoc_EventDispatcher.
public static void samplesForDoc_EventDispatcher(Context context) {
// -- sample starts here
// Using an anonymous class here to implement the EventHandler interface.
// Feel free to create an explicit class that implements the interface instead.
EventHandler eventHandler = new EventHandler() {
@Override
public void dispatchEvent(LogEvent logEvent) throws Exception {
// Send event to our log endpoint as documented in
// https://developers.optimizely.com/x/events/api/index.html
}
};
// Build a manager
OptimizelyManager optimizelyManager = OptimizelyManager.builder().withSDKKey("<Your_SDK_Key>").withEventDispatchInterval(60, TimeUnit.SECONDS).withEventHandler(eventHandler).build(context);
// With the new Android O differences, you need to register the
// service for the intent filter you desire in code instead of in the manifest.
EventRescheduler eventRescheduler = new EventRescheduler();
context.registerReceiver(eventRescheduler, new IntentFilter(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION));
}
use of com.optimizely.ab.android.sdk.OptimizelyManager in project android-sdk by optimizely.
the class APISamplesInJava method samplesForDoc_ErrorHandler.
public static void samplesForDoc_ErrorHandler(Context context) {
// -- sample starts here
// Error handler that raises exceptions
ErrorHandler errorHandler = new RaiseExceptionErrorHandler();
OptimizelyManager optimizelyManager = OptimizelyManager.builder().withSDKKey("<Your_SDK_Key>").withErrorHandler(errorHandler).withDatafileDownloadInterval(15, TimeUnit.MINUTES).build(context);
}
use of com.optimizely.ab.android.sdk.OptimizelyManager in project android-sdk by optimizely.
the class APISamplesInJava method samplesForDecide.
public static void samplesForDecide(Context context) {
// this default-options will be applied to all following decide calls.
List<OptimizelyDecideOption> defaultDecideOptions = Arrays.asList(OptimizelyDecideOption.DISABLE_DECISION_EVENT);
OptimizelyManager optimizelyManager = OptimizelyManager.builder().withSDKKey("FCnSegiEkRry9rhVMroit4").withDefaultDecideOptions(defaultDecideOptions).build(context);
optimizelyManager.initialize(context, R.raw.datafile, optimizelyClient -> {
// createUserContext
String userId = "user_123";
Map<String, Object> attributes = new HashMap<>();
attributes.put("is_logged_in", false);
attributes.put("app_version", "1.3.2");
OptimizelyUserContext user = optimizelyClient.createUserContext(userId, attributes);
// attributes can be set in this way too
user.setAttribute("location", "NY");
// decide
List<OptimizelyDecideOption> options = Arrays.asList(OptimizelyDecideOption.INCLUDE_REASONS);
OptimizelyDecision decision = user.decide("show_coupon", options);
// or can be called without options
// OptimizelyDecision decision = user.decide("show_coupon");
String variationKey = decision.getVariationKey();
boolean enabled = decision.getEnabled();
OptimizelyJSON variables = decision.getVariables();
String varStr = null;
try {
varStr = variables.getValue("text_color", String.class);
} catch (JsonParseException e) {
e.printStackTrace();
}
int varInt = (int) variables.toMap().get("discount");
String ruleKey = decision.getRuleKey();
String flagKey = decision.getFlagKey();
OptimizelyUserContext userContext = decision.getUserContext();
List<String> reasons = decision.getReasons();
Log.d("Samples", "decision: " + decision.toString());
Log.d("Samples", "items: " + variationKey + " " + String.valueOf(enabled) + " " + varStr + " " + String.valueOf(varInt) + " " + ruleKey + " " + flagKey + " " + userContext + " " + reasons);
// decideForKeys
List<String> keys = Arrays.asList("show_coupon", "bg-feature");
Map<String, OptimizelyDecision> decisionsMultiple = user.decideForKeys(keys);
OptimizelyDecision decision1 = decisionsMultiple.get(keys.get(0));
OptimizelyDecision decision2 = decisionsMultiple.get(keys.get(1));
Log.d("Samples", "decisionsMultiple: " + keys + " " + decision1.toString() + " " + decision2.toString());
// decideAll
List<OptimizelyDecideOption> options2 = Arrays.asList(OptimizelyDecideOption.ENABLED_FLAGS_ONLY);
Map<String, OptimizelyDecision> decisionsAll = user.decideAll(options2);
Set<String> allKeys = decisionsAll.keySet();
Collection<OptimizelyDecision> allDecisions = decisionsAll.values();
Log.d("Samples", "all keys: " + allKeys);
Log.d("Samples", "all decisions: " + allDecisions);
// trackEvent
user.trackEvent("sample_conversion");
});
}
Aggregations