HomeGuidesAPI Reference
Submit Documentation FeedbackJoin Developer CommunityLog In
Hey! These docs are for version 2.1.0-full-stack, which is no longer officially supported. Click here for the latest version, 1.0!

Instantiates an instance of the Optimizely client class that exposes API methods like Activate and Track.

Each client corresponds to the datafile representing the state of a project for a certain environment. See Review core concepts and Instantiate a client for more information.

Version

SDK v2.1

Description

The constructor accepts a configuration object to configure Optimizely.

Some parameters are optional because the SDK provides a default implementation, but you may want to override these for your production environments. For example, you may want override these to set up an error handler and logger to catch issues, an event dispatcher to manage network calls, and a User Profile Service to ensure sticky bucketing. See Customize an SDK for more background on these topics.

Parameter definitions

This section provides general information about the required and optional parameters.
For the specific parameter name in one of Optimizely's supported languages, see Parameter names.

Parameter

Type

Description

datafile
optional

string

The JSON string representing the project.

event dispatcher
optional

IEventDispatcher

An event handler to manage network calls.

logger
optional

ILogger

A logger implementation to log issues.

error handler
optional

IErrorHandler

An error handler object to handle errors.

user profile service
optional

UserProfileService

A User Profile Service.

skip JSON validation
optional

boolean

Specifies whether the JSON should be validated. Set to true to skip JSON validation on the schema, or false to perform validation.

Parameter names

This section shows the specific parameter names for each supported language.

Client constructed using Builder class and passing the following to Build():
datafile
eventhandle
datafile
eventDispatcher
logger
errorHandler
userProfileService
skipJsonValidation
Created using the Builder class. The builder constructor takes in these parameters:
datafile
eventHandler
Created by passing in a Config object. The Config class consists of these fields:
clientEngine
clientVersion
datafile
errorHandler
eventDispatcher
logger
skipJSONValidation
userProfileService
Created by passing in a Config object. The Config class consists of these fields:
clientEngine
clientVersion
datafile
errorHandler
eventDispatcher
logger
skipJSONValidation
userProfileService
datafile
errorHandler
eventDispatcher
logger
userProfileService
clientVersion
clientEngine
datafile
eventDispatcher
logger
errorHandler
skipJsonValidation
userProfileService
datafile
event_dispatcher
logger
error_handler
skip_json_validation
user_profile_service
datafile
event_dispatcher
logger
error_handler
skip_json_validation
user_profile_service
datafile
errorHandler
eventDispatcher
logger
userProfileService
clientVersion
clientEngine

Returns

Instantiates an instance of the Optimzely class.

Examples

For simple examples of how you can use the method with the Android and iOS SDKs, see Initialize a mobile SDK. For the other SDKs, see Instantiate a client.

Exceptions

SDK

Exception

Meaning

Android
Java

ConfigParseException

The datafile could not be parsed either because it is malformed or has an incorrect schema.

See also

Review core concepts
Instantiate a client

Side effects

None

Notes

Enable JSON schema validation

Skipping JSON schema validation enhances performance during instantiation. In certain SDKs, you have control over whether to validate the JSON schema of the datafile when instantiating the client. The examples show how to skip JSON schema validation.

// Skip JSON schema validation for the datafile
var optimizelyClientInstance = optimizely.createInstance({
  datafile: datafile,
  skipJSONValidation: true
});
// Skip JSON schema validation
$optimizelyClient = new Optimizely($datafile);
# Skip JSON schema validation (SDK versions 0.1.1 and above)
optimizely_client = optimizely.Optimizely(datafile,
                                          skip_json_validation=True)
# Skip JSON schema validation (SDK versions 0.1.1 and above)
optimizely_client = Optimizely::Project.new(datafile, nil, nil, nil, true)
let optimizelyClient: OPTLYClient? = optimizelyManager?.initialize(withDatafile:jsonDatafile!)

To enable JSON schema validation in JavaScript, you must pass in false for the skip JSON validation parameter and provide your own validator. See the default validator for an example.

Provide an event handler for Java

In addition to the datafile, you'll need to provide an event dispatcher (also called event handler) object as an argument to the Optimizely.builder function. Use our default event dispatcher implementation, or provide your own implementation as described in Configure the event dispatcher.

Source files

The table lists the language/platform source files containing the implementations.

Language/Platform

Source files

Android

OptimizelyClient.java

C#

Optimizely.cs

Java

Optimizely.java

JavaScript

index.js

Node

index.js

Objective-C and Swift

Optimizely.h

PHP

Optimizely.php

Python

optimizely.py

Ruby

optimizely.rb