Skip to main content

Javascript SDK Getting Started

Npm package version GitHub

  • If the JS SDK is installed using NPM, call initializeDevCycle with your client key, a user object, and an optional options object.
  • Otherwise, If you’re using the CDN to install the JS SDK, call DevCycle.initializeDevCycle with your client key, a user object, and an optional options object.

The user object needs either a user_id, or isAnonymous set to true for an anonymous user. The options object is optional, but can passed a logWriter for a custom logging solution and a logLevel, which must be one of info, debug, warn or error. The default options are to set the logWriter to be the console and the logLevel to error.

const user = { user_id: 'my_user' }
const dvcOptions = { logLevel: 'debug' }
// replace initializeDevCycle with DevCycle.initializeDevCycle if using the CDN
const devcycleClient = initializeDevCycle(
'<DEVCYCLE_CLIENT_SDK_KEY>',
user,
dvcOptions,
)

Deferred Initialization

In many cases, user data is not available at the time of initialization. If the intialization call is made without a user object, then by default the SDK will be instantiated with an "anonymous" user and a configuration will be downloaded from DevCycle. The SDK makes a call to get the configuration for anonymous user and initializes.

If you would like to defer initialization of the SDK until your user data is available, you can pass the deferInitialization option to the intializeDevCycle method. This will cause the SDK to not fetch a configuration until the devcycleClient.identifyUser method is called with the user data. Until that config is retrieved, all calls to retrieve variable values will return their default values.

const dvcOptions = { logLevel: 'debug', deferInitialization: true }
// replace initializeDevCycle with DevCycle.initializeDevCycle if using the CDN
const devcycleClient = initializeDevCycle(
'<DEVCYCLE_CLIENT_SDK_KEY>',
dvcOptions,
)

DevCycleUser Object

DevCycleUser Typescript Schema

PropertyTypeDescription
isAnonymousBooleanBoolean to indicate if the user is anonymous
user_idStringUnique user ID
emailStringUser's email
nameStringUser's name
languageStringUser's language
countryStringUser's country
appVersionStringApp version
appBuildNumberApp build
customDataDVCJSONKey/value map of properties to be used for targeting
privateCustomDataDVCJSONKey/value map of properties to be used for targeting. Private properties will not be included in event logging.

Initialization Options

The SDK exposes various initialization options which can be set on the initialization() method:

DevCycleOptions Typescript Schema

DevCycle OptionTypeDescription
enableEdgeDBBooleanEnables the usage of EdgeDB for DevCycle that syncs User Data to DevCycle.
loggerDVCLoggerLogger override to replace default logger
logLevelDVCDefaultLogLevelSet log level of the default logger. Options are: debug, info, warn, error. Defaults to info.
eventFlushIntervalMSNumberControls the interval between flushing events to the DevCycle servers in milliseconds, defaults to 10 seconds.
flushEventQueueSizeNumberControls the maximum size the event queue can grow to until a flush is forced. Defaults to 100.
maxEventQueueSizeNumberControls the maximum size the event queue can grow to until events are dropped. Defaults to 1000.
apiProxyURLStringAllows the SDK to communicate with a proxy of DevCycle bucketing API / client SDK API.
configCacheTTLNumberThe maximum allowed age of a cached config in milliseconds, defaults to 7 days
disableConfigCacheBooleanDisable the use of cached configs
disableRealtimeUpdatesBooleanDisable Realtime Updates
deferInitializationBooleanDefer initialization (fetching configuration from DevCycle) until user is identified with identifyUser call
disableAutomaticEventLoggingBooleanDisables logging of sdk generated events (e.g. variableEvaluated, variableDefaulted) to DevCycle.
disableCustomEventLoggingBooleanDisables logging of custom events, from track() method, and user data to DevCycle.