What is @optimizely/optimizely-sdk?
@optimizely/optimizely-sdk is a feature flagging and A/B testing library that allows developers to experiment with and optimize their applications. It provides tools for running experiments, managing feature flags, and personalizing user experiences.
What are @optimizely/optimizely-sdk's main functionalities?
Feature Flagging
This feature allows you to manage feature flags, enabling or disabling features for specific users or groups of users.
const optimizely = require('@optimizely/optimizely-sdk');
const optimizelyClient = optimizely.createInstance({
sdkKey: 'your-sdk-key'
});
const userId = 'user123';
const featureEnabled = optimizelyClient.isFeatureEnabled('new_feature', userId);
if (featureEnabled) {
console.log('Feature is enabled for this user');
} else {
console.log('Feature is not enabled for this user');
}
A/B Testing
This feature allows you to run A/B tests by assigning users to different variations of an experiment and measuring their behavior.
const optimizely = require('@optimizely/optimizely-sdk');
const optimizelyClient = optimizely.createInstance({
sdkKey: 'your-sdk-key'
});
const userId = 'user123';
const variation = optimizelyClient.activate('experiment_key', userId);
if (variation === 'variation_1') {
console.log('User is in variation 1');
} else if (variation === 'variation_2') {
console.log('User is in variation 2');
} else {
console.log('User is in control group');
}
Event Tracking
This feature allows you to track events and user actions, which can be used to measure the impact of experiments and feature flags.
const optimizely = require('@optimizely/optimizely-sdk');
const optimizelyClient = optimizely.createInstance({
sdkKey: 'your-sdk-key'
});
const userId = 'user123';
optimizelyClient.track('event_key', userId, { revenue: 100 });
console.log('Event tracked for user');
Other packages similar to @optimizely/optimizely-sdk
launchdarkly-node-server-sdk
LaunchDarkly is a feature management platform that provides similar functionality to Optimizely, including feature flagging, A/B testing, and user segmentation. It is known for its robust feature set and ease of use.
unleash-client
Unleash is an open-source feature management solution that offers feature toggling and gradual rollouts. It is a good alternative for those looking for a self-hosted solution with a strong community support.
splitio
Split.io is a feature experimentation platform that provides feature flagging, A/B testing, and real-time analytics. It is designed for teams looking to make data-driven decisions and optimize their applications.
Optimizely JavaScript SDK
This repository houses the JavaScript SDK for Optimizely X Full Stack.
Getting Started
Installing the SDK
The SDK is available through npm. To install:
npm install @optimizely/optimizely-sdk --save
Or to use in a non CommonJS fashion in the Browser:
- Run
npm run build
- Pull in
dist/optimizely.browser.umd.min.js
as a <script>
- Use as global variable
window.optimizelyClient
Migrating from 1.x.x
This version represents a major version change and, as such, introduces some breaking changes:
-
The Node SDK is now combined with the JavaScript SDK so that we have one optimizely-sdk
package that works across both server + browser environments.
-
We no longer support legacy Node versions (under 4.0).
-
You will no longer be able to pass in revenue
value as a stand-alone argument to the track
call. Instead you will need to pass it as an entry in the eventTags
.
Feature Management Access
To access the Feature Management configuration in the Optimizely dashboard, please contact your Optimizely account executive.
Using the SDK
See the Optimizely X Full Stack testing developer documentation to learn how to set up your first JavaScript project and use the SDK.
Development
Installing dependencies
npm install
Unit tests
You can run all unit tests with:
npm test
Build distribution packages
npm run build
This command will build several distribution bundles under the dist
directory:
- optimizely.browser.cjs.js - This is the main entry point for browser/client-side bundles
- optimizely.browser.umd.js - This is used when not packaging the optimizely-sdk with your own JS bundles. Instead you would load this script as a
<script>
tag and reference it via the global var optimizelyClient
- optimizely.node.js - This is the main entry point for Node apps
The browser bundles also come with a minified / production-ready version.
Environment Variables
The .yml of this project contains environment vairables for BROWSER_STACK_USERNAME
and BROWSER_STACK_ACCESS_KEY
.
These variables, created in BrowserStack, are encrypted by the TravisCI public key. This is done directly with the TravisCI command line tools; for additional information see travis encrypt-file.