public class

Optimizely

extends Object
java.lang.Object
   ↳ com.optimizely.Optimizely

Class Overview

This class defines the Optimizely SDK interface.

The Optimizely SDK is configurable through static methods defined on this class. startOptimizely(String, android.app.Application) must be called in order to initialize Optimizely. Multiple calls to startOptimizely are allowed.

Summary

Nested Classes
enum Optimizely.OptimizelyStartState Enum for the Optimizely startup state, ensures that Optimizely is only started once  
Constants
String DUMP_NETWORK_KEY
String PROJECT_ID_KEY
String VERBOSE_LOGGING_KEY
Fields
public Application application
Public Methods
static boolean activateAllManualExperiments()
Activates all manual experiments in your data file.
static boolean activateManualExperiment(String experimentId)
Activates a manual experiment with the given id.
static void addOptimizelyEventListener(OptimizelyEventListener listener)
Add the given listener to our list if it does not already exist.
static LiveVariable<Boolean> booleanForKey(String variableKey, Boolean defaultValue, Callback<Boolean> callback)
Method to create a OptimizelyVariable in client app with type boolean.
static LiveVariable<Boolean> booleanForKey(String variableKey, Boolean defaultValue)
Method to create a OptimizelyVariable in client app with type boolean.
static LiveVariable<Boolean> booleanVariable(String variableKey, Boolean defaultValue)
This method is deprecated. Use booleanForKey(String, Boolean) Method to create a OptimizelyVariable in client app with type boolean.
static LiveVariable<Boolean> booleanVariable(String variableKey, Boolean defaultValue, Callback<Boolean> callback)
This method is deprecated. Use booleanForKey(String, Boolean, LiveVariable.Callback) Method to create a OptimizelyVariable in client app with type boolean. This method also takes a callback to be executed when value changes.
static OptimizelyCodeBlocks.OptimizelyCodeBlockBuilder codeBlock(String codeBlockName)
Get the OptimizelyCodeBlockBuilder in client app with the given name in order
static OptimizelyCodeBlock codeBlockWithBranchNames(String codeBlockName, String... branchNames)
This method is deprecated. Please use codeBlock(String) and the OptimizelyCodeBlocks.OptimizelyCodeBlockBuilder interface.
static LiveVariable<Integer> colorForKey(String variableKey, int defaultValue)
Method to create a OptimizelyVariable in client app with type Color.
static LiveVariable<Integer> colorForKey(String variableKey, int defaultValue, Callback<Integer> callback)
Method to create a OptimizelyVariable in client app with type Color.
static LiveVariable<Integer> colorVariable(String variableKey, int defaultValue, Callback<Integer> callback)
This method is deprecated. Use colorForKey(String, int, LiveVariable.Callback) Method to create a OptimizelyVariable in client app with type Color. This method also takes a callback to be executed when value changes.
static LiveVariable<Integer> colorVariable(String variableKey, int defaultValue)
This method is deprecated. Use colorForKey(String, int) Method to create a OptimizelyVariable in client app with type Color.
static void disableKillSwitch()
Disables the Optimizely Kill Switch

The Optimizely Kill Switch allows the SDK to be disabled remotely.

static void enableEditor()
This method makes the device available to the Optimizely web editor.
static void enablePreview(boolean showFloatingButton)
static void enablePreview()
Makes Optimizely launch in preview mode.
static void fetchNewDataFile()
Forces Optimizely to download and cache the most recent version of the data file from the CDN.
fetchNewDataFile(boolean fetchAsync)
static void fetchNewDataFileAsync()
Forces Optimizely to download and cache the most recent version of the data file from the CDN.
static LiveVariable<Float> floatForKey(String variableKey, float defaultValue, Callback<Float> callback)
Method to create a OptimizelyVariable in client app with type as float precision.
static LiveVariable<Float> floatForKey(String variableKey, float defaultValue)
Method to create a OptimizelyVariable in client app with type as float precision.
static LiveVariable<Float> floatVariable(String variableKey, float defaultValue)
This method is deprecated. Use floatForKey(String, float) Method to create a OptimizelyVariable in client app with type as float precision.
static LiveVariable<Float> floatVariable(String variableKey, float defaultValue, Callback<Float> callback)
This method is deprecated. Use floatForKey(String, float, LiveVariable.Callback) Method to create a OptimizelyVariable in client app with type as float precision. This method also takes a callback to be executed when value changes.
static void forceVariation(String variationId, String experimentId)
Force the user into a particular variation of an experiment.
static Map<String, String> getActiveExperiments()
This method is deprecated. Use getAllExperiments() or getVisitedExperiments()
static Map<String, OptimizelyExperimentData> getAllExperiments()
Get all experiments.
static boolean getAppInForeground()
Application getApplication()
static ArrayList<OptimizelyAttribute> getAttributes()
This method returns a copy of all attributes that are defined in the data file.
static JSONArray getAudiences()
Get all audiences within the app.
OptimizelyAudiencesManager getAudiencesManager()
OptimizelyDataStore getDataStore()
static ArrayList<OptimizelyDimension> getDimensions()
This method is deprecated. will be removed
EditorModule getEditorModule()
static OptimizelyExperimentData getExperimentDataById(String experimentId)
Get an OptimizelyExperimentData Object representing the state of the experimentId that was searched If this is called before Optimizely Starts, it will return null.
IntegrationEventsDispatcher getIntegrationEventsDispatcher()
OptimizelyAttributesManager getOptimizelyAttributesManager(Context context)
Get Optimizely's Dimensions Manager
static String getOptimizelyEndUserId(Context context)
OptimizelySegmentsManager getOptimizelySegmentsManager()
Get Optimizely's Segments Manager
static Optimizely.OptimizelyStartState getStartState()
This method provides access to the start state of the Optimizely singleton.
Thread.UncaughtExceptionHandler getUncaughtExceptionHandler()
static String getUniversalUserId(Context context)
Returns the custom universal user Id
static String getUserId(Context context)
This method is deprecated. Please use `getUniversalUserId` or `getOptimizelyEndUserId` instead.
static String getValueForAttributeApiName(String attributeApiName, Context context)
This method returns the current value for the OptimizelyAttribute with the corresponding attributeApiName.
static String getValueForAttributeApiName(String attributeApiName)
This method returns the current value for the OptimizelyAttribute with the corresponding attributeApiName.
static String getValueForAttributeId(String attributeId)
This method returns the current value for the OptimizelyAttribute with the corresponding attributeId.
static String getValueForAttributeId(String attributeId, Context context)
This method returns the current value for the OptimizelyAttribute with the corresponding attributeId.
ViewModule getViewModule()
static Map<String, OptimizelyExperimentData> getVisitedExperiments()
Get all experiments that the user has seen to date (across all sessions).
static LiveVariable<Integer> integerForKey(String variableKey, int defaultValue, Callback<Integer> callback)
Method to create a OptimizelyVariable in client app with type as integer precision.
static LiveVariable<Integer> integerForKey(String variableKey, int defaultValue)
Method to create a OptimizelyVariable in client app with type as integer precision.
static LiveVariable<Integer> integerVariable(String variableKey, int defaultValue, Callback<Integer> callback)
This method is deprecated. Use integerVariable(String, int, LiveVariable.Callback) Method to create a OptimizelyVariable in client app with type as integer precision. This method also takes a callback to be executed when value changes.
static LiveVariable<Integer> integerVariable(String variableKey, int defaultValue)
This method is deprecated. Use integerVariable(String, int) Method to create a OptimizelyVariable in client app with type as integer precision.
boolean isRestarting()
static boolean isUserInAudience(String audienceID)
Takes an audience is and returns whether the current state of the user would have satisfied that audience's condition(s).
static void launchPreview(PendingIntent pendingIntent)
Makes Optimizely launch in preview mode.
static LiveVariable<Point> pointForKey(String variableKey, Point defaultValue)
Method to create a OptimizelyVariable in client app with type Point.
static LiveVariable<Point> pointForKey(String variableKey, Point defaultValue, Callback<Point> callback)
Method to create a OptimizelyVariable in client app with type Point.
static LiveVariable<Point> pointVariable(String variableKey, Point defaultValue, Callback<Point> callback)
This method is deprecated. Use pointForKey(String, Point, LiveVariable.Callback) Method to create a OptimizelyVariable in client app with type Point. This method also takes a callback to be executed when value changes.
static LiveVariable<Point> pointVariable(String variableKey, Point defaultValue)
This method is deprecated. Use pointForKey(String, Point) Method to create a OptimizelyVariable in client app with type Point.
static LiveVariable<Rect> rectForKey(String variableKey, Rect defaultValue)
Method to create a OptimizelyVariable in client app with type Rectangle.
static LiveVariable<Rect> rectForKey(String variableKey, Rect defaultValue, Callback<Rect> callback)
Method to create a OptimizelyVariable in client app with type Rectangle.
static LiveVariable<Rect> rectVariable(String variableKey, Rect defaultValue)
This method is deprecated. Use rectForKey(String, Rect) Method to create a OptimizelyVariable in client app with type Rectangle.
static LiveVariable<Rect> rectVariable(String variableKey, Rect defaultValue, Callback<Rect> callback)
This method is deprecated. Use rectForKey(String, Rect, LiveVariable.Callback) Method to create a OptimizelyVariable in client app with type Rectangle. This method also takes a callback to be executed when value changes.
static void refreshExperimentData()
This method is deprecated. Use refreshExperiments() This method manually refreshes all currently running experiments so as to take into account the most recent targeting conditions and tags.

Note: The use of this method may invalidate statistical results.

static void refreshExperiments()
This method manually refreshes all currently running experiments so as to take into account the most recent targeting conditions and tags.
static void registerClassWithOptlyFields(Class... clazz)
Preemptively let the Optimizely SDK know about classes with Live Varibles and Code Blocks

Before Android Studio 2.0 and the new Jack and Jill compiler the SDK was able to scan the Dexfile at runtime to find Live Variables and Code Blocks in classes dynamically.

static void registerPlugin(OptimizelyPlugin plugin)
Register the given plugin for activation now if Optimizely is running or for activiation once Optimizely loads
static void removeOptimizelyEventListener(OptimizelyEventListener listener)
static void resetUserBucketing(Context userContext)
Reset the user bucketing before Optimizely starts so the app can record a new session.
static String sdkVersion()
Get the Optimizely SDK version.
static void sendEvents()
Manually sends events to the server.
static void setAppInForeground(boolean appInForeground)
Sets whether optimizely considers itself to be in the foreground
static void setAutomaticEventSending(boolean automaticEventSending)
Sets whether events will be automatically sent to the backend on activity transitions.
static void setCustomTag(String key, String value)
This method allows you to add custom tags for targeting.
static void setDataFileDownloadInterval(long dataFileDownloadInterval)
static void setDumpNetworkCalls(boolean dumpNetworkCalls)
Enable or disable network logging.
static void setEditGestureEnabled(boolean editGestureEnabled)
Enable or disable the Optimizely edit gesture.
void setEditorModule(EditorModule editorModule)
static void setEnableUncaughtExceptionHandler(boolean enableUncaughtExceptionHandler)
Enable or disable Optimizely Uncaught Exception handling.
static void setEventRecordingEnabled(boolean eventRecordingEnabled)
Enable or disable the Optimizely event recording.
static void setHttpClient(OkHttpClient httpClient)
Set the HTTP client used by the Optimizely SDK for all network calls except for the data file downloads.
static void setNetworkTimeout(int networkTimeout)
void setOptimizelyData(OptimizelyData data)
Set the OptimizelyData
void setOptimizelyEventsManager(OptimizelyEventsManager eventsManager)
static void setOptimizelyId(String id, View view)
void setShowFloatingButton(boolean showFloatingButton)
void setStartState(Optimizely.OptimizelyStartState startState)
static void setUniversalUserId(Context context, String universalUserId)
static void setUserId(Context context, String userId)
This method is deprecated. This method is deprecated. Use setUniversalUserId instead.
static boolean setValueForAttributeApiName(String attributeValue, String attributeApiName)
This method returns whether the value was successfully set for an attribute with the corresponding attributeApiName.
static boolean setValueForAttributeApiName(String attributeValue, String attributeApiName, Context context)
This method returns whether the value was successfully set for an attribute.
static boolean setValueForAttributeId(String attributeValue, String attributeId, Context context)
This method returns whether the value was successfully set for an attribute.
static boolean setValueForAttributeId(String attributeValue, String attributeId)
This method returns whether the value was successfully set for an attribute with corresponding attributeId.
static void setVerboseLogging(boolean enableLogging)
Enable or disable verbose logging.
void setViewModule(ViewModule viewModule)
static void setVisualExperimentsEnabled(boolean visualExperimentsEnabled)
Enable or disable the Optimizely views modification.
static void setshouldReloadExperimentsOnForegrounding(boolean shouldReloadExperimentsOnForegrounding)
Indicates whether experiments should be reloaded on foregrounding.
static void startOptimizely(String apiToken, Application application)
This method is deprecated. Use startOptimizelyWithAPIToken(String, Application) Calling this activates the Optimizely framework. If not called, the app will behave as though Optimizely wasn't included.

This method attempts to fetch the latest Optimizely experiment data with a timeout of two seconds and returns im after the data has been successfully loaded. In the case of a timeout and when no experiment data is available, the user will not be counted as a visitor to your experiment.

static void startOptimizelyAsync(String apiToken, Application application, OptimizelyEventListener callback)
Calling this method begins the Optimizely activation (see startOptimizely(String, android.app.Application)) but returns before the network request to download the data file is made.
static void startOptimizelyWithAPIToken(String apiToken, Application application, OptimizelyEventListener optimizelyEventListener)
Calling this activates the Optimizely framework.
static void startOptimizelyWithAPIToken(String apiToken, Application application)
Calling this activates the Optimizely framework.
void stopOptimizely()
Tear down Optimizely and remove components
static LiveVariable<String> stringForKey(String variableKey, String defaultValue, Callback<String> callback)
Method to create a OptimizelyVariable in client app with type String.
static LiveVariable<String> stringForKey(String variableKey, String defaultValue)
Method to create a OptimizelyVariable in client app with type String.
static LiveVariable<String> stringVariable(String variableKey, String defaultValue)
This method is deprecated. Use stringForKey(String, String) Method to create a OptimizelyVariable in client app with type String.
static LiveVariable<String> stringVariable(String variableKey, String defaultValue, Callback<String> callback)
This method is deprecated. Use stringForKey(String, String, LiveVariable.Callback) Method to create a OptimizelyVariable in client app with type String. This method also takes a callback to be executed when value changes.
static void trackEvent(String description)
This method informs Optimizely that a custom goal with key `description` occurred.
static void trackRevenue(int revenueAmount)
This method is deprecated. use trackRevenueWithDescription(int, String) instead.
static void trackRevenueWithDescription(int revenueAmount, String revenueDescription)
This method informs Optimizely that a revenue goal custom event occurred.
static void trackViewGoal(String id)
[Expand]
Inherited Methods
From class java.lang.Object

Constants

public static final String DUMP_NETWORK_KEY

Constant Value: "com.optimizely.DUMP_NETWORK"

public static final String PROJECT_ID_KEY

Constant Value: "com.optimizely.PROJECT_ID"

public static final String VERBOSE_LOGGING_KEY

Constant Value: "com.optimizely.VERBOSE_LOGGING"

Fields

public Application application

Public Methods

public static boolean activateAllManualExperiments ()

Activates all manual experiments in your data file. If it passes targeting, the experiments will be bucketed and marked as visited. This must be called after startOptimizely(String, Application)

Returns
  • boolean depending on whether or not we were able to activate all manual experiments. Turn on verbose logging for more debugging info.

public static boolean activateManualExperiment (String experimentId)

Activates a manual experiment with the given id. If it passes targeting, the experiment will be bucketed and marked as visited. This must be called after startOptimizely(String, Application)

Returns
  • boolean depending on whether or not we successfully activated the experiment. Turn on verbose logging for more debugging info

public static void addOptimizelyEventListener (OptimizelyEventListener listener)

Add the given listener to our list if it does not already exist. Listeners are held by weak reference and will need to be re-registered. For example, if your Activity subclass implements the listener interface, you will need to re-register the activity in each onCreate.

Parameters
listener the listener to add

public static LiveVariable<Boolean> booleanForKey (String variableKey, Boolean defaultValue, Callback<Boolean> callback)

Method to create a OptimizelyVariable in client app with type boolean. This method also takes a callback to be executed when value changes.

Parameters
variableKey the name of the variable. Must be unique.
defaultValue the value that will be returned if no value is specified in the experiment.
callback the callback to be executed when value changes in editor.
Returns
  • a new LiveVariable which may be queried with get() to retrieve the value.

public static LiveVariable<Boolean> booleanForKey (String variableKey, Boolean defaultValue)

Method to create a OptimizelyVariable in client app with type boolean.

Parameters
variableKey the name of the variable. Must be unique.
defaultValue the value that will be returned if no value is specified in the experiment.
Returns
  • a new LiveVariable which may be queried with get() to retrieve the value.

public static LiveVariable<Boolean> booleanVariable (String variableKey, Boolean defaultValue)

This method is deprecated.
Use booleanForKey(String, Boolean) Method to create a OptimizelyVariable in client app with type boolean.

Parameters
variableKey the name of the variable. Must be unique.
defaultValue the value that will be returned if no value is specified in the experiment.
Returns
  • a new LiveVariable which may be queried with get() to retrieve the value.

public static LiveVariable<Boolean> booleanVariable (String variableKey, Boolean defaultValue, Callback<Boolean> callback)

This method is deprecated.
Use booleanForKey(String, Boolean, LiveVariable.Callback) Method to create a OptimizelyVariable in client app with type boolean. This method also takes a callback to be executed when value changes.

Parameters
variableKey the name of the variable. Must be unique.
defaultValue the value that will be returned if no value is specified in the experiment.
callback the callback to be executed when value changes in editor.
Returns
  • a new LiveVariable which may be queried with get() to retrieve the value.

public static OptimizelyCodeBlocks.OptimizelyCodeBlockBuilder codeBlock (String codeBlockName)

Get the OptimizelyCodeBlockBuilder in client app with the given name in order

Parameters
codeBlockName name of the code block.
Returns
  • the code block builder with the given name

public static OptimizelyCodeBlock codeBlockWithBranchNames (String codeBlockName, String... branchNames)

This method is deprecated.
Please use codeBlock(String) and the OptimizelyCodeBlocks.OptimizelyCodeBlockBuilder interface.

Get the OptimizelyCodeBlock in client app with the given branch names.

Parameters
codeBlockName name of the code block.
branchNames array of 0 or more branch names
Returns
  • the code block associated with the given name

public static LiveVariable<Integer> colorForKey (String variableKey, int defaultValue)

Method to create a OptimizelyVariable in client app with type Color.

Parameters
variableKey the name of the variable. Must be unique.
defaultValue the value that will be returned if no value is specified in the experiment.
Returns
  • a new LiveVariable which may be queried with get() to retrieve the value.

public static LiveVariable<Integer> colorForKey (String variableKey, int defaultValue, Callback<Integer> callback)

Method to create a OptimizelyVariable in client app with type Color. This method also takes a callback to be executed when value changes.

Parameters
variableKey the name of the variable. Must be unique.
defaultValue the value that will be returned if no value is specified in the experiment.
callback the callback to be executed when value changes in editor.
Returns
  • a new LiveVariable which may be queried with get() to retrieve the value.

public static LiveVariable<Integer> colorVariable (String variableKey, int defaultValue, Callback<Integer> callback)

This method is deprecated.
Use colorForKey(String, int, LiveVariable.Callback) Method to create a OptimizelyVariable in client app with type Color. This method also takes a callback to be executed when value changes.

Parameters
variableKey the name of the variable. Must be unique.
defaultValue the value that will be returned if no value is specified in the experiment.
callback the callback to be executed when value changes in editor.
Returns
  • a new LiveVariable which may be queried with get() to retrieve the value.

public static LiveVariable<Integer> colorVariable (String variableKey, int defaultValue)

This method is deprecated.
Use colorForKey(String, int) Method to create a OptimizelyVariable in client app with type Color.

Parameters
variableKey the name of the variable. Must be unique.
defaultValue the value that will be returned if no value is specified in the experiment.
Returns
  • a new LiveVariable which may be queried with get() to retrieve the value.

public static void disableKillSwitch ()

Disables the Optimizely Kill Switch

The Optimizely Kill Switch allows the SDK to be disabled remotely. This is a common request if an app starts crashing to due to the SDK. However, For this to work a blocking network call must be made while the SDK is starting up. This directly affects an app's startup time. Disabling this will speed up startup time, particularly when using startOptimizelyAsync(String, Application, OptimizelyEventListener).

public static void enableEditor ()

This method makes the device available to the Optimizely web editor.

It is a programmatic shortcut for developers to place the device in edit mode, rather than opening the app through the custom URL accessible in the Optimizely web editor.

This should be removed from test and production builds. Should be called before `startOptimizely()`.

public static void enablePreview (boolean showFloatingButton)

Parameters
showFloatingButton toggle controlling whether or not the floating Optimizely "O" is shown

The floating O is button that launches the preview UI. If it is disabled you must use launchPreview(PendingIntent) manually.

See Also
  • {@link #enablePreview()}

public static void enablePreview ()

Makes Optimizely launch in preview mode.

The datafile on the device will be synced with the one on our servers. This method may block the UI thread when initializing. This method should NOT be called in production builds.

If this method is called after startOptimizelyWithAPIToken(String, Application, OptimizelyEventListener) or startOptimizelyAsync(String, Application, OptimizelyEventListener) it will no-op.

public static void fetchNewDataFile ()

Forces Optimizely to download and cache the most recent version of the data file from the CDN.

Data file will be downloaded synchronously.

public fetchNewDataFile (boolean fetchAsync)

public static void fetchNewDataFileAsync ()

Forces Optimizely to download and cache the most recent version of the data file from the CDN.

Download will happen asynchronously on a background. You will be notified via a call to onOptimizelyDataFileLoaded()

public static LiveVariable<Float> floatForKey (String variableKey, float defaultValue, Callback<Float> callback)

Method to create a OptimizelyVariable in client app with type as float precision. This method also takes a callback to be executed when value changes.

Parameters
variableKey the name of the variable. Must be unique.
defaultValue the value that will be returned if no value is specified in the experiment.
callback the callback to be executed when value changes in editor.
Returns
  • a new LiveVariable which may be queried with get() to retrieve the value.

public static LiveVariable<Float> floatForKey (String variableKey, float defaultValue)

Method to create a OptimizelyVariable in client app with type as float precision.

Parameters
variableKey the name of the variable. Must be unique.
defaultValue the value that will be returned if no value is specified in the experiment.
Returns
  • a new LiveVariable which may be queried with get() to retrieve the value.

public static LiveVariable<Float> floatVariable (String variableKey, float defaultValue)

This method is deprecated.
Use floatForKey(String, float) Method to create a OptimizelyVariable in client app with type as float precision.

Parameters
variableKey the name of the variable. Must be unique.
defaultValue the value that will be returned if no value is specified in the experiment.
Returns
  • a new LiveVariable which may be queried with get() to retrieve the value.

public static LiveVariable<Float> floatVariable (String variableKey, float defaultValue, Callback<Float> callback)

This method is deprecated.
Use floatForKey(String, float, LiveVariable.Callback) Method to create a OptimizelyVariable in client app with type as float precision. This method also takes a callback to be executed when value changes.

Parameters
variableKey the name of the variable. Must be unique.
defaultValue the value that will be returned if no value is specified in the experiment.
callback the callback to be executed when value changes in editor.
Returns
  • a new LiveVariable which may be queried with get() to retrieve the value.

public static void forceVariation (String variationId, String experimentId)

Force the user into a particular variation of an experiment. This function must be called before startOptimizely(). If the experimentId or variationId is invalid, there will be no effect. If the relevant data is not downloaded yet, nothing will be done. This function only takes effect if the app is running in normal mode. This function should be used for QA only, since forcing a variation will clear out any saved experiment states for the user.

Parameters
variationId The id of the variation you wish to bucket the user into.
experimentId The id of the experiment you are bucketing within.

public static Map<String, String> getActiveExperiments ()

This method is deprecated.
Use getAllExperiments() or getVisitedExperiments()

Get the active variations for all active experiments. Calling this function before calling startOptimizely(String, android.app.Application) will return an empty map.

Returns
  • a mapping of experiment id to active variation id

public static Map<String, OptimizelyExperimentData> getAllExperiments ()

Get all experiments. Calling this function before calling startOptimizely(String, android.app.Application) will return an empty map.

Returns
  • a mapping of experiment id to experiment state

public static boolean getAppInForeground ()

Returns
  • Whether the app is currently in the foreground. This may temporarily be incorrect during an activity transition.

public Application getApplication ()

Returns
  • the current application instance

public static ArrayList<OptimizelyAttribute> getAttributes ()

This method returns a copy of all attributes that are defined in the data file.

Returns
  • If this is called before Optimizely starts, it will return an empty array If there are no attributes, it will return an empty array. Else it will return an ArrayList of OptimizelyAttribute objects.

public static JSONArray getAudiences ()

Get all audiences within the app.

Returns
  • A JSON array of all audiences, with a JSON Object for each audience. The keys for the audience dictionaries are "audience_id" "name" "conditions"

public OptimizelyAudiencesManager getAudiencesManager ()

public OptimizelyDataStore getDataStore ()

Returns
  • Returns the interface to the on-disk persistence store

public static ArrayList<OptimizelyDimension> getDimensions ()

This method is deprecated.
will be removed

This method returns a copy of all dimensions that are defined in the data file.

Returns
  • If this is called before Optimizely starts, it will return an empty array If there are no dimensions, it will return an empty array. Else it will return an ArrayList of OptimizelyDimension objects.
See Also

public EditorModule getEditorModule ()

public static OptimizelyExperimentData getExperimentDataById (String experimentId)

Get an OptimizelyExperimentData Object representing the state of the experimentId that was searched If this is called before Optimizely Starts, it will return null. If the experimentId does not exist or the Experiment could not be found, it will return null If the Experiment was found in the instance of Optimizely that is running, this function will return a corresponding OptimizelyExperimentData Object.

Parameters
experimentId The experiment id to search for. This can be found in the mobile editor.
Returns
  • The OptimizelyExperimentData representing the state of the Experiment.

public IntegrationEventsDispatcher getIntegrationEventsDispatcher ()

Returns
  • Returns the integration events manager used for SDK lifecycle events.

public OptimizelyAttributesManager getOptimizelyAttributesManager (Context context)

Get Optimizely's Dimensions Manager

Returns
  • a OptimizelyDimensionsManager instance

public static String getOptimizelyEndUserId (Context context)

public OptimizelySegmentsManager getOptimizelySegmentsManager ()

Get Optimizely's Segments Manager

Returns
  • a OptimizelySegmentsManager instance

public static Optimizely.OptimizelyStartState getStartState ()

This method provides access to the start state of the Optimizely singleton.

Returns
  • The Optimizely singleton's start state

public Thread.UncaughtExceptionHandler getUncaughtExceptionHandler ()

public static String getUniversalUserId (Context context)

Returns the custom universal user Id

Parameters
context The current application's context.
Returns
  • The custom user Id set by the Application.

public static String getUserId (Context context)

This method is deprecated.
Please use `getUniversalUserId` or `getOptimizelyEndUserId` instead.

Returns a unique identifier for the current user. If a userId has not been provided, this will return a persistent random id.

Parameters
context The current application's context
Returns
  • A unique identifier for the current user.

public static String getValueForAttributeApiName (String attributeApiName, Context context)

This method returns the current value for the OptimizelyAttribute with the corresponding attributeApiName.

Parameters
attributeApiName The attributeApiName to look up.
context The current Application Context.
Returns
  • If this is called before Optimizely starts, it will return what is stored in a key value store for the attributeApiName. If this is called after Optimizely starts, it will function the same as getValueForAttributeApiName(String).

public static String getValueForAttributeApiName (String attributeApiName)

This method returns the current value for the OptimizelyAttribute with the corresponding attributeApiName.

Parameters
attributeApiName the attributeApiName to look up.
Returns
  • If this is called before Optimizely starts, it will return null. If there is no attribute found with the attributeApiName, it will return null. If there is a attribute found, it will return the current value of the attribute.

public static String getValueForAttributeId (String attributeId)

This method returns the current value for the OptimizelyAttribute with the corresponding attributeId.

Parameters
attributeId The attributeId to look up.
Returns
  • If this is called before Optimizely starts, it will return null. If there is no attribute found with the attributeId, it will return null. If there is a attribute found, it will return the current value of the attribute.

public static String getValueForAttributeId (String attributeId, Context context)

This method returns the current value for the OptimizelyAttribute with the corresponding attributeId.

Parameters
attributeId The attributeId to look up.
context The current Application Context.
Returns
  • If this is called before Optimizely starts, it will return what is stored in a key value store for the attributeId. If this is called after Optimizely starts it will function the same as getValueForAttributeId(String).

public ViewModule getViewModule ()

public static Map<String, OptimizelyExperimentData> getVisitedExperiments ()

Get all experiments that the user has seen to date (across all sessions). Calling this function before calling startOptimizely(String, android.app.Application) will return an empty map. Excludes experiments which have been paused or archived.

Returns
  • a mapping of experiment id to experiment state for experiments that the user has visited.

public static LiveVariable<Integer> integerForKey (String variableKey, int defaultValue, Callback<Integer> callback)

Method to create a OptimizelyVariable in client app with type as integer precision. This method also takes a callback to be executed when value changes.

Parameters
variableKey the name of the variable. Must be unique.
defaultValue the value that will be returned if no value is specified in the experiment.
callback the callback to be executed when value changes in editor.
Returns
  • a new LiveVariable which may be queried with get() to retrieve the value.

public static LiveVariable<Integer> integerForKey (String variableKey, int defaultValue)

Method to create a OptimizelyVariable in client app with type as integer precision.

Parameters
variableKey the name of the variable. Must be unique.
defaultValue the value that will be returned if no value is specified in the experiment.
Returns
  • a new LiveVariable which may be queried with get() to retrieve the value.

public static LiveVariable<Integer> integerVariable (String variableKey, int defaultValue, Callback<Integer> callback)

This method is deprecated.
Use integerVariable(String, int, LiveVariable.Callback) Method to create a OptimizelyVariable in client app with type as integer precision. This method also takes a callback to be executed when value changes.

Parameters
variableKey the name of the variable. Must be unique.
defaultValue the value that will be returned if no value is specified in the experiment.
callback the callback to be executed when value changes in editor.
Returns
  • a new LiveVariable which may be queried with get() to retrieve the value.

public static LiveVariable<Integer> integerVariable (String variableKey, int defaultValue)

This method is deprecated.
Use integerVariable(String, int) Method to create a OptimizelyVariable in client app with type as integer precision.

Parameters
variableKey the name of the variable. Must be unique.
defaultValue the value that will be returned if no value is specified in the experiment.
Returns
  • a new LiveVariable which may be queried with get() to retrieve the value.

public boolean isRestarting ()

public static boolean isUserInAudience (String audienceID)

Takes an audience is and returns whether the current state of the user would have satisfied that audience's condition(s).

Parameters
audienceID ID of the audience you want to check.
Returns
  • true if any the user would satisfy the audience criteria, false otherwise

public static void launchPreview (PendingIntent pendingIntent)

Makes Optimizely launch in preview mode.

The datafile on the device will be synced with the one on our servers. This method may block the UI thread when initializing. This method should NOT be called in production builds.

If this method is called after startOptimizelyWithAPIToken(String, Application, OptimizelyEventListener) or startOptimizelyAsync(String, Application, OptimizelyEventListener) it will no-op.

Parameters
pendingIntent Provide a PendingIntent that will be launched instead of the default activity task's launcher when preview settings are applied to your app. Optimizely will alse launch the preview UI instead of just the floating the "O" over the running app. You will need to kill your app via the Android OS to stop preview mode. This is useful for devices without touch.

public static LiveVariable<Point> pointForKey (String variableKey, Point defaultValue)

Method to create a OptimizelyVariable in client app with type Point.

Parameters
variableKey the name of the variable. Must be unique.
defaultValue the value that will be returned if no value is specified in the experiment.
Returns
  • a new LiveVariable which may be queried with get() to retrieve the value.

public static LiveVariable<Point> pointForKey (String variableKey, Point defaultValue, Callback<Point> callback)

Method to create a OptimizelyVariable in client app with type Point. This method also takes a callback to be executed when value changes.

Parameters
variableKey the name of the variable. Must be unique.
defaultValue the value that will be returned if no value is specified in the experiment.
callback the callback to be executed when value changes in editor.
Returns
  • a new LiveVariable which may be queried with get() to retrieve the value.

public static LiveVariable<Point> pointVariable (String variableKey, Point defaultValue, Callback<Point> callback)

This method is deprecated.
Use pointForKey(String, Point, LiveVariable.Callback) Method to create a OptimizelyVariable in client app with type Point. This method also takes a callback to be executed when value changes.

Parameters
variableKey the name of the variable. Must be unique.
defaultValue the value that will be returned if no value is specified in the experiment.
callback the callback to be executed when value changes in editor.
Returns
  • a new LiveVariable which may be queried with get() to retrieve the value.

public static LiveVariable<Point> pointVariable (String variableKey, Point defaultValue)

This method is deprecated.
Use pointForKey(String, Point) Method to create a OptimizelyVariable in client app with type Point.

Parameters
variableKey the name of the variable. Must be unique.
defaultValue the value that will be returned if no value is specified in the experiment.
Returns
  • a new LiveVariable which may be queried with get() to retrieve the value.

public static LiveVariable<Rect> rectForKey (String variableKey, Rect defaultValue)

Method to create a OptimizelyVariable in client app with type Rectangle.

Parameters
variableKey the name of the variable. Must be unique.
defaultValue the value that will be returned if no value is specified in the experiment.
Returns
  • a new LiveVariable which may be queried with get() to retrieve the value.

public static LiveVariable<Rect> rectForKey (String variableKey, Rect defaultValue, Callback<Rect> callback)

Method to create a OptimizelyVariable in client app with type Rectangle. This method also takes a callback to be executed when value changes.

Parameters
variableKey the name of the variable. Must be unique.
defaultValue the value that will be returned if no value is specified in the experiment.
callback the callback to be executed when value changes in editor.
Returns
  • a new LiveVariable which may be queried with get() to retrieve the value.

public static LiveVariable<Rect> rectVariable (String variableKey, Rect defaultValue)

This method is deprecated.
Use rectForKey(String, Rect) Method to create a OptimizelyVariable in client app with type Rectangle.

Parameters
variableKey the name of the variable. Must be unique.
defaultValue the value that will be returned if no value is specified in the experiment.
Returns
  • a new LiveVariable which may be queried with get() to retrieve the value.

public static LiveVariable<Rect> rectVariable (String variableKey, Rect defaultValue, Callback<Rect> callback)

This method is deprecated.
Use rectForKey(String, Rect, LiveVariable.Callback) Method to create a OptimizelyVariable in client app with type Rectangle. This method also takes a callback to be executed when value changes.

Parameters
variableKey the name of the variable. Must be unique.
defaultValue the value that will be returned if no value is specified in the experiment.
callback the callback to be executed when value changes in editor.
Returns
  • a new LiveVariable which may be queried with get() to retrieve the value.

public static void refreshExperimentData ()

This method is deprecated.
Use refreshExperiments() This method manually refreshes all currently running experiments so as to take into account the most recent targeting conditions and tags.

Note: The use of this method may invalidate statistical results.

public static void refreshExperiments ()

This method manually refreshes all currently running experiments so as to take into account the most recent targeting conditions and tags.

Note: The use of this method may invalidate statistical results.

public static void registerClassWithOptlyFields (Class... clazz)

Preemptively let the Optimizely SDK know about classes with Live Varibles and Code Blocks

Before Android Studio 2.0 and the new Jack and Jill compiler the SDK was able to scan the Dexfile at runtime to find Live Variables and Code Blocks in classes dynamically. The Dexfile no longer contains all the classes in the app. Use this method to register classes with the SDK. This will cause the LiveVariables and CodeBlocks to show up on the Optimizely editor without needing to navigate to the first place in the host app they are referenced..

This method is *not* needed for normal mode operation of LiveVariables and CodeBlocks.

public static void registerPlugin (OptimizelyPlugin plugin)

Register the given plugin for activation now if Optimizely is running or for activiation once Optimizely loads

Parameters
plugin the plugin instance to register.

public static void removeOptimizelyEventListener (OptimizelyEventListener listener)

public static void resetUserBucketing (Context userContext)

Reset the user bucketing before Optimizely starts so the app can record a new session. This MUST be called before Optimizely.startWithAPIToken(...) or else it will not do anything.

Parameters
userContext Your application context.

public static String sdkVersion ()

Get the Optimizely SDK version.

Returns
  • sdk version.

public static void sendEvents ()

Manually sends events to the server. Can be used in conjunction with automaticEventSending to control when Optimizely network requests are made to send events to the backend.

public static void setAppInForeground (boolean appInForeground)

Sets whether optimizely considers itself to be in the foreground

Parameters
appInForeground Whether the app is in the foreground

public static void setAutomaticEventSending (boolean automaticEventSending)

Sets whether events will be automatically sent to the backend on activity transitions. Defaults to true.

Parameters
automaticEventSending Boolean indicating whether events should be automatically sent to the backend.

public static void setCustomTag (String key, String value)

This method allows you to add custom tags for targeting. This method should be called before startOptimizely(String, Application)

Parameters
key Key for custom tag
value Value for custom tag

public static void setDataFileDownloadInterval (long dataFileDownloadInterval)

Parameters
dataFileDownloadInterval The frequency (in milliseconds) at which the experiment data file is fetched from server. Must be called before startOptimizely.

public static void setDumpNetworkCalls (boolean dumpNetworkCalls)

Enable or disable network logging. This logging is disabled by default.

Parameters
dumpNetworkCalls set to true to enable (very) verbose network traffic dumps.

public static void setEditGestureEnabled (boolean editGestureEnabled)

Enable or disable the Optimizely edit gesture.

Parameters
editGestureEnabled whether to enable the gesture.

public void setEditorModule (EditorModule editorModule)

public static void setEnableUncaughtExceptionHandler (boolean enableUncaughtExceptionHandler)

Enable or disable Optimizely Uncaught Exception handling.

Generally, you want this enabled. If one of our background threads crash it won't crash your app. However, it can lead to weird behavior in tests so if I Optimizely is part of your automated test build you might want to disable it.

It is enabled by default.

Parameters
enableUncaughtExceptionHandler true or false flag

public static void setEventRecordingEnabled (boolean eventRecordingEnabled)

Enable or disable the Optimizely event recording.

Parameters
eventRecordingEnabled whether to enable the event recording.

public static void setHttpClient (OkHttpClient httpClient)

Set the HTTP client used by the Optimizely SDK for all network calls except for the data file downloads.

Parameters
httpClient the client to use

public static void setNetworkTimeout (int networkTimeout)

Parameters
networkTimeout The timeout (in milliseconds) for the initial download of the Optimizely config file. Defaults to 2 seconds, must be set prior to calling startOptimizely.

public void setOptimizelyData (OptimizelyData data)

Set the OptimizelyData

public void setOptimizelyEventsManager (OptimizelyEventsManager eventsManager)

public static void setOptimizelyId (String id, View view)

public void setShowFloatingButton (boolean showFloatingButton)

public void setStartState (Optimizely.OptimizelyStartState startState)

public static void setUniversalUserId (Context context, String universalUserId)

public static void setUserId (Context context, String userId)

This method is deprecated.
This method is deprecated. Use setUniversalUserId instead.

Sets a unique identifier for the current user.

Parameters
context The application's current context
userId A unique identifier for the current user.

public static boolean setValueForAttributeApiName (String attributeValue, String attributeApiName)

This method returns whether the value was successfully set for an attribute with the corresponding attributeApiName.

Parameters
attributeValue The value to set.
attributeApiName The attributeApiName to look up.
Returns
  • If this is called before Optimizely starts, it will return false. If there is no attribute found with the attributeApiName, it will return false. If the value is successfully set for an OptimizelyAttribute object with the matching attributeApiName, it will return true.

public static boolean setValueForAttributeApiName (String attributeValue, String attributeApiName, Context context)

This method returns whether the value was successfully set for an attribute. If the attribute does not exist in the datafile, it will still return true, but it will get cleared when Optimizely starts.

Parameters
attributeValue The value to set.
attributeApiName The attributeApiName to look up.
context The current Application Context.
Returns
  • If this is called before Optimizely starts, it will return true, and store the key value pairing in storage. If this is called after Optimizely starts, it will behave the same as setValueForAttributeApiName(String, String).

public static boolean setValueForAttributeId (String attributeValue, String attributeId, Context context)

This method returns whether the value was successfully set for an attribute. If the attribute does not exist in the datafile, it will still return true, but it will get cleared when Optimizely starts.

Parameters
attributeValue The value to set.
attributeId The attributeId to look up.
context The current Application Context.
Returns
  • If this is called before Optimizely starts, it will return true, and store the key value pairing in storage. If this is called after Optimizely starts, it will behave the same as setValueForAttributeId(String, String).

public static boolean setValueForAttributeId (String attributeValue, String attributeId)

This method returns whether the value was successfully set for an attribute with corresponding attributeId.

Parameters
attributeValue The value to set.
attributeId The attributeId to look up.
Returns
  • If this is called before Optimizely starts, it will return false. If there is no attribute found with the attributeId, it will return false. If value is successfully set for an OptimizelyAttribute object with the matching attributeId, it will return true.

public static void setVerboseLogging (boolean enableLogging)

Enable or disable verbose logging. This logging is disabled by default.

Parameters
enableLogging set to true to enable verbose (debug) logging.

public void setViewModule (ViewModule viewModule)

public static void setVisualExperimentsEnabled (boolean visualExperimentsEnabled)

Enable or disable the Optimizely views modification.

Parameters
visualExperimentsEnabled whether to enable the views modification.

public static void setshouldReloadExperimentsOnForegrounding (boolean shouldReloadExperimentsOnForegrounding)

Indicates whether experiments should be reloaded on foregrounding. Defaults to true.

By default, Optimizely will activate experiments at two times: the first time an app launches, and when an app is foregrounded. In practice, many app launches are actually foregrounding events as opposed to a full fresh launch. This is because apps are frequently kept in memory and sometimes do not go through a fresh launch for extended periods of time, and so experiments are activated again when foregrounding. Developers should be aware that Optimizely values may change throughout the duration of the app run and that this may have unintended consequences on statistical validity.

If you want to ensure that a user will never have an inconsistent experience as a result of experiment activation, set this value to false so that experiments are only activated when the app launches from a completely clean start. Setting this value to false means that once a view becomes visible, a variable is read, or a code block is executed, its value/appearance will not change for the duration of the app run (applicationDidFinishLaunching:withOptions: is called).

public static void startOptimizely (String apiToken, Application application)

This method is deprecated.
Use startOptimizelyWithAPIToken(String, Application) Calling this activates the Optimizely framework. If not called, the app will behave as though Optimizely wasn't included.

This method attempts to fetch the latest Optimizely experiment data with a timeout of two seconds and returns im after the data has been successfully loaded. In the case of a timeout and when no experiment data is available, the user will not be counted as a visitor to your experiment.

Parameters
apiToken The api token associated with your Optimizely project
application An application object is necessary for internal operations

public static void startOptimizelyAsync (String apiToken, Application application, OptimizelyEventListener callback)

Calling this method begins the Optimizely activation (see startOptimizely(String, android.app.Application)) but returns before the network request to download the data file is made. The network request will occur in the background and the given callback will be notified on success or failure.

Parameters
apiToken The api token associated with your Optimizely project
application An application object is necessary for internal operations
callback A runnable which will be run after Optimizely has started. If Optimizely fails to start, the runnable will not be called.

public static void startOptimizelyWithAPIToken (String apiToken, Application application, OptimizelyEventListener optimizelyEventListener)

Calling this activates the Optimizely framework. If not called, the app will behave as though Optimizely wasn't included.

This method attempts to fetch the latest Optimizely experiment data with a timeout of two seconds and returns im after the data has been successfully loaded. In the case of a timeout and when no experiment data is available, the user will not be counted as a visitor to your experiment.

Parameters
apiToken The api token associated with your Optimizely project
application An application object is necessary for internal operations

public static void startOptimizelyWithAPIToken (String apiToken, Application application)

Calling this activates the Optimizely framework. If not called, the app will behave as though Optimizely wasn't included.

This method attempts to fetch the latest Optimizely experiment data with a timeout of two seconds and returns im after the data has been successfully loaded. In the case of a timeout and when no experiment data is available, the user will not be counted as a visitor to your experiment.

Parameters
apiToken The api token associated with your Optimizely project
application An application object is necessary for internal operations

public void stopOptimizely ()

Tear down Optimizely and remove components

public static LiveVariable<String> stringForKey (String variableKey, String defaultValue, Callback<String> callback)

Method to create a OptimizelyVariable in client app with type String. This method also takes a callback to be executed when value changes.

Parameters
variableKey the name of the variable. Must be unique.
defaultValue the value that will be returned if no value is specified in the experiment.
callback the callback to be executed when value changes in editor.
Returns
  • a new LiveVariable which may be queried with get() to retrieve the value.

public static LiveVariable<String> stringForKey (String variableKey, String defaultValue)

Method to create a OptimizelyVariable in client app with type String.

Parameters
variableKey the name of the variable. Must be unique.
defaultValue the value that will be returned if no value is specified in the experiment.
Returns
  • a new LiveVariable which may be queried with get() to retrieve the value.

public static LiveVariable<String> stringVariable (String variableKey, String defaultValue)

This method is deprecated.
Use stringForKey(String, String) Method to create a OptimizelyVariable in client app with type String.

Parameters
variableKey the name of the variable. Must be unique.
defaultValue the value that will be returned if no value is specified in the experiment.
Returns
  • a new LiveVariable which may be queried with get() to retrieve the value.

public static LiveVariable<String> stringVariable (String variableKey, String defaultValue, Callback<String> callback)

This method is deprecated.
Use stringForKey(String, String, LiveVariable.Callback) Method to create a OptimizelyVariable in client app with type String. This method also takes a callback to be executed when value changes.

Parameters
variableKey the name of the variable. Must be unique.
defaultValue the value that will be returned if no value is specified in the experiment.
callback the callback to be executed when value changes in editor.
Returns
  • a new LiveVariable which may be queried with get() to retrieve the value.

public static void trackEvent (String description)

This method informs Optimizely that a custom goal with key `description` occurred.

Parameters
description A unique string identifying the custom goal

public static void trackRevenue (int revenueAmount)

This method is deprecated.
use trackRevenueWithDescription(int, String) instead.

This method informs Optimizely that a revenue goal custom event occurred.

Parameters
revenueAmount The revenue amount in cents associated with the event

public static void trackRevenueWithDescription (int revenueAmount, String revenueDescription)

This method informs Optimizely that a revenue goal custom event occurred.

Parameters
revenueAmount The revenue amount in cents associated with the event
revenueDescription A description of the revenue.

public static void trackViewGoal (String id)