Install the JavaScript SDK v6+
The JavaScript SDK can be used in Node, browser, React Native, and other platforms (using the universal bundle) and is distributed through npm.
Minimum SDK version
v6.0.0+
For versions 5.3.5 and below, see JavaScript (Browser) SDK or JavaScript (Node) SDK.
See the SDK compatibility matrix documentation for a list of current SDK releases and the features they support.
Install
Using npm.
npm install --save @optimizely/optimizely-sdkUsing yarn.
yarn add @optimizely/optimizely-sdkUsing pnpm.
pnpm add @optimizely/optimizely-sdkUsing deno (no installation required).
import optimizely from "npm:@optimizely/optimizely-sdk"Installation on React Native
React Native apps need extra peer dependencies for datafile caching, offline event persistence, and two missing web APIs. After you install the JavaScript SDK, install these packages in your React Native app:
npm install --save \
@react-native-async-storage/async-storage \
@react-native-community/netinfo \
react-native-get-random-values \
fast-text-encoding- @react-native-async-storage/async-storage – Used to enable datafile caching by storing the datafile locally on the user's device. It also enables offline event persistence by temporarily storing events while the device is offline. However, using this package is optional, you can provide your own custom cache implementation instead.
- @react-native-community/netinfo – Required to detect changes in internet connectivity. Ensures that any events triggered while offline are retried when connectivity is restored.
- react-native-get-random-values – Provides the crypto.getRandomValues() API, which React Native does not include. Skip this package if your app loads another crypto.getRandomValues() polyfill, such as react-native-quick-crypto, in its entry file before it imports the SDK.
- fast-text-encoding – Provides the TextEncoder API that user bucketing uses. Skip this package if your JavaScript engine provides TextEncoder, as Hermes does in React Native 0.74 and later. Also skip it if your app loads another TextEncoder polyfill before it imports the SDK.
The SDK loads each of these packages only if your app depends on it. If TextEncoder or crypto.getRandomValues() is still missing, importing the SDK throws an error that names the package to install.
The SDK declares the following supported version ranges for these peer dependencies. Install versions within these ranges to avoid peer dependency conflicts:
- @react-native-async-storage/async-storage – 1.x, 2.x, or 3.x
- @react-native-community/netinfo – 5.x through 12.x
- react-native-get-random-values – 1.11.0 or later, through 2.x
- fast-text-encoding – 1.0.6 or later 1.x releases
If you prefer to use a different persistent caching solution instead of @react-native-async-storage/async-storage, you can pass your own implementation that conforms to the Store<string> interface in the following cases:
- Polling config manager – If you are using the polling project config manager, provide your custom cache implementation in the config.
- Batch event processor – If you are using the batch event processor, provide your custom eventStore in the config.
By doing this, you can avoid installing @react-native-async-storage/async-storage and instead use your own implementation to handle caching and event storage.
The full JavaScript SDK source code is available on GitHub.
Next
After installing, you must initialize the SDK.