Search in sources :

Example 21 with OptimizelyManager

use of com.optimizely.ab.android.sdk.OptimizelyManager in project android-sdk by optimizely.

the class APISamplesInJava method samplesForDoc_InitializeSDK.

public static void samplesForDoc_InitializeSDK(Context context) {
    // -- sample starts here
    // Build a manager
    OptimizelyManager optimizelyManager = OptimizelyManager.builder().withSDKKey("<Your_SDK_Key>").withDatafileDownloadInterval(15, TimeUnit.MINUTES).withEventDispatchInterval(30, TimeUnit.SECONDS).build(context);
    // Instantiate a client synchronously with a bundled datafile
    String datafile = "REPLACE_WITH_YOUR_DATAFILE";
    OptimizelyClient optimizelyClient = optimizelyManager.initialize(context, datafile);
    // Or, instantiate it asynchronously with a callback
    optimizelyManager.initialize(context, null, (OptimizelyClient client) -> {
        // flag decision
        OptimizelyUserContext user = client.createUserContext("<User_ID>");
        OptimizelyDecision decision = user.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 22 with OptimizelyManager

use of com.optimizely.ab.android.sdk.OptimizelyManager in project android-sdk by optimizely.

the class APISamplesInJava method samplesForDoc_DatafilePolling.

public static void samplesForDoc_DatafilePolling(Context context) {
    // -- sample starts here
    // Poll every 15 minutes
    OptimizelyManager optimizelyManager = OptimizelyManager.builder().withSDKKey("<Your_SDK_Key>").withDatafileDownloadInterval(15, TimeUnit.MINUTES).build(context);
    // datafile notifiation
    OptimizelyClient optimizelyClient = optimizelyManager.initialize(context, R.raw.datafile);
    // -- sample starts here
    optimizelyClient.addUpdateConfigNotificationHandler(notification -> {
        Log.d("Optimizely", "got datafile change");
    });
}
Also used : OptimizelyClient(com.optimizely.ab.android.sdk.OptimizelyClient) OptimizelyManager(com.optimizely.ab.android.sdk.OptimizelyManager)

Example 23 with OptimizelyManager

use of com.optimizely.ab.android.sdk.OptimizelyManager in project android-sdk by optimizely.

the class APISamplesInJava method samplesForDoc_EventBatchingDefault.

public static void samplesForDoc_EventBatchingDefault(Context context) {
    // -- sample starts here
    OptimizelyManager optimizelyManager = OptimizelyManager.builder().withSDKKey("<Your_SDK_Key>").build(context);
    OptimizelyClient optimizely = optimizelyManager.initialize(context, R.raw.datafile);
}
Also used : OptimizelyClient(com.optimizely.ab.android.sdk.OptimizelyClient) OptimizelyManager(com.optimizely.ab.android.sdk.OptimizelyManager)

Example 24 with OptimizelyManager

use of com.optimizely.ab.android.sdk.OptimizelyManager in project android-sdk by optimizely.

the class APISamplesInJava method samplesForDoc_NotificatonListener.

public static void samplesForDoc_NotificatonListener(Context context) {
    OptimizelyManager optimizelyManager = OptimizelyManager.builder().withSDKKey("FCnSegiEkRry9rhVMroit4").build(context);
    OptimizelyClient optimizelyClient = optimizelyManager.initialize(context, R.raw.datafile);
    // -- sample starts here
    // Add Notification Listener (LogEvent)
    int notificationId = optimizelyClient.addLogEventNotificationHandler(logEvent -> {
        Log.d("Optimizely", "event dispatched: " + logEvent);
    });
    // Remove Notification Listener
    optimizelyClient.getNotificationCenter().removeNotificationListener(notificationId);
    // Remove all Notification Listeners
    optimizelyClient.getNotificationCenter().clearAllNotificationListeners();
    // Remove all Notification Listeners of a certain type
    optimizelyClient.getNotificationCenter().clearNotificationListeners(DecisionNotification.class);
    // notification listener types
    // -- sample starts here
    // SET UP DECISION NOTIFICATION LISTENER
    int notificationId1 = optimizelyClient.addDecisionNotificationHandler(notification -> {
        // Access type on decisionObject to get type of decision
        String decisionType = notification.getType();
        if (decisionType == "flag") {
            Map<String, ?> flagDecisionInfo = notification.getDecisionInfo();
            String flagKey = (String) flagDecisionInfo.get("flagKey");
            Boolean enabled = (Boolean) flagDecisionInfo.get("enabled");
            Boolean decisionEventDispatched = (Boolean) flagDecisionInfo.get("decisionEventDispatched");
        // Send data to analytics provider here
        }
    });
    // SET UP LOG EVENT NOTIFICATION LISTENER
    int notificationId2 = optimizelyClient.addLogEventNotificationHandler(notification -> {
    // process the logEvent object here (send to analytics provider, audit/inspect data)
    });
    // SET UP OPTIMIZELY CONFIG NOTIFICATION LISTENER
    int notificationId3 = optimizelyClient.addUpdateConfigNotificationHandler(notification -> {
        OptimizelyConfig optimizelyConfig = optimizelyClient.getOptimizelyConfig();
    });
    // SET UP TRACK LISTENER
    int notificationId4 = optimizelyClient.addTrackNotificationHandler(notification -> {
    // process the event here (send to analytics provider, audit/inspect data)
    });
}
Also used : OptimizelyConfig(com.optimizely.ab.optimizelyconfig.OptimizelyConfig) OptimizelyClient(com.optimizely.ab.android.sdk.OptimizelyClient) OptimizelyManager(com.optimizely.ab.android.sdk.OptimizelyManager)

Example 25 with OptimizelyManager

use of com.optimizely.ab.android.sdk.OptimizelyManager in project android-oss by kickstarter.

the class ApplicationModule method provideOptimizely.

@Provides
@Singleton
@NonNull
ExperimentsClientType provideOptimizely(@ApplicationContext @NonNull final Context context, @NonNull final ApiEndpoint apiEndpoint, @NonNull final Build build) {
    final OptimizelyEnvironment optimizelyEnvironment;
    if (apiEndpoint == ApiEndpoint.PRODUCTION) {
        optimizelyEnvironment = OptimizelyEnvironment.PRODUCTION;
    } else if (apiEndpoint == ApiEndpoint.STAGING) {
        optimizelyEnvironment = OptimizelyEnvironment.STAGING;
    } else {
        optimizelyEnvironment = OptimizelyEnvironment.DEVELOPMENT;
    }
    final OptimizelyManager optimizelyManager = OptimizelyManager.builder().withSDKKey(optimizelyEnvironment.getSdkKey()).withDatafileDownloadInterval(15, TimeUnit.MINUTES).withEventDispatchInterval(2L, TimeUnit.MILLISECONDS).build(context);
    optimizelyManager.initialize(context, null, optimizely -> {
        if (!optimizely.isValid()) {
            FirebaseCrashlytics.getInstance().recordException(new Throwable("Optimizely failed to initialize."));
        } else {
            if (build.isDebug()) {
                Timber.d(ApplicationModule.class.getSimpleName(), "🔮 Optimizely successfully initialized.");
            }
            context.sendBroadcast(new Intent(ExperimentsClientTypeKt.EXPERIMENTS_CLIENT_READY));
        }
    });
    return new OptimizelyExperimentsClient(optimizelyManager, optimizelyEnvironment);
}
Also used : OptimizelyExperimentsClient(com.kickstarter.libs.OptimizelyExperimentsClient) OptimizelyEnvironment(com.kickstarter.libs.models.OptimizelyEnvironment) Intent(android.content.Intent) OptimizelyManager(com.optimizely.ab.android.sdk.OptimizelyManager) Singleton(javax.inject.Singleton) NonNull(androidx.annotation.NonNull) Provides(dagger.Provides)

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