Search in sources :

Example 11 with OptimizelyClient

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");
}
Also used : OptimizelyJSON(com.optimizely.ab.optimizelyjson.OptimizelyJSON) HashMap(java.util.HashMap) OptimizelyDecision(com.optimizely.ab.optimizelydecision.OptimizelyDecision) JsonParseException(com.optimizely.ab.config.parser.JsonParseException) OptimizelyClient(com.optimizely.ab.android.sdk.OptimizelyClient) OptimizelyUserContext(com.optimizely.ab.OptimizelyUserContext) OptimizelyManager(com.optimizely.ab.android.sdk.OptimizelyManager)

Example 12 with OptimizelyClient

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);
    });
}
Also used : EventHandler(com.optimizely.ab.event.EventHandler) DefaultEventHandler(com.optimizely.ab.android.event_handler.DefaultEventHandler) BatchEventProcessor(com.optimizely.ab.event.BatchEventProcessor) OptimizelyClient(com.optimizely.ab.android.sdk.OptimizelyClient) OptimizelyManager(com.optimizely.ab.android.sdk.OptimizelyManager)

Example 13 with OptimizelyClient

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);
}
Also used : OptimizelyDecision(com.optimizely.ab.optimizelydecision.OptimizelyDecision) OptimizelyClient(com.optimizely.ab.android.sdk.OptimizelyClient) OptimizelyUserContext(com.optimizely.ab.OptimizelyUserContext) OptimizelyManager(com.optimizely.ab.android.sdk.OptimizelyManager)

Example 14 with OptimizelyClient

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>");
}
Also used : HashMap(java.util.HashMap) OptimizelyDecision(com.optimizely.ab.optimizelydecision.OptimizelyDecision) OptimizelyClient(com.optimizely.ab.android.sdk.OptimizelyClient) OptimizelyUserContext(com.optimizely.ab.OptimizelyUserContext) OptimizelyManager(com.optimizely.ab.android.sdk.OptimizelyManager)

Example 15 with OptimizelyClient

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();
}
Also used : OptimizelyClient(com.optimizely.ab.android.sdk.OptimizelyClient) OptimizelyManager(com.optimizely.ab.android.sdk.OptimizelyManager)

Aggregations

OptimizelyClient (com.optimizely.ab.android.sdk.OptimizelyClient)19 OptimizelyManager (com.optimizely.ab.android.sdk.OptimizelyManager)19 OptimizelyUserContext (com.optimizely.ab.OptimizelyUserContext)14 OptimizelyDecision (com.optimizely.ab.optimizelydecision.OptimizelyDecision)12 HashMap (java.util.HashMap)8 OptimizelyJSON (com.optimizely.ab.optimizelyjson.OptimizelyJSON)4 JsonParseException (com.optimizely.ab.config.parser.JsonParseException)3 OptimizelyDecideOption (com.optimizely.ab.optimizelydecision.OptimizelyDecideOption)3 OptimizelyDecisionContext (com.optimizely.ab.OptimizelyDecisionContext)1 OptimizelyForcedDecision (com.optimizely.ab.OptimizelyForcedDecision)1 DefaultEventHandler (com.optimizely.ab.android.event_handler.DefaultEventHandler)1 Variation (com.optimizely.ab.config.Variation)1 BatchEventProcessor (com.optimizely.ab.event.BatchEventProcessor)1 EventHandler (com.optimizely.ab.event.EventHandler)1 OptimizelyConfig (com.optimizely.ab.optimizelyconfig.OptimizelyConfig)1 OptimizelyVariation (com.optimizely.ab.optimizelyconfig.OptimizelyVariation)1