
Product
Rust Support Now in Beta
Socket's Rust support is moving to Beta: all users can scan Cargo projects and generate SBOMs, including Cargo.toml-only crates, with Rust-aware supply chain checks.
@kameleoon/javascript-sdk
Advanced tools
Kameleoon JavaScript SDK is used to work with Kameleoon Feature Flags and Experiments using native JavaScript api. Integration of this SDK into web application is easy, and its footprint is low.
This page describes the most basic KameleoonClient
configuration, for more in-depth review of all the methods and configuration options follow Official Kameleoon Documentation
npm install @kameleoon/javascript-sdk
yarn add @kameleoon/javascript-sdk
pnpm add @kameleoon/javascript-sdk
bun install @kameleoon/javascript-sdk
import { KameleoonClient } from '@kameleoon/javascript-sdk';
const client = new KameleoonClient({ siteCode: 'my_site_code' });
// -- Wait for the client initialization
await client.initialize();
// -- Generate or obtain a visitor code
const visitorCode = KameleoonClient.getVisitorCode();
// -- Add targeting data
const customDataIndex = 0;
client.addData(visitorCode, new CustomData(customDataIndex, 'my_data'));
// -- Check if the feature flag is active
const isActive = client.isFeatureFlagActive(visitorCode, 'my_feature_key');
JavaScript SDK utilizes certain external dependencies, which are required to be able to use specific APIs.
There are several possible external dependencies used by Kameleoon JavaScript SDK, all of them have built-in implementation in SDK but can optionally be implemented by a developer using an SDK.
Here is the list of such dependencies:
visitorCodeManager
is responsible for managing the visitor code and cookieseventSource
is responsible for Real Time Updates(Streaming) for SDKstorage
is responsible for storing all SDK related dataFollowing is the example implementation for each dependency.
import {
KameleoonClient,
IExternalVisitorCodeManager,
} from '@kameleoon/javascript-sdk';
class MyVisitorCodeManager implements IExternalVisitorCodeManager {
// - Get visitor code from browser cookies
public getData(key: string): string | null {
const cookieString = document.cookie;
const cookieEntry = cookieString.split(' ;').find((keyValue) => {
const [cookieKey, cookieValue] = keyValue.split('=');
return cookieKey === key && cookieValue !== '';
});
if (cookieEntry) {
const [_, value] = cookieEntry.split('=');
return value;
}
return null;
}
// - Set visitor code back to browser cookies
public setData({
visitorCode,
domain,
maxAge,
key,
path,
}: SetDataParametersType): void {
let resultCookie = `${key}=${visitorCode}; Max-Age=${maxAge}; Path=${path}`;
if (domain) {
resultCookie += `; Domain=${domain}`;
}
document.cookie = resultCookie;
}
}
const client = new KameleoonClient({
siteCode: "my_site_code",
externals: {
visitorCodeManager: new MyVisitorCodeManager();
}
});
import { KameleoonClient, IExternalEventSource } from '@kameleoon/javascript-sdk';
class MyEventSource implements IExternalEventSource {
private eventSource?: EventSource;
public open({
eventType,
onEvent,
url,
}: EventSourceOpenParametersType): void {
// - Use any suitable EventSource implementation here
const eventSource = new EventSource(url);
this.eventSource = eventSource;
this.eventSource.addEventListener(eventType, onEvent);
}
public close(): void {
if (this.eventSource) {
this.eventSource.close();
}
}
}
const client = new KameleoonClient({
siteCode: "my_site_code",
externals: {
eventSource: new MyEventSource();
}
});
import { KameleoonClient, IExternalStorage } from '@kameleoon/javascript-sdk';
const storage = new Map();
class MyStorage<T> implements IExternalStorage<T> {
public read(key: string): T {
// - Utilize the storage implementation of your choice
const data = storage.get(key);
// - Optionally handle errors
if (!data) {
throw new Error("Couldn't read data from myStorage");
}
return data;
}
public write(key: string, data: T): void {
storage.set(key, data);
}
}
const client = new KameleoonClient({
siteCode: "my_site_code",
externals: {
storage: new MyStorage();
}
});
FAQs
Kameleoon JavaScript SDK
The npm package @kameleoon/javascript-sdk receives a total of 3,939 weekly downloads. As such, @kameleoon/javascript-sdk popularity was classified as popular.
We found that @kameleoon/javascript-sdk demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 3 open source maintainers 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.
Product
Socket's Rust support is moving to Beta: all users can scan Cargo projects and generate SBOMs, including Cargo.toml-only crates, with Rust-aware supply chain checks.
Product
Socket Fix 2.0 brings targeted CVE remediation, smarter upgrade planning, and broader ecosystem support to help developers get to zero alerts.
Security News
Socket CEO Feross Aboukhadijeh joins Risky Business Weekly to unpack recent npm phishing attacks, their limited impact, and the risks if attackers get smarter.