Search in sources :

Example 1 with JsonParseException

use of com.optimizely.ab.config.parser.JsonParseException 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)

Example 2 with JsonParseException

use of com.optimizely.ab.config.parser.JsonParseException in project android-sdk by optimizely.

the class APISamplesInJava method samplesForDoc_Decide.

public static void samplesForDoc_Decide(Context context) {
    OptimizelyManager optimizelyManager = OptimizelyManager.builder().withSDKKey("FCnSegiEkRry9rhVMroit4").build(context);
    OptimizelyClient optimizelyClient = optimizelyManager.initialize(context, R.raw.datafile);
    // -- sample starts here
    // create the user and decide which flag rule & variation they bucket into
    Map<String, Object> attributes = new HashMap<>();
    attributes.put("logged_in", true);
    OptimizelyUserContext user = optimizelyClient.createUserContext("user123", attributes);
    OptimizelyDecision decision = user.decide("product_sort");
    // variation. if null, decision fail with a critical error
    String variationKey = decision.getVariationKey();
    // flag enabled state:
    boolean enabled = decision.getEnabled();
    // all variable values
    OptimizelyJSON variables = decision.getVariables();
    // String variable value
    String varStr = null;
    try {
        varStr = variables.getValue("sort_method", String.class);
    } catch (JsonParseException e) {
        e.printStackTrace();
    }
    // Boolean variable value
    Boolean varBool = (Boolean) variables.toMap().get("k_boolean");
    // flag key for which decision was made
    String flagKey = decision.getFlagKey();
    // user for which the decision was made
    OptimizelyUserContext userContext = decision.getUserContext();
    // reasons for the decision
    List<String> reasons = decision.getReasons();
}
Also used : HashMap(java.util.HashMap) OptimizelyDecision(com.optimizely.ab.optimizelydecision.OptimizelyDecision) JsonParseException(com.optimizely.ab.config.parser.JsonParseException) OptimizelyClient(com.optimizely.ab.android.sdk.OptimizelyClient) OptimizelyManager(com.optimizely.ab.android.sdk.OptimizelyManager) OptimizelyJSON(com.optimizely.ab.optimizelyjson.OptimizelyJSON) OptimizelyUserContext(com.optimizely.ab.OptimizelyUserContext)

Example 3 with JsonParseException

use of com.optimizely.ab.config.parser.JsonParseException 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 4 with JsonParseException

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

Example 5 with JsonParseException

use of com.optimizely.ab.config.parser.JsonParseException in project android-sdk by optimizely.

the class OptimizelyClientTest method testGetValueOfOptimizelyJson.

// Accessibility testing of OptimizelyJSON.getValue
@Test
public void testGetValueOfOptimizelyJson() {
    assumeTrue(datafileVersion == Integer.parseInt(ProjectConfig.Version.V4.toString()));
    Map<String, Object> expectedMap = new HashMap<>();
    expectedMap.put("kk1", "vv1");
    expectedMap.put("kk2", false);
    OptimizelyClient optimizelyClient = new OptimizelyClient(optimizely, logger);
    OptimizelyJSON optimizelyJSON = optimizelyClient.getAllFeatureVariables(STRING_FEATURE_KEY, GENERIC_USER_ID);
    try {
        assertEquals(optimizelyJSON.getValue("first_letter", String.class), "H");
        assertEquals(optimizelyJSON.getValue("json_patched.k4", Map.class), expectedMap);
        // When given jsonKey does not exist
        assertNull(optimizelyJSON.getValue("json_patched.k5", String.class));
    } catch (JsonParseException e) {
        e.printStackTrace();
    }
}
Also used : OptimizelyJSON(com.optimizely.ab.optimizelyjson.OptimizelyJSON) HashMap(java.util.HashMap) Matchers.anyObject(org.mockito.Matchers.anyObject) JsonParseException(com.optimizely.ab.config.parser.JsonParseException) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.junit.Test)

Aggregations

JsonParseException (com.optimizely.ab.config.parser.JsonParseException)5 OptimizelyJSON (com.optimizely.ab.optimizelyjson.OptimizelyJSON)5 OptimizelyUserContext (com.optimizely.ab.OptimizelyUserContext)4 OptimizelyManager (com.optimizely.ab.android.sdk.OptimizelyManager)4 OptimizelyDecision (com.optimizely.ab.optimizelydecision.OptimizelyDecision)4 HashMap (java.util.HashMap)4 OptimizelyClient (com.optimizely.ab.android.sdk.OptimizelyClient)3 OptimizelyDecideOption (com.optimizely.ab.optimizelydecision.OptimizelyDecideOption)1 Map (java.util.Map)1 Test (org.junit.Test)1 Matchers.anyObject (org.mockito.Matchers.anyObject)1