Search in sources :

Example 1 with OptimizelyManager

use of com.optimizely.ab.android.sdk.OptimizelyManager 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");
}
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 2 with OptimizelyManager

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

Example 3 with OptimizelyManager

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

Example 4 with OptimizelyManager

use of com.optimizely.ab.android.sdk.OptimizelyManager 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>");
    });
}
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 5 with OptimizelyManager

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

Aggregations

OptimizelyManager (com.optimizely.ab.android.sdk.OptimizelyManager)25 OptimizelyClient (com.optimizely.ab.android.sdk.OptimizelyClient)19 OptimizelyUserContext (com.optimizely.ab.OptimizelyUserContext)15 OptimizelyDecision (com.optimizely.ab.optimizelydecision.OptimizelyDecision)13 HashMap (java.util.HashMap)10 OptimizelyJSON (com.optimizely.ab.optimizelyjson.OptimizelyJSON)5 JsonParseException (com.optimizely.ab.config.parser.JsonParseException)4 OptimizelyDecideOption (com.optimizely.ab.optimizelydecision.OptimizelyDecideOption)4 DefaultEventHandler (com.optimizely.ab.android.event_handler.DefaultEventHandler)2 EventHandler (com.optimizely.ab.event.EventHandler)2 OptimizelyConfig (com.optimizely.ab.optimizelyconfig.OptimizelyConfig)2 OptimizelyVariation (com.optimizely.ab.optimizelyconfig.OptimizelyVariation)2 Intent (android.content.Intent)1 IntentFilter (android.content.IntentFilter)1 NonNull (androidx.annotation.NonNull)1 OptimizelyExperimentsClient (com.kickstarter.libs.OptimizelyExperimentsClient)1 OptimizelyEnvironment (com.kickstarter.libs.models.OptimizelyEnvironment)1 OptimizelyDecisionContext (com.optimizely.ab.OptimizelyDecisionContext)1 OptimizelyForcedDecision (com.optimizely.ab.OptimizelyForcedDecision)1 EventRescheduler (com.optimizely.ab.android.event_handler.EventRescheduler)1