
Research
Two Malicious Rust Crates Impersonate Popular Logger to Steal Wallet Keys
Socket uncovers malicious Rust crates impersonating fast_log to steal Solana and Ethereum wallet keys from source code.
@optimizely/optimizely-sdk
Advanced tools
JavaScript SDK for Optimizely Feature Experimentation, Optimizely Full Stack (legacy), and Optimizely Rollouts
@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.
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');
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 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.
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.
This is the official JavaScript and TypeScript SDK for use with Optimizely Feature Experimentation and Optimizely Full Stack (legacy). The SDK now features a modular architecture for greater flexibility and control. If you're upgrading from a previous version, see our Migration Guide.
Optimizely Feature Experimentation is an A/B testing and feature management tool for product development teams that enables you to experiment at every step. Using Optimizely Feature Experimentation allows for every feature on your roadmap to be an opportunity to discover hidden insights. Learn more at Optimizely.com, or see the developer documentation.
Optimizely Rollouts is free feature flags for development teams. You can easily roll out and roll back features in any application without code deploys, mitigating risk for every feature on your roadmap.
Refer to the JavaScript SDK's developer documentation for detailed instructions on getting started with using the SDK.
For Edge Functions, we provide starter kits that utilize the Optimizely JavaScript SDK for the following platforms:
Note: We recommend using the Lite entrypoint (for version < 6) / Universal entrypoint (for version >=6) of the sdk for edge platforms. These starter kits also use the Lite variant of the JavaScript SDK.
Ensure the SDK supports all of the platforms you're targeting. In particular, the SDK targets modern ES6-compliant JavaScript environments. We officially support:
npm test
to validate for yourself), but are not formally supported.In addition, other environments are likely compatible but are not formally supported including:
Once you've validated that the SDK supports the platforms you're targeting, fetch the package from NPM:
Using npm
:
npm install --save @optimizely/optimizely-sdk
Using yarn
:
yarn add @optimizely/optimizely-sdk
Using pnpm
:
pnpm add @optimizely/optimizely-sdk
Using deno
(no installation required):
import optimizely from 'npm:@optimizely/optimizely-sdk';
See the JavaScript SDK's developer documentation to learn how to set up your first JavaScript project using the SDK.
The SDK uses a modular architecture with dedicated components for project configuration, event processing, and more. The examples below demonstrate the recommended initialization pattern.
import {
createInstance,
createPollingProjectConfigManager,
createBatchEventProcessor,
createOdpManager,
} from '@optimizely/optimizely-sdk';
// 1. Configure your project config manager
const pollingConfigManager = createPollingProjectConfigManager({
sdkKey: '<YOUR_SDK_KEY>',
autoUpdate: true, // Optional: enable automatic updates
updateInterval: 300000, // Optional: update every 5 minutes (in ms)
});
// 2. Create an event processor for analytics
const batchEventProcessor = createBatchEventProcessor({
batchSize: 10, // Optional: default batch size
flushInterval: 1000, // Optional: flush interval in ms
});
// 3. Set up ODP manager for segments and audience targeting
const odpManager = createOdpManager();
// 4. Initialize the Optimizely client with the components
const optimizelyClient = createInstance({
projectConfigManager: pollingConfigManager,
eventProcessor: batchEventProcessor,
odpManager: odpManager,
});
optimizelyClient
.onReady()
.then(() => {
console.log('Optimizely client is ready');
// Your application code using Optimizely goes here
})
.catch(error => {
console.error('Error initializing Optimizely client:', error);
});
The package has different entry points for different environments. The browser entry point is an ES module, which can be used with an appropriate bundler like Webpack or Rollup. Additionally, for ease of use during initial evaluations you can include a standalone umd bundle of the SDK in your web page by fetching it from unpkg:
<script src="https://unpkg.com/@optimizely/optimizely-sdk@6/dist/optimizely.browser.umd.min.js"></script>
<!-- You can also use the unminified version if necessary -->
<script src="https://unpkg.com/@optimizely/optimizely-sdk@6/dist/optimizely.browser.umd.js"></script>
⚠️ Warning: Always include a specific version number (such as @6) when using CDN URLs like the
unpkg
example above. If you use a URL without a version, your application may automatically receive breaking changes when a new major version is released, which can lead to unexpected issues.
When evaluated, that bundle assigns the SDK's exports to window.optimizelySdk
. If you wish to use the asset locally (for example, if unpkg is down), you can find it in your local copy of the package at dist/optimizely.browser.umd.min.js. We do not recommend using this method in production settings as it introduces a third-party performance dependency.
As window.optimizelySdk
should be a global variable at this point, you can continue to use it like so:
<script>
// Extract the factory functions from the global SDK
const {
createInstance,
createPollingProjectConfigManager,
createBatchEventProcessor,
createOdpManager,
} = window.optimizelySdk;
// Initialize components
const pollingConfigManager = createPollingProjectConfigManager({
sdkKey: '<YOUR_SDK_KEY>',
autoUpdate: true,
});
const batchEventProcessor = createBatchEventProcessor();
const odpManager = createOdpManager();
// Create the Optimizely client
const optimizelyClient = createInstance({
projectConfigManager: pollingConfigManager,
eventProcessor: batchEventProcessor,
odpManager: odpManager,
});
optimizelyClient
.onReady()
.then(() => {
console.log('Optimizely client is ready');
// Start using the client here
})
.catch(error => {
console.error('Error initializing Optimizely client:', error);
});
</script>
Depending on the sdk configuration, the client instance might schedule tasks in the background. If the instance has background tasks scheduled, then the instance will not be garbage collected even though there are no more references to the instance in the code. (Basically, the background tasks will still hold references to the instance). Therefore, it's important to close it to properly clean up resources.
// Close the Optimizely client when you're done using it
optimizelyClient.close()
Using the following settings will cause background tasks to be scheduled
⚠️ Warning: Failure to close SDK instances when they're no longer needed may result in memory leaks. This is particularly important for applications that create multiple instances over time. For some environment like SSR applications, it might not be convenient to close each instance, in which case, the
disposable
option ofcreateInstance
can be used to disable all background tasks on the server side, allowing the instance to be garbage collected.
If you're updating your SDK version, please check the appropriate migration guide:
There is a mix of testing paradigms used within the JavaScript SDK which include Mocha, Chai, Karma, and Vitest, indicated by their respective *.tests.js
and *.spec.ts
filenames.
When contributing code to the SDK, aim to keep the percentage of code test coverage at the current level () or above.
To run unit tests, you can take the following steps:
npm install
to install all project dependencies.npm test
to run all test files.npm run test-vitest
to run only tests written using Vitest.npm run test-mocha
to run only tests written using Mocha.npm run test-xbrowser
to run tests in many browsers via BrowserStack.This information is relevant only if you plan on contributing to the SDK itself.
# Prerequisite: Install dependencies.
npm install
# Run unit tests.
npm test
# Run unit tests in many browsers, currently via BrowserStack.
# For this to work, the following environment variables must be set:
# - BROWSER_STACK_USERNAME
# - BROWSER_STACK_PASSWORD
npm run test-xbrowser
/.github/workflows/javascript.yml contains the definitions for BROWSER_STACK_USERNAME
and BROWSER_STACK_ACCESS_KEY
used in the GitHub Actions CI pipeline. When developing locally, you must provide your own credentials in order to run npm run test-xbrowser
. You can register for an account for free on the BrowserStack official website here.
For more information regarding contributing to the Optimizely JavaScript SDK, please read Contributing.
To access the Feature Management configuration in the Optimizely dashboard, please contact your Optimizely customer success manager.
@optimizely/optimizely-sdk
is developed and maintained by Optimizely and many contributors. If you're interested in learning more about what Optimizely Feature Experimentation can do for your company you can visit the official Optimizely Feature Experimentation product page here to learn more.
First-party code (under lib/
) is copyright Optimizely, Inc., licensed under Apache 2.0.
FAQs
JavaScript SDK for Optimizely Feature Experimentation, Optimizely Full Stack (legacy), and Optimizely Rollouts
The npm package @optimizely/optimizely-sdk receives a total of 218,768 weekly downloads. As such, @optimizely/optimizely-sdk popularity was classified as popular.
We found that @optimizely/optimizely-sdk demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Socket uncovers malicious Rust crates impersonating fast_log to steal Solana and Ethereum wallet keys from source code.
Research
A malicious package uses a QR code as steganography in an innovative technique.
Research
/Security News
Socket identified 80 fake candidates targeting engineering roles, including suspected North Korean operators, exposing the new reality of hiring as a security function.