@ThreadSafe
public class Optimizely
extends java.lang.Object
implements java.lang.AutoCloseable
Optimizely optimizely = Optimizely.builder(projectWatcher, eventHandler).build();
To activate an experiment and perform variation specific processing:
Variation variation = optimizely.activate(experimentKey, userId, attributes);
if (variation.is("ALGORITHM_A")) {
// execute code for algorithm A
} else if (variation.is("ALGORITHM_B")) {
// execute code for algorithm B
} else {
// execute code for default algorithm
}
NOTE: by default, all exceptions originating from Optimizely calls are suppressed.
For example, attempting to activate an experiment that does not exist in the project config will cause an error
to be logged, and for the "control" variation to be returned.| Modifier and Type | Class and Description |
|---|---|
static class |
Optimizely.Builder
Optimizely instance builder. |
| Modifier and Type | Field and Description |
|---|---|
java.util.List<OptimizelyDecideOption> |
defaultDecideOptions |
NotificationCenter |
notificationCenter |
| Modifier and Type | Method and Description |
|---|---|
Variation |
activate(Experiment experiment,
java.lang.String userId) |
Variation |
activate(Experiment experiment,
java.lang.String userId,
java.util.Map<java.lang.String,?> attributes) |
Variation |
activate(java.lang.String experimentKey,
java.lang.String userId) |
Variation |
activate(java.lang.String experimentKey,
java.lang.String userId,
java.util.Map<java.lang.String,?> attributes) |
int |
addDecisionNotificationHandler(NotificationHandler<DecisionNotification> handler)
Convenience method for adding DecisionNotification Handlers
|
int |
addLogEventNotificationHandler(NotificationHandler<LogEvent> handler)
Convenience method for adding LogEvent Notification Handlers
|
<T> int |
addNotificationHandler(java.lang.Class<T> clazz,
NotificationHandler<T> handler)
Convenience method for adding NotificationHandlers
|
int |
addTrackNotificationHandler(NotificationHandler<TrackNotification> handler)
Convenience method for adding TrackNotification Handlers
|
int |
addUpdateConfigNotificationHandler(NotificationHandler<UpdateConfigNotification> handler)
Convenience method for adding UpdateConfigNotification Handlers
|
static Optimizely.Builder |
builder() |
static Optimizely.Builder |
builder(java.lang.String datafile,
EventHandler eventHandler)
Deprecated.
|
void |
close()
Checks if eventHandler
EventHandler and projectConfigManager ProjectConfigManager
are Closeable Closeable and calls close on them. |
OptimizelyUserContext |
createUserContext(java.lang.String userId) |
OptimizelyUserContext |
createUserContext(java.lang.String userId,
java.util.Map<java.lang.String,?> attributes)
Create a context of the user for which decision APIs will be called.
|
java.util.List<java.lang.String> |
fetchQualifiedSegments(java.lang.String userId,
java.util.List<ODPSegmentOption> segmentOptions) |
void |
fetchQualifiedSegments(java.lang.String userId,
ODPSegmentManager.ODPSegmentFetchCallback callback,
java.util.List<ODPSegmentOption> segmentOptions) |
OptimizelyJSON |
getAllFeatureVariables(java.lang.String featureKey,
java.lang.String userId)
Get the values of all variables in the feature.
|
OptimizelyJSON |
getAllFeatureVariables(java.lang.String featureKey,
java.lang.String userId,
java.util.Map<java.lang.String,?> attributes)
Get the values of all variables in the feature.
|
java.util.List<java.lang.String> |
getEnabledFeatures(java.lang.String userId,
java.util.Map<java.lang.String,?> attributes)
Get the list of features that are enabled for the user.
|
java.lang.Boolean |
getFeatureVariableBoolean(java.lang.String featureKey,
java.lang.String variableKey,
java.lang.String userId)
Get the Boolean value of the specified variable in the feature.
|
java.lang.Boolean |
getFeatureVariableBoolean(java.lang.String featureKey,
java.lang.String variableKey,
java.lang.String userId,
java.util.Map<java.lang.String,?> attributes)
Get the Boolean value of the specified variable in the feature.
|
java.lang.Double |
getFeatureVariableDouble(java.lang.String featureKey,
java.lang.String variableKey,
java.lang.String userId)
Get the Double value of the specified variable in the feature.
|
java.lang.Double |
getFeatureVariableDouble(java.lang.String featureKey,
java.lang.String variableKey,
java.lang.String userId,
java.util.Map<java.lang.String,?> attributes)
Get the Double value of the specified variable in the feature.
|
java.lang.Integer |
getFeatureVariableInteger(java.lang.String featureKey,
java.lang.String variableKey,
java.lang.String userId)
Get the Integer value of the specified variable in the feature.
|
java.lang.Integer |
getFeatureVariableInteger(java.lang.String featureKey,
java.lang.String variableKey,
java.lang.String userId,
java.util.Map<java.lang.String,?> attributes)
Get the Integer value of the specified variable in the feature.
|
OptimizelyJSON |
getFeatureVariableJSON(java.lang.String featureKey,
java.lang.String variableKey,
java.lang.String userId)
Get the JSON value of the specified variable in the feature.
|
OptimizelyJSON |
getFeatureVariableJSON(java.lang.String featureKey,
java.lang.String variableKey,
java.lang.String userId,
java.util.Map<java.lang.String,?> attributes)
Get the JSON value of the specified variable in the feature.
|
java.lang.String |
getFeatureVariableString(java.lang.String featureKey,
java.lang.String variableKey,
java.lang.String userId)
Get the String value of the specified variable in the feature.
|
java.lang.String |
getFeatureVariableString(java.lang.String featureKey,
java.lang.String variableKey,
java.lang.String userId,
java.util.Map<java.lang.String,?> attributes)
Get the String value of the specified variable in the feature.
|
Variation |
getForcedVariation(java.lang.String experimentKey,
java.lang.String userId)
Gets the forced variation for a given user and experiment.
|
NotificationCenter |
getNotificationCenter() |
ODPManager |
getODPManager() |
OptimizelyConfig |
getOptimizelyConfig()
Get
OptimizelyConfig containing experiments and features map |
ProjectConfig |
getProjectConfig() |
UserProfileService |
getUserProfileService() |
Variation |
getVariation(Experiment experiment,
java.lang.String userId) |
Variation |
getVariation(Experiment experiment,
java.lang.String userId,
java.util.Map<java.lang.String,?> attributes) |
Variation |
getVariation(java.lang.String experimentKey,
java.lang.String userId) |
Variation |
getVariation(java.lang.String experimentKey,
java.lang.String userId,
java.util.Map<java.lang.String,?> attributes) |
void |
identifyUser(java.lang.String userId) |
java.lang.Boolean |
isFeatureEnabled(java.lang.String featureKey,
java.lang.String userId)
Determine whether a boolean feature is enabled.
|
java.lang.Boolean |
isFeatureEnabled(java.lang.String featureKey,
java.lang.String userId,
java.util.Map<java.lang.String,?> attributes)
Determine whether a boolean feature is enabled.
|
boolean |
isValid()
Determine if the instance of the Optimizely client is valid.
|
void |
sendODPEvent(java.lang.String type,
java.lang.String action,
java.util.Map<java.lang.String,java.lang.String> identifiers,
java.util.Map<java.lang.String,java.lang.Object> data)
Send an event to the ODP server.
|
boolean |
setForcedVariation(java.lang.String experimentKey,
java.lang.String userId,
java.lang.String variationKey)
Force a user into a variation for a given experiment.
|
void |
track(java.lang.String eventName,
java.lang.String userId) |
void |
track(java.lang.String eventName,
java.lang.String userId,
java.util.Map<java.lang.String,?> attributes) |
void |
track(java.lang.String eventName,
java.lang.String userId,
java.util.Map<java.lang.String,?> attributes,
java.util.Map<java.lang.String,?> eventTags) |
public final java.util.List<OptimizelyDecideOption> defaultDecideOptions
public final NotificationCenter notificationCenter
public boolean isValid()
public void close()
EventHandler and projectConfigManager ProjectConfigManager
are Closeable Closeable and calls close on them.
NOTE: There is a chance that this could be long running if the implementations of close are long running.close in interface java.lang.AutoCloseable@Nullable public Variation activate(@Nonnull java.lang.String experimentKey, @Nonnull java.lang.String userId) throws UnknownExperimentException
UnknownExperimentException@Nullable public Variation activate(@Nonnull java.lang.String experimentKey, @Nonnull java.lang.String userId, @Nonnull java.util.Map<java.lang.String,?> attributes) throws UnknownExperimentException
UnknownExperimentException@Nullable public Variation activate(@Nonnull Experiment experiment, @Nonnull java.lang.String userId)
@Nullable public Variation activate(@Nonnull Experiment experiment, @Nonnull java.lang.String userId, @Nonnull java.util.Map<java.lang.String,?> attributes)
public void track(@Nonnull
java.lang.String eventName,
@Nonnull
java.lang.String userId)
throws UnknownEventTypeException
UnknownEventTypeExceptionpublic void track(@Nonnull
java.lang.String eventName,
@Nonnull
java.lang.String userId,
@Nonnull
java.util.Map<java.lang.String,?> attributes)
throws UnknownEventTypeException
UnknownEventTypeExceptionpublic void track(@Nonnull
java.lang.String eventName,
@Nonnull
java.lang.String userId,
@Nonnull
java.util.Map<java.lang.String,?> attributes,
@Nonnull
java.util.Map<java.lang.String,?> eventTags)
throws UnknownEventTypeException
UnknownEventTypeException@Nonnull
public java.lang.Boolean isFeatureEnabled(@Nonnull
java.lang.String featureKey,
@Nonnull
java.lang.String userId)
featureKey - The unique key of the feature.userId - The ID of the user.@Nonnull
public java.lang.Boolean isFeatureEnabled(@Nonnull
java.lang.String featureKey,
@Nonnull
java.lang.String userId,
@Nonnull
java.util.Map<java.lang.String,?> attributes)
featureKey - The unique key of the feature.userId - The ID of the user.attributes - The user's attributes.@Nullable
public java.lang.Boolean getFeatureVariableBoolean(@Nonnull
java.lang.String featureKey,
@Nonnull
java.lang.String variableKey,
@Nonnull
java.lang.String userId)
featureKey - The unique key of the feature.variableKey - The unique key of the variable.userId - The ID of the user.@Nullable
public java.lang.Boolean getFeatureVariableBoolean(@Nonnull
java.lang.String featureKey,
@Nonnull
java.lang.String variableKey,
@Nonnull
java.lang.String userId,
@Nonnull
java.util.Map<java.lang.String,?> attributes)
featureKey - The unique key of the feature.variableKey - The unique key of the variable.userId - The ID of the user.attributes - The user's attributes.@Nullable
public java.lang.Double getFeatureVariableDouble(@Nonnull
java.lang.String featureKey,
@Nonnull
java.lang.String variableKey,
@Nonnull
java.lang.String userId)
featureKey - The unique key of the feature.variableKey - The unique key of the variable.userId - The ID of the user.@Nullable
public java.lang.Double getFeatureVariableDouble(@Nonnull
java.lang.String featureKey,
@Nonnull
java.lang.String variableKey,
@Nonnull
java.lang.String userId,
@Nonnull
java.util.Map<java.lang.String,?> attributes)
featureKey - The unique key of the feature.variableKey - The unique key of the variable.userId - The ID of the user.attributes - The user's attributes.@Nullable
public java.lang.Integer getFeatureVariableInteger(@Nonnull
java.lang.String featureKey,
@Nonnull
java.lang.String variableKey,
@Nonnull
java.lang.String userId)
featureKey - The unique key of the feature.variableKey - The unique key of the variable.userId - The ID of the user.@Nullable
public java.lang.Integer getFeatureVariableInteger(@Nonnull
java.lang.String featureKey,
@Nonnull
java.lang.String variableKey,
@Nonnull
java.lang.String userId,
@Nonnull
java.util.Map<java.lang.String,?> attributes)
featureKey - The unique key of the feature.variableKey - The unique key of the variable.userId - The ID of the user.attributes - The user's attributes.@Nullable
public java.lang.String getFeatureVariableString(@Nonnull
java.lang.String featureKey,
@Nonnull
java.lang.String variableKey,
@Nonnull
java.lang.String userId)
featureKey - The unique key of the feature.variableKey - The unique key of the variable.userId - The ID of the user.@Nullable
public java.lang.String getFeatureVariableString(@Nonnull
java.lang.String featureKey,
@Nonnull
java.lang.String variableKey,
@Nonnull
java.lang.String userId,
@Nonnull
java.util.Map<java.lang.String,?> attributes)
featureKey - The unique key of the feature.variableKey - The unique key of the variable.userId - The ID of the user.attributes - The user's attributes.@Nullable public OptimizelyJSON getFeatureVariableJSON(@Nonnull java.lang.String featureKey, @Nonnull java.lang.String variableKey, @Nonnull java.lang.String userId)
featureKey - The unique key of the feature.variableKey - The unique key of the variable.userId - The ID of the user.@Nullable public OptimizelyJSON getFeatureVariableJSON(@Nonnull java.lang.String featureKey, @Nonnull java.lang.String variableKey, @Nonnull java.lang.String userId, @Nonnull java.util.Map<java.lang.String,?> attributes)
featureKey - The unique key of the feature.variableKey - The unique key of the variable.userId - The ID of the user.attributes - The user's attributes.@Nullable public OptimizelyJSON getAllFeatureVariables(@Nonnull java.lang.String featureKey, @Nonnull java.lang.String userId)
featureKey - The unique key of the feature.userId - The ID of the user.@Nullable public OptimizelyJSON getAllFeatureVariables(@Nonnull java.lang.String featureKey, @Nonnull java.lang.String userId, @Nonnull java.util.Map<java.lang.String,?> attributes)
featureKey - The unique key of the feature.userId - The ID of the user.attributes - The user's attributes.public java.util.List<java.lang.String> getEnabledFeatures(@Nonnull
java.lang.String userId,
@Nonnull
java.util.Map<java.lang.String,?> attributes)
userId - The ID of the user.attributes - The user's attributes.@Nullable public Variation getVariation(@Nonnull Experiment experiment, @Nonnull java.lang.String userId) throws UnknownExperimentException
UnknownExperimentException@Nullable public Variation getVariation(@Nonnull Experiment experiment, @Nonnull java.lang.String userId, @Nonnull java.util.Map<java.lang.String,?> attributes) throws UnknownExperimentException
UnknownExperimentException@Nullable public Variation getVariation(@Nonnull java.lang.String experimentKey, @Nonnull java.lang.String userId) throws UnknownExperimentException
UnknownExperimentException@Nullable public Variation getVariation(@Nonnull java.lang.String experimentKey, @Nonnull java.lang.String userId, @Nonnull java.util.Map<java.lang.String,?> attributes)
public boolean setForcedVariation(@Nonnull
java.lang.String experimentKey,
@Nonnull
java.lang.String userId,
@Nullable
java.lang.String variationKey)
experimentKey - The key for the experiment.userId - The user ID to be used for bucketing.variationKey - The variation key to force the user into. If the variation key is null
then the forcedVariation for that experiment is removed.@Nullable public Variation getForcedVariation(@Nonnull java.lang.String experimentKey, @Nonnull java.lang.String userId)
DecisionService.getForcedVariation(Experiment, String)
method of the same signature.experimentKey - The key for the experiment.userId - The user ID to be used for bucketing.@Nullable public ProjectConfig getProjectConfig()
ProjectConfig instance.@Nullable public UserProfileService getUserProfileService()
public OptimizelyConfig getOptimizelyConfig()
OptimizelyConfig containing experiments and features mapOptimizelyConfigpublic OptimizelyUserContext createUserContext(@Nonnull java.lang.String userId, @Nonnull java.util.Map<java.lang.String,?> attributes)
userId - The user ID to be used for bucketing.attributes: - A map of attribute names to current user attribute values.public OptimizelyUserContext createUserContext(@Nonnull java.lang.String userId)
public NotificationCenter getNotificationCenter()
public int addDecisionNotificationHandler(NotificationHandler<DecisionNotification> handler)
handler - DicisionNotification handlerpublic int addTrackNotificationHandler(NotificationHandler<TrackNotification> handler)
handler - TrackNotification handlerpublic int addUpdateConfigNotificationHandler(NotificationHandler<UpdateConfigNotification> handler)
handler - UpdateConfigNotification handlerpublic int addLogEventNotificationHandler(NotificationHandler<LogEvent> handler)
handler - NotificationHandler handlerpublic <T> int addNotificationHandler(java.lang.Class<T> clazz,
NotificationHandler<T> handler)
T - This is the type parameterclazz - The class of NotificationHandlerhandler - NotificationHandler handlerpublic java.util.List<java.lang.String> fetchQualifiedSegments(java.lang.String userId,
@Nonnull
java.util.List<ODPSegmentOption> segmentOptions)
public void fetchQualifiedSegments(java.lang.String userId,
ODPSegmentManager.ODPSegmentFetchCallback callback,
java.util.List<ODPSegmentOption> segmentOptions)
@Nullable public ODPManager getODPManager()
public void sendODPEvent(@Nullable
java.lang.String type,
@Nonnull
java.lang.String action,
@Nullable
java.util.Map<java.lang.String,java.lang.String> identifiers,
@Nullable
java.util.Map<java.lang.String,java.lang.Object> data)
type - the event type (default = "fullstack").action - the event action name.identifiers - a dictionary for identifiers. The caller must provide at least one key-value pair unless non-empty common identifiers have been set already with ODPManager.Builder#withUserCommonIdentifiers(Map) .data - a dictionary for associated data. The default event data will be added to this data before sending to the ODP server.public void identifyUser(@Nonnull
java.lang.String userId)
@Deprecated public static Optimizely.Builder builder(@Nonnull java.lang.String datafile, @Nonnull EventHandler eventHandler)
builder() along with
Optimizely.Builder.withDatafile(java.lang.String) and
Optimizely.Builder.withEventHandler(com.optimizely.ab.event.EventHandler)
respectively.
Example:
Optimizely optimizely = Optimizely.builder()
.withDatafile(datafile)
.withEventHandler(eventHandler)
.build();
datafile - A datafileeventHandler - An EventHandlerpublic static Optimizely.Builder builder()