Search in sources :

Example 16 with OptimizelyManager

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>");
}
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 17 with OptimizelyManager

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

Example 18 with OptimizelyManager

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

Example 19 with OptimizelyManager

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

Example 20 with OptimizelyManager

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");
    });
}
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)

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