NodeJS SDK
You can run experiments and activate feature flags with the NodeJS SDK. Integrating our SDK into your server is easy, and its footprint (memory and network usage) is low. Our Node SDK supports Node version 16+ with the option to downgrade it to version 14 and 12 using compatibility mode.
Getting started: For help getting started, see the developer guide.
SDK methods: For the full reference documentation of the NodeJS SDK, see the reference section.
Changelog: Latest version of the NodeJS SDK: 5.2.3 Changelog.
Before you begin installing our NodeJS SDK, we recommend reading our technical considerations article to understand the technological concepts behind our SDKs. This article will help ensure a successful integration.
Developer Guide
Follow this section to install and configure the SDK and to learn about advanced features.
Get started
Installation
Use the Kameleoon SDK Installation tool to install the SDK. The SDK Installer helps you install the SDK of your choice, generate a basic code sample, and configure external dependencies if needed.
To use the SDK Installation tool, install and run it globally:
npm install --global @kameleoon/sdk-installer
kameleoon-sdk
Or run it directly with npx
:
npx @kameleoon/sdk-installer
When using Deno, provide dependencies manually in deno.json
:
{
"imports": {
"@kameleoon/nodejs-sdk": "npm:@kameleoon/nodejs-sdk@^4.0",
// -- Optional dependencies, can be implemented manually
"@kameleoon/nodejs-requester": "@kameleoon/nodejs-requester@^1.0"
"@kameleoon/nodejs-event-source": "@kameleoon/nodejs-event-source@^1.0"
"@kameleoon/deno-visitor-code-manager": ":@kameleoon/deno-visitor-code-manager@^1.0",
}
}
Initializing the Kameleoon Client
Developers must make an entry point for the NodeJS SDK by creating a new instance of Kameleoon Client.
Use KameleoonClient
to run feature experiments and retrieve the status of feature flags and their variations.
KameleoonClient
initializes asynchronously to confirm the Kameleoon API call is successful, using the initialize()
method. Use async/await
, Promise.then()
or any other method to handle asynchronous client initialization.
To add NodeJS SDK to an Edge environment, please refer to this section.
- TypeScript
- JavaScript
import {
Environment,
KameleoonClient,
SDKConfigurationType,
CredentialsType,
} from '@kameleoon/nodejs-sdk';
import { KameleoonVisitorCodeManager } from '@kameleoon/nodejs-visitor-code-manager';
import { KameleoonEventSource } from '@kameleoon/nodejs-event-source';
// -- Mandatory credentials
const credentials: CredentialsType = {
clientId: 'my_client_id',
clientSecret: 'my_client_secret',
};
// -- Optional configuration
const configuration: Partial<SDKConfigurationType> = {
updateInterval: 20,
environment: Environment.Production,
cookieDomain: '.example.com',
};
const client = new KameleoonClient({
siteCode: 'my_site_code',
credentials,
configuration,
externals: {
visitorCodeManager: new KameleoonVisitorCodeManager(),
eventSource: new KameleoonEventSource(),
},
});
// -- Waiting for the client initialization using `async/await`
async function init(): Promise<void> {
await client.initialize();
}
init();
// -- Waiting for the client initialization using `Promise.then()`
client
.initialize()
.then(() => {})
.catch((error) => {});
import { Environment, KameleoonClient } from '@kameleoon/nodejs-sdk';
import { KameleoonVisitorCodeManager } from '@kameleoon/nodejs-visitor-code-manager';
import { KameleoonEventSource } from '@kameleoon/nodejs-event-source';
// -- Mandatory credentials
const credentials = {
clientId: 'my_client_id',
clientSecret: 'my_client_secret',
};
// -- Optional configuration
const configuration = {
updateInterval: 20,
environment: Environment.Production,
cookieDomain: '.example.com',
};
const client = new KameleoonClient({
siteCode: 'my_site_code',
credentials,
configuration,
externals: {
visitorCodeManager: new KameleoonVisitorCodeManager(),
eventSource: new KameleoonEventSource(),
},
});
// -- Waiting for the client initialization using `async/await`
async function init() {
await client.initialize();
}
init();
// -- Waiting for the client initialization using `Promise.then()`
client
.initialize()
.then(() => {})
.catch((error) => {});
Arguments
Name | Type | Description |
---|---|---|
siteCode (required) | string | This is the unique key of the Kameleoon project you are using with the SDK. This field is mandatory. |
credentials (required) | CredentialsType | client API credentials, see credentials flow for more information |
externals (required) | ExternalsType | external implementation of SDK dependencies (External dependencies) |
configuration (optional) | Partial<SDKConfigurationType> | client's configuration |
compatibility (optional) | Compatibility | SDK's compatibility mode, see Compatibility Mode |
integrations (optional) | IntegrationType | compute edge integrations, see Integration with Edge providers |
Throws
Type | Description |
---|---|
KameleoonException.Credentials | Client credentials were not provided or are empty |
Configuration Parameters
- SDK Version 4
- SDK Version 5
Name | Type | Description | Default Value |
---|---|---|---|
updateInterval (optional) | number | Specifies the refresh interval, in minutes, that the SDK fetches the configuration for the active experiments and feature flags. The value determines the maximum time it takes to propagate changes, such as activating or deactivating feature flags or launching experiments, to your production servers. If left unspecified, the default interval is set to 60 minutes. Additionally, we offer a streaming mode that uses server-sent events (SSE) to push new configurations to the SDK automatically and apply new configurations in real-time, without any delays. | 60 |
environment (optional) | Environment | feature flag environment | Environment.Production |
targetingDataCleanupInterval (optional) | number | interval in minutes for cleaning up targeting data, minimum value is 1 minute | 30 |
domain (optional) | string | domain to which the cookie belongs. Deprecated, use cookieDomain instead | undefined |
cookieDomain (optional) | string | domain to which the cookie belongs. | undefined |
networkDomain (optional) | string | custom domain the SDK uses in all outgoing network requests, commonly used for proxying. The format is second_level_domain.top_level_domain (for example, example.com ). The SDK uses the default Kameleoon value if the format is invalid. | undefined |
requestTimeout (optional) | number | timeout in milliseconds for all SDK network requests, if timeout is exceeded request will fail | 10_000 (10 seconds) |
trackingInterval (optional) | number | Specifies the interval for tracking requests in milliseconds. All visitors who were evaluated for any feature flag or had associated data will be included in this tracking request. The tracking request is performed once per interval. The minimum value is 100 ms and the maximum value is 1_000 ms | 1_000 (1 second) |
The domain
parameter is deprecated and will be removed in the future. Use cookieDomain
instead.
Name | Type | Description | Default Value |
---|---|---|---|
updateInterval (optional) | number | Specifies the refresh interval, in minutes, that the SDK fetches the configuration for the active experiments and feature flags. The value determines the maximum time it takes to propagate changes, such as activating or deactivating feature flags or launching experiments, to your production servers. If left unspecified, the default interval is set to 60 minutes. Additionally, we offer a streaming mode that uses server-sent events (SSE) to push new configurations to the SDK automatically and apply new configurations in real-time, without any delays. | 60 |
environment (optional) | Environment | string | feature flag environment | Environment.Production |
targetingDataCleanupInterval (optional) | number | interval in minutes for cleaning up targeting data, minimum value is 1 minute | 30 |
cookieDomain (optional) | string | domain that the cookie belongs to. | undefined |
networkDomain (optional) | string | The custom domain the SDK uses in all outgoing network requests, commonly used for proxying. The format is second_level_domain.top_level_domain (for example, example.com ). The SDK uses the default Kameleoon value if the format is invalid. | undefined |
requestTimeout (optional) | number | timeout in milliseconds for all SDK network requests, if timeout is exceeded request will fail immediately | 10_000 (10 seconds) |
trackingInterval (optional) | number | Specifies the interval for tracking requests in milliseconds. All visitors who were evaluated for any feature flag or had associated data will be included in this tracking request. The tracking request is performed once per interval. The minimum value is 100 ms and the maximum value is 1_000 ms | 1_000 (1 second) |
Compatibility Mode
Use the SDK parameter compatibility
to disable some of the SDK's features to improve compatibility with older NodeJS versions.
Compatibility
is an enum representing all possible compatibility modes:
Compatibility.Node16
- default mode, all features are enabled. This mode will be used if no compatibility mode is provided. Supports Node version 16 and higher.Compatibility.Node14
- compatibility with this version will make therequestTimeout
parameter inSDKConfigurationType
unavailable and prevent the SDK from usingAbortController
for request cancellation, even within default10_000
ms timeout. Supports Node version 14 and higher.Compatibility.Node12
- compatibility with this version implies the same limitations as theNode14
compatibility mode. Additionally, you cannot provide "@kameleoon/nodejs-requester" as a requester implementation in this compatibility mode, as it uses the "node-fetch" library, which doesn't support the Node.js 12.x.x version. A developer must provide a custom requester implementation of their choice, such as an older "node-fetch" version or other HTTP based implementation.
Activating a feature flag
Assigning a unique ID to a user
Use the getVisitorCode()
method to assign a unique ID to a user. If a visitor code doesn’t exist (from the request headers cookie), the method generates a random unique ID or uses a defaultVisitorCode
. The ID is then set in a response headers cookie.
If you are using Kameleoon in Hybrid mode, calling getVisitorCode()
ensures that the unique ID (visitorCode
) is shared between the application file (kameleoon.js) and the SDK.
Retrieving a flag configuration
To implement a feature flag in your code, you must first create a feature flag in your Kameleoon account.
You need to retrieve a feature flag's configuration to determine if it's active for a user. Use the getFeatureFlagVariationKey()
or isFeatureFlagActive()
method to retrieve the configuration based on the featureKey
.
Use the isFeatureFlagActive()
method if you want to retrieve the configuration of a simple feature flag that has only an ON or OFF state.
The getFeatureFlagVariationKey()
method retrieves the configuration of a feature experiment with several feature variations. You can use the method to get a variation key for a given user by providing the visitorCode
and featureKey
as mandatory arguments.
Feature flags can have associated variables that are used to customize their behavior. Use the getFeatureFlagVariables()
method to retrieve these variables. This method checks whether the user is targeted, finds the visitor’s assigned variation, saves it to storage, and sends a tracking request.
You only need to use one method to check if a feature flag is active. Choose isFeatureFlagActive
if you want to know if a feature flag is on or off. Use getFeatureFlagVariables
for more complex scenarios, like dynamically changing the feature's behavior.
Adding data points to target a user or filter / breakdown visits in reports
To target a user, ensure you’ve added relevant data points to their profile before retrieving the feature variation or checking if the flag is active. Use the addData()
method to add these data points to the user’s profile.
To retrieve data points that have been collected on other devices or to access a user's past data points (which would have been collected client-side if you are using Kameleoon in Hybrid mode), use the getRemoteVisitorData()
method. This method fetches data asynchronously from our servers. However, it is important to call getRemoteVisitorData()
before retrieving the variation or checking if the feature flag is active, as this data might be required to assign a user to a given variation of a feature flag.
To learn more about available targeting conditions, read our detailed article on the subject.
Additionally, the data points you add to the visitor profile will be available when analyzing your experiments, letting you filter and break down your results by criteria like device and browser. Kameleoon Hybrid mode automatically collects a variety of data points on the client-side, making it easy to break down your results based on these pre-collected data points. See the complete list here.
Use Kameleoon's Custom Data feature to track additional data points beyond what's collected automatically. Custom Data lets you capture and analyze specific information relevant to your experiments. Filter out bots using the userAgent
data type to ensure your results are accurate. You can learn more about userAgent
here. Don't forget to call the flush()
method to send the collected data to Kameleoon servers for analysis.
Tracking flag exposition and goal conversions
Kameleoon will automatically track visitors’ exposition to flags when you call one of these methods:
getFeatureFlagVariationKey()
getFeatureFlagVariable()
getFeatureFlagVariables()
isFeatureFlagActive()
When a user completes a desired action (for example, making a purchase), it counts as a conversion. You must use to the trackConversion()
method to track conversions, and provide the visitorCode
and goalId
parameters.
Sending events to analytics solutions
You must implement Kameleoon in Hybrid mode to track conversions and send exposure events to your analytics solution. Then, use the getEngineTrackingCode()
method.
The getEngineTrackingCode
method retrieves the unique tracking code that is required to send exposure events to your analytics solution. Using this method lets you record events and send them to your desired analytics platform.
Error Handling
Almost every KameleoonClient
method may throw an error at some point. These errors are deliberately predefined KameleoonError
s
that extend the native JavaScript Error
class, providing useful messages and a special type
field with a type KameleoonException
.
KameleoonException
is an enum containing all possible error variants.
To know exactly what variant of KameleoonException
the method may throw, check the method description's Throws
section, or hover over the method in your IDE to see the jsdocs description.
Handling errors is considered a good practice to make your application more stable and avoid technical issues.
- TypeScript
- JavaScript
import {
KameleoonError,
KameleoonClient,
KameleoonException,
} from '@kameleoon/nodejs-sdk';
import { KameleoonVisitorCodeManager } from '@kameleoon/nodejs-visitor-code-manager';
import { KameleoonEventSource } from '@kameleoon/nodejs-event-source';
import { KameleoonRequester } from '@kameleoon/nodejs-requester';
const client = new KameleoonClient({
siteCode: 'my_site_code',
credentials: { clientId: 'my_client_id', clientSecret: 'my_client_secret' },
externals: {
visitorCodeManager: new KameleoonVisitorCodeManager(),
eventSource: new KameleoonEventSource(),
requester: new KameleoonRequester(),
},
});
async function init(): Promise<void> {
try {
await client.initialize();
const customData = new CustomData(0, 'my_data');
client.addData(visitorCode, customData);
} catch (error) {
// -- Type guard for inferring error type, as native JavaScript `catch`
// only infers `unknown`
if (error instanceof KameleoonError) {
switch (error.type) {
case KameleoonException.VisitorCodeMaxLength:
// -- Handle an error
break;
case KameleoonException.StorageWrite:
// -- Handle an error
break;
case KameleoonException.Initialization:
// -- Handle an error
break;
default:
break;
}
}
}
}
init();
import { KameleoonClient, KameleoonException } from '@kameleoon/nodejs-sdk';
import { KameleoonVisitorCodeManager } from '@kameleoon/nodejs-visitor-code-manager';
import { KameleoonEventSource } from '@kameleoon/nodejs-event-source';
import { KameleoonRequester } from '@kameleoon/nodejs-requester';
const client = new KameleoonClient({
siteCode: 'my_site_code',
credentials: { clientId: 'my_client_id', clientSecret: 'my_client_secret' },
externals: {
visitorCodeManager: new KameleoonVisitorCodeManager(),
eventSource: new KameleoonEventSource(),
requester: new KameleoonRequester(),
},
});
async function init() {
try {
await client.initialize();
const customData = new CustomData(0, 'my_data');
client.addData(visitorCode, customData);
} catch (error) {
switch (error.type) {
case KameleoonException.VisitorCodeMaxLength:
// -- Handle an error
break;
case KameleoonException.StorageWrite:
// -- Handle an error
break;
case KameleoonException.Initialization:
// -- Handle an error
break;
default:
break;
}
}
}
init();
Integration with Edge providers
- SDK Version 4
- SDK Version 5
Kameleoon provides the following starter packs to automate your integration with specific edge providers:
Provider | Starter pack |
---|---|
Fastly Compute@Edge | https://github.com/Kameleoon/fastly-compute-starter-kit |
Cloudfare Workers | https://github.com/Kameleoon/cloudflare-worker-starter-kit |
AWS Lambda@Edge Function | https://github.com/Kameleoon/aws-lambda-edge-starter-kit |
For the other edge providers, you can initialize the Kameleoon Client yourself using externalClientConfiguration
. Passing externalClientConfiguration
causes the SDK to rely solely on the provided configuration data instead of making a call to the Kameleoon servers. externalClientConfiguration
gives you greater control and flexibility over the configuration data used in your application. For example:
- TypeScript / JavaScript
import rp from 'request-promise';
import { KameleoonClient, KameleoonUtils } from '@kameleoon/nodejs-sdk';
import { KameleoonVisitorCodeManager } from '@kameleoon/nodejs-visitor-code-manager';
import { KameleoonEventSource } from '@kameleoon/nodejs-event-source';
async function init() {
// -- Get Kameleoon Client Configuration URL
const uri = KameleoonUtils.getClientConfigurationUrl('my_site_code');
const clientConfiguration = await rp({
uri,
json: true,
});
const client = new KameleoonClient({
siteCode: 'my_site_code',
credentials: { clientId: 'my_client_id', clientSecret: 'my_client_secret' },
integrations: {
externalClientConfiguration: clientConfiguration,
},
externals: {
visitorCodeManager: new KameleoonVisitorCodeManager(),
eventSource: new KameleoonEventSource(),
},
});
}
init();
Kameleoon provides the following starter packs to automate your integration with specific edge providers:
Provider | Starter pack |
---|---|
Fastly Compute@Edge | https://github.com/Kameleoon/fastly-compute-starter-kit |
Cloudfare Workers | https://github.com/Kameleoon/cloudflare-worker-starter-kit |
AWS Lambda@Edge Function | https://github.com/Kameleoon/aws-lambda-edge-starter-kit |
For other edge providers, use External Dependencies for greater control over the SDK.
Cross-device experimentation
To support visitors who access your app from multiple devices, Kameleoon allows you to synchronize previously collected visitor data across each of the visitor's devices and reconcile their visit history across devices through cross-device experimentation. We recommend reading our article on cross-device experimentation for more information on how Kameleoon handles data across devices and detailed use cases.
Synchronizing custom data across devices
Although custom mapping synchronization is used to align visitor data across devices, it is not always necessary. Below are two scenarios where custom mapping sync is not required:
Same user ID across devices
If the same user ID is used consistently across all devices, synchronization is handled automatically without a custom mapping sync. It is enough to call the getRemoteVisitorData()
method when you want to sync the data collected between multiple devices.
Multi-server instances with consistent IDs
In complex setups involving multiple servers (for example, distributed server instances), where the same user ID is available across servers, synchronization between servers (with getRemoteVisitorData()
) is sufficient without additional custom mapping sync.
Customers who need additional data can refer to the getRemoteVisitorData()
method description for further guidance. In the below code, it is assumed that the same unique identifier (in this case, the visitorCode
, which can also be referred to as userId
) is used consistently between the two devices for accurate data retrieval.
If you want to sync collected data in real time, you need to choose the scope Visitor for your custom data.
- TypeScript
- JavaScript
import { KameleoonClient, CustomData } from '@kameleoon/nodejs-sdk';
import { KameleoonVisitorCodeManager } from '@kameleoon/nodejs-visitor-code-manager';
import { KameleoonEventSource } from '@kameleoon/nodejs-event-source';
import { KameleoonRequester } from '@kameleoon/nodejs-requester';
const client = new KameleoonClient({
siteCode: 'my_site_code',
credentials: { clientId: 'my_client_id', clientSecret: 'my_client_secret' },
externals: {
visitorCodeManager: new KameleoonVisitorCodeManager(),
eventSource: new KameleoonEventSource(),
requester: new KameleoonRequester(),
},
});
async function init(): Promise<void> {
await client.initialize();
// -- Custom Data with index `0` was set to `Visitor` scope
// in Kameleoon.
const customDataIndex = 0;
const customData = new CustomData(customDataIndex, 'my_data');
client.addData('my_visitor', customData);
client.flush();
}
init();
import { KameleoonClient } from '@kameleoon/nodejs-sdk';
import { KameleoonVisitorCodeManager } from '@kameleoon/nodejs-visitor-code-manager';
import { KameleoonEventSource } from '@kameleoon/nodejs-event-source';
import { KameleoonRequester } from '@kameleoon/nodejs-requester';
const client = new KameleoonClient({
siteCode: 'my_site_code',
credentials: { clientId: 'my_client_id', clientSecret: 'my_client_secret' },
externals: {
visitorCodeManager: new KameleoonVisitorCodeManager(),
eventSource: new KameleoonEventSource(),
requester: new KameleoonRequester(),
},
});
async function init(): Promise<void> {
await client.initialize();
// -- Before working with data, call `getRemoteVisitorData`.
await getRemoteVisitorData({ visitorCode: 'my_visitor_code' });
// -- New SDK code will have access to CustomData with `Visitor` scope
// defined on Device One.
// So, "my_data" is available to target and track "my_visitor".
}
init();
import { KameleoonClient, CustomData } from '@kameleoon/nodejs-sdk';
import { KameleoonVisitorCodeManager } from '@kameleoon/nodejs-visitor-code-manager';
import { KameleoonEventSource } from '@kameleoon/nodejs-event-source';
import { KameleoonRequester } from '@kameleoon/nodejs-requester';
const client = new KameleoonClient({
siteCode: 'my_site_code',
credentials: { clientId: 'my_client_id', clientSecret: 'my_client_secret' },
externals: {
visitorCodeManager: new KameleoonVisitorCodeManager(),
eventSource: new KameleoonEventSource(),
requester: new KameleoonRequester(),
},
});
async function init() {
await client.initialize();
// -- Custom Data with index `0` was set to `Visitor` scope
// in Kameleoon.
const customDataIndex = 0;
const customData = new CustomData(customDataIndex, 'my_data');
client.addData('my_visitor', customData);
client.flush();
}
init();
import { KameleoonClient } from '@kameleoon/nodejs-sdk';
import { KameleoonVisitorCodeManager } from '@kameleoon/nodejs-visitor-code-manager';
import { KameleoonEventSource } from '@kameleoon/nodejs-event-source';
import { KameleoonRequester } from '@kameleoon/nodejs-requester';
const client = new KameleoonClient({
siteCode: 'my_site_code',
credentials: { clientId: 'my_client_id', clientSecret: 'my_client_secret' },
externals: {
visitorCodeManager: new KameleoonVisitorCodeManager(),
eventSource: new KameleoonEventSource(),
requester: new KameleoonRequester(),
},
});
async function init() {
await client.initialize();
// -- Before working with data, call `getRemoteVisitorData`.
await getRemoteVisitorData({ visitorCode: 'my_visitor_code' });
// -- New SDK code will have access to CustomData with `Visitor` scope
// defined on Device One.
// So, "my_data" is now available to target and track "my_visitor".
}
init();
Using custom data for session merging
- SDK Version 4
- SDK Version 5
Cross-device experimentation lets you combine a visitor's history across each of their devices (history reconciliation). You can merge multiple visitor sessions into one with history reconciliation. Use CustomData
and provide a unique identifier for the visitor to reconcile visit history.
Follow the activating cross-device history reconciliation guide to set up your custom data in Kameleoon.
You can use custom data in your code to merge a visitor's session. Sessions with the same identifier will always see the same experiment variation, and will be displayed as a single visitor in the Visitor
view of your experiment's result page.
The SDK configuration ensures that associated sessions always see the same variation of the experiment.
The following methods might be helpful in the context of session merging:
- Use
getRemoteVisitorData
withisUniqueIdentifier=true
to retrieve data for all linked visitors. - Use
trackConversion
orflush
withisUniqueIdentifier=true
to track data for specific visitor that is associated with another visitor.
Since the custom data you use as the identifier must be set to the Visitor
scope, you must use cross-device custom data synchronization to retrieve the identifier with the getRemoteVisitorData
method on each device.
In the following example, we have an application with a login page. Since we don't know the user ID at the time of login, we use an anonymous visitor identifier generated by the getVisitorCode
method. After the user logs in, we can associate the anonymous visitor with the user ID and use it as a unique identifier for the visitor.
- TypeScript
- JavaScript
import { KameleoonClient } from '@kameleoon/nodejs-sdk';
import { KameleoonVisitorCodeManager } from '@kameleoon/nodejs-visitor-code-manager';
import { KameleoonEventSource } from '@kameleoon/nodejs-event-source';
import { KameleoonRequester } from '@kameleoon/nodejs-requester';
const client = new KameleoonClient({
siteCode: 'my_site_code',
credentials: { clientId: 'my_client_id', clientSecret: 'my_client_secret' },
externals: {
visitorCodeManager: new KameleoonVisitorCodeManager(),
eventSource: new KameleoonEventSource(),
requester: new KameleoonRequester(),
},
});
async function init(): Promise<void> {
await client.initialize();
const anonymousVisitor = getVisitorCode();
// -- Saving `visitorCode` in `global` to re-use it later
global.anonymousVisitor = anonymousVisitor;
// -- Getting a variation, assume it's variation `A`
const variation = client.getFeatureFlagVariationKey(
anonymousVisitor,
'my_feature_key',
);
}
init();
import { CustomData } from '@kameleoon/nodejs-sdk';
async function init(): Promise<void> {
// -- At this point anonymous visitor has logged in,
// and we have a user ID to use as a visitor identifier
// -- Associating both visitors with an identifier Custom Data,
// where index `1` is the Custom Data's index, configured
// as a unique identifier in Kameleoon.
const userIdentifierData = new CustomData(1, 'my_user_id');
// -- Taking `visitorCode` from `global` object
client.addData(global.anonymousVisitor, userIdentifierData);
// -- Retrieving the variation for the user ID ensures
// consistency with the anonymous visitor's variation.
// Both the anonymous visitor and the user ID will be
// assigned variation `A`.
const variation = client.getFeatureFlagVariationKey(
'my_user_id',
'my_feature_key',
);
// -- `my_user_id` and `anonymousVisitor` are now linked.
// They can be tracked as a single visitor
client.trackConversion({
visitorCode: 'my_user_id',
goalId: 123,
revenue: 100,
// -- Informing the SDK that the visitor is a unique identifier.
isUniqueIdentifier: true,
});
// -- Additionally, linked visitors share previously
// collected remote data.
const data = await client.getRemoteVisitorData({
visitorCode: 'my_user_id',
// -- Informing the SDK that the visitor is a unique identifier.
isUniqueIdentifier: true,
});
}
init();
import { KameleoonClient } from '@kameleoon/nodejs-sdk';
import { KameleoonVisitorCodeManager } from '@kameleoon/nodejs-visitor-code-manager';
import { KameleoonEventSource } from '@kameleoon/nodejs-event-source';
import { KameleoonRequester } from '@kameleoon/nodejs-requester';
const client = new KameleoonClient({
siteCode: 'my_site_code',
credentials: { clientId: 'my_client_id', clientSecret: 'my_client_secret' },
externals: {
visitorCodeManager: new KameleoonVisitorCodeManager(),
eventSource: new KameleoonEventSource(),
requester: new KameleoonRequester(),
},
});
async function init() {
await client.initialize();
const anonymousVisitor = getVisitorCode();
// -- Saving `visitorCode` in `global` to re-use it later.
global.anonymousVisitor = anonymousVisitor;
// -- Getting a variation, assume it's variation `A`.
const variation = client.getFeatureFlagVariationKey(
anonymousVisitor,
'my_feature_key',
);
}
init();
import { CustomData } from '@kameleoon/nodejs-sdk';
async function init() {
// -- At this point anonymous visitor has logged in,
// and we have a user ID to use as a visitor identifier
// -- Associating both visitors with an identifier Custom Data,
// where index `1` is the Custom Data's index, configured
// as a unique identifier in Kameleoon.
const userIdentifierData = new CustomData(1, 'my_user_id');
// -- Taking `visitorCode` from `global` object
client.addData(global.anonymousVisitor, userIdentifierData);
// -- Retrieving the variation for the user ID ensures
// consistency with the anonymous visitor's variation.
// Both the anonymous visitor and the user ID will be
// assigned variation `A`.
const variation = client.getFeatureFlagVariationKey(
'my_user_id',
'my_feature_key',
);
// -- `my_user_id` and `anonymousVisitor` are now linked.
// They can be tracked as a single visitor.
client.trackConversion({
visitorCode: 'my_user_id',
goalId: 123,
revenue: 100,
// -- Informing the SDK that the visitor is a unique identifier.
isUniqueIdentifier: true,
});
// -- Additionally, linked visitors share previously
// collected remote data.
const data = await client.getRemoteVisitorData({
visitorCode: 'my_user_id',
// -- Informing the SDK that the visitor is a unique identifier.
isUniqueIdentifier: true,
});
}
init();
Cross-device experimentation lets you combine a visitor's history across each of their devices (history reconciliation). You can merge multiple visitor sessions into one with history reconciliation. Use CustomData
and provide a unique identifier for the visitor to reconcile visit history.
Follow the activating cross-device history reconciliation guide to set up your custom data in Kameleoon.
You can use custom data in your code to merge a visitor's session. Sessions with the same identifier will always see the same experiment variation, and will be displayed as a single visitor in the Visitor
view of your experiment's result page.
The SDK configuration ensures that associated sessions always see the same variation of the experiment.
Before using other methods, inform the SDK that the visitor is a unique identifier by adding UniqueIdentifier
data to a visitor.
Since the custom data you use as the identifier must be set to the Visitor
scope, you must use cross-device custom data synchronization to retrieve the identifier with the getRemoteVisitorData
method on each device.
In the following example, we have an application with a login page. Since we don't know the user ID at the time of login, we use an anonymous visitor identifier generated by the getVisitorCode
method. After the user logs in, we can associate the anonymous visitor with the user ID and use it as a unique identifier for the visitor.
- TypeScript
- JavaScript
import { KameleoonClient } from '@kameleoon/nodejs-sdk';
import { KameleoonVisitorCodeManager } from '@kameleoon/nodejs-visitor-code-manager';
import { KameleoonEventSource } from '@kameleoon/nodejs-event-source';
import { KameleoonRequester } from '@kameleoon/nodejs-requester';
const client = new KameleoonClient({
siteCode: 'my_site_code',
credentials: { clientId: 'my_client_id', clientSecret: 'my_client_secret' },
externals: {
visitorCodeManager: new KameleoonVisitorCodeManager(),
eventSource: new KameleoonEventSource(),
requester: new KameleoonRequester(),
},
});
async function init(): Promise<void> {
await client.initialize();
const anonymousVisitor = getVisitorCode();
// -- Saving `visitorCode` in `global` to re-use it later.
global.anonymousVisitor = anonymousVisitor;
// -- Getting a variation, assume it's variation `A`.
const variation = client.getFeatureFlagVariationKey(
anonymousVisitor,
'my_feature_key',
);
}
init();
import { CustomData, UniqueIdentifier } from '@kameleoon/nodejs-sdk';
async function init(): Promise<void> {
// -- At this point anonymous visitor has logged in,
// and we have a user ID to use as a visitor identifier
// -- Associating both visitors with an identifier Custom Data,
// where index `1` is the Custom Data's index, configured
// as a unique identifier in Kameleoon.
const userIdentifierData = new CustomData(1, 'my_user_id');
// -- Taking `visitorCode` from `global` object
client.addData(global.anonymousVisitor, userIdentifierData);
// -- Informing the SDK that the visitor is a unique identifier.
client.addData('my_user_id', new UniqueIdentifier(true));
// -- Retrieving the variation for the user ID ensures
// consistency with the anonymous visitor's variation.
// Both the anonymous visitor and the user ID will be
// assigned variation `A`.
const variation = client.getFeatureFlagVariationKey(
'my_user_id',
'my_feature_key',
);
// -- `my_user_id` and `anonymousVisitor` are now linked.
// They can be tracked as a single visitor.
client.trackConversion({
visitorCode: 'my_user_id',
goalId: 123,
revenue: 100,
});
// -- Additionally, linked visitors share previously
// collected remote data.
const data = await client.getRemoteVisitorData({
visitorCode: 'my_user_id',
});
}
init();
import { KameleoonClient } from '@kameleoon/nodejs-sdk';
import { KameleoonVisitorCodeManager } from '@kameleoon/nodejs-visitor-code-manager';
import { KameleoonEventSource } from '@kameleoon/nodejs-event-source';
import { KameleoonRequester } from '@kameleoon/nodejs-requester';
const client = new KameleoonClient({
siteCode: 'my_site_code',
credentials: { clientId: 'my_client_id', clientSecret: 'my_client_secret' },
externals: {
visitorCodeManager: new KameleoonVisitorCodeManager(),
eventSource: new KameleoonEventSource(),
requester: new KameleoonRequester(),
},
});
async function init() {
await client.initialize();
const anonymousVisitor = getVisitorCode();
// -- Saving `visitorCode` in `global` to re-use it later.
global.anonymousVisitor = anonymousVisitor;
// -- Getting a variation, assume it's variation `A`.
const variation = client.getFeatureFlagVariationKey(
anonymousVisitor,
'my_feature_key',
);
}
init();
import { CustomData, UniqueIdentifier } from '@kameleoon/nodejs-sdk';
async function init() {
// -- At this point anonymous visitor has logged in,
// and we have a user ID to use as a visitor identifier
// -- Associating both visitors with an identifier Custom Data,
// where index `1` is the Custom Data's index, configured
// as a unique identifier in Kameleoon.
const userIdentifierData = new CustomData(1, 'my_user_id');
// -- Taking `visitorCode` from `global` object
client.addData(global.anonymousVisitor, userIdentifierData);
// -- Informing the SDK that the visitor is a unique identifier.
client.addData('my_user_id', new UniqueIdentifier(true));
// -- Retrieving the variation for the user ID ensures
// consistency with the anonymous visitor's variation.
// Both the anonymous visitor and the user ID will be
// assigned variation `A`.
const variation = client.getFeatureFlagVariationKey(
'my_user_id',
'my_feature_key',
);
// -- `my_user_id` and `anonymousVisitor` are now linked.
// They can be tracked as a single visitor.
client.trackConversion({
visitorCode: 'my_user_id',
goalId: 123,
revenue: 100,
});
// -- Additionally, linked visitors share previously
// collected remote data.
const data = await client.getRemoteVisitorData({
visitorCode: 'my_user_id',
});
}
init();
Targeting conditions
The Kameleoon SDKs support a variety of predefined targeting conditions that you can use to target users in your campaigns. For the list of conditions supported by this SDK, see use visit history to target users.
You can also use your own external data to target users.
Logging
The SDK generates logs to reflect various internal processes and issues.
Log levels
The SDK supports configuring limiting logging by a log level.
- TypeScript
- JavaScript
import { KameleoonClient, KameleoonLogger, LogLevel } from '@kameleoon/nodejs-sdk';
const client = new KameleoonClient({ siteCode: 'my_site_code', configuration });
// The `NONE` log level does not allow logging.
client.setLogLevel(LogLevel.NONE);
// Or use directly KameleoonLogger
KameleoonLogger.setLogLevel(LogLevel.NONE);
// The `ERROR` log level only allows logging issues that may affect the SDK's main behaviour.
client.setLogLevel(LogLevel.ERROR);
// Or use directly KameleoonLogger
KameleoonLogger.setLogLevel(LogLevel.ERROR);
// The `WARNING` log level allows logging issues which may require additional attention.
// It extends the `ERROR` log level.
// The `WARNING` log level is a default log level.
client.setLogLevel(LogLevel.WARNING);
// Or use KameleoonLogger
KameleoonLogger.setLogLevel(LogLevel.WARNING);
// The `INFO` log level allows logging general information on the SDK's internal processes.
// It extends the `WARNING` log level.
client.setLogLevel(LogLevel.INFO);
// Or use KameleoonLogger
KameleoonLogger.setLogLevel(LogLevel.INFO);
// The `DEBUG` log level allows logging extra information on the SDK's internal processes.
// It extends the `INFO` log level.
client.setLogLevel(LogLevel.DEBUG);
// Or use KameleoonLogger
KameleoonLogger.setLogLevel(LogLevel.DEBUG);
import { KameleoonClient, KameleoonLogger, LogLevel } from '@kameleoon/nodejs-sdk';
const client = new KameleoonClient({ siteCode: 'my_site_code', configuration });
// The `NONE` log level does not allow logging.
client.setLogLevel(LogLevel.NONE);
// Or use KameleoonLogger
KameleoonLogger.setLogLevel(LogLevel.NONE);
// The `ERROR` log level only allows logging issues that may affect the SDK's main behaviour.
client.setLogLevel(LogLevel.ERROR);
// Or use KameleoonLogger
KameleoonLogger.setLogLevel(LogLevel.ERROR);
// The `WARNING` log level allows logging issues which may require additional attention.
// It extends the `ERROR` log level.
// The `WARNING` log level is a default log level.
client.setLogLevel(LogLevel.WARNING);
// Or use KameleoonLogger
KameleoonLogger.setLogLevel(LogLevel.WARNING);
// The `INFO` log level allows logging general information on the SDK's internal processes.
// It extends the `WARNING` log level.
client.setLogLevel(LogLevel.INFO);
// Or use KameleoonLogger
KameleoonLogger.setLogLevel(LogLevel.INFO);
// The `DEBUG` log level allows logging extra information on the SDK's internal processes.
// It extends the `INFO` log level.
client.setLogLevel(LogLevel.DEBUG);
// Or use KameleoonLogger
KameleoonLogger.setLogLevel(LogLevel.DEBUG);
Custom handling of logs
The SDK writes its logs to the console output by default. This behaviour can be overridden.
Logging limiting by a log level is performed apart from the log handling logic.
- TypeScript
- JavaScript
import { KameleoonClient, KameleoonLogger, IExternalLogger, LogLevel } from '@kameleoon/nodejs-sdk';
export class CustomLogger implements IExternalLogger {
// `log` method accepts logs from the SDK
public log(level: LogLevel, message: string): void {
// Custom log handling logic here. For example:
switch (level) {
case LogLevel.DEBUG:
console.debug(message);
break;
case LogLevel.INFO:
console.info(message);
break;
case LogLevel.WARNING:
console.warn(message);
break;
case LogLevel.ERROR:
console.error(message);
break;
}
}
}
const client = new KameleoonClient({
siteCode: 'my_site_code',
externals: {
logger: new CustomLogger(),
},
});
// Log level filtering is applied separately from log handling logic.
// The custom logger will only accept logs that meet or exceed the specified log level.
// Ensure the log level is set correctly.
client.setLogLevel(LogLevel.DEBUG);
// Or use KameleoonLogger
KameleoonLogger.setLogLevel(LogLevel.DEBUG);
import { KameleoonClient, KameleoonLogger, LogLevel } from '@kameleoon/nodejs-sdk';
export class CustomLogger {
// `log` method accepts logs from the SDK
log(level, message) {
// Custom log handling logic here. For example:
switch (level) {
case 'DEBUG':
console.debug(message);
break;
case 'INFO':
console.info(message);
break;
case 'WARNING':
console.warn(message);
break;
case 'ERROR':
console.error(message);
break;
}
}
}
const client = new KameleoonClient({
siteCode: 'my_site_code',
externals: {
logger: new CustomLogger(),
},
});
// Log level filtering is applied separately from log handling logic.
// The custom logger will only accept logs that meet or exceed the specified log level.
// Ensure the log level is set correctly.
client.setLogLevel(LogLevel.DEBUG);
// Or use KameleoonLogger
KameleoonLogger.setLogLevel(LogLevel.DEBUG);
Domain information
You provide a domain as the domain
in KameleoonClient
configuration, which is used for storing Kameleoon visitor code in cookies. Providing a domain is important when working with the getVisitorCode
and setLegalConsent
methods. The domain you provide is stored in the cookie as the Domain=
key.
Setting the domain
The domain you provide lets the URL address use the cookie. For example, if your domain is www.example.com
, the cookie is only available from a www.example.com
URL. Pages with the app.example.com
domain can't use the cookie.
For more flexibility with subdomains, you can specify a domain starting with a .
. For instance, domain .example.com
allows the cookie to function on both app.example.com
and login.example.com
.
You can't use regular expressions, special symbols, protocol, or port numbers in the domain
. Additionally, a specific list of subdomains cannot be used with the prefix .
.
Here's a small domain cheat sheet:
Domain | Allowed URLs | Disallowed URLs |
---|---|---|
www.example.com | ✅www.example.com | ❌ app.example.com |
✅ example.com | ❌ .com | |
.example.com = example.com | ✅ example.com | ❌ otherexample.com |
✅ www.example.com | ||
✅ app.example.com | ||
✅ login.example.com | ||
https://www.example.com | ⛔ bad domain | ⛔ bad domain |
www.example.com:4408 | ⛔ bad domain | ⛔ bad domain |
.localhost.com = localhost | ⛔ bad domain | ⛔ bad domain |
Developing on localhost
localhost
is always considered a bad domain, making it hard to test the domain when developing on localhost
.
There are two ways to avoid this issue:
- Don't specify the
domain
field in the SDK client while testing. This preventslocalhost
issues (the cookie will be set on any domain). - Create a local domain for
localhost
. For example:- Navigate to
/etc/hosts
on Linux or toc:\Windows\System32\Drivers\etc\hosts
on Windows - Open
hosts
with file super user or administrator rights - Add a domain to the
localhost
port, for example:127.0.0.1 app.com
- Now, you can run your app locally on
app.com:{my_port}
and specify.app.com
as your domain
- Navigate to
External dependencies
The SDK's external dependencies use the dependency injection pattern, letting you provide your own implementations for certain parts of an SDK.
In the NodeJS SDK, some external dependencies have default implementations, while others must be provided by the user, whether using dedicated Kameleoon implementations or custom implementations.
Here's the list of available external dependencies:
Dependency | Interface | Required/Optional | API Used | Description |
---|---|---|---|---|
storage | IExternalStorage | Optional | Server memory | Used for storing all of the existing and collected SDK data |
eventSource | IExternalEventSource | Required | - | Used for receiving Server Sent Events for Real Time Updates capabilities |
requester | IExternalRequester | Required | - | Used for performing all network requests |
visitorCodeManager | IExternalVisitorCodeManager | Required | - | Used for storing and synchronizing visitor code |
logger | ILogger | Optional | Custom implementation | Used for custom handling of logs from the SDK. Lets users define how logs are processed and where they output. |
You can also implement visitorCodeManager
using the IExternalNextJSVisitorCodeManager
, IExternalDenoVisitorCodeManager
, or IExternalCustomVisitorCodeManager
interfaces for NextJS, Deno, or custom visitor code manager implementations, respectively.
External dependencies provide developers flexibility to adapt and use the NodeJS SDK in any environment. There are a number of npm packages Kameleoon provides for frequently used environments. You can install the packages manually, or by using the SDK installation tool (recommended).
These are the Kameleoon-provided external dependencies for NodeJS SDK:
@kameleoon/nodejs-event-source
- based oneventsource
library (can be used for NodeJS/Deno/NextJS SSR)@kameleoon/nodejs-requester
- based onnode-fetch
library (can be used for NodeJS/Deno/NextJS SSR)@kameleoon/nodejs-visitor-code-manager
- implemented with server memory storage@kameleoon/deno-visitor-code-manager
- implemented using Deno request/response cookies@kameleoon/nextjs-visitor-code-manager
- implemented using NextJS SSRheaders
cookie or NextJS SSR request/response
You can optionally implement external dependencies on your own.
The following example implements external dependencies. To import an interface from an SDK, create a class that implements it and pass the instantiated class to the SDK.
Storage
- TypeScript
- JavaScript
import { IExternalStorage, KameleoonClient } from '@kameleoon/nodejs-sdk';
// --- External Storage implementation ---
// - JavaScript `Map` is used as an example storage
const storage = new Map();
class MyStorage<T> implements IExternalStorage<T> {
public read(key: string): T | null {
// - Read data using `key`
const data = storage.get(key);
// - Return `null` if there's no data
if (!data) {
return null;
}
// - Return obtained data
return data;
}
public write(key: string, data: T): void {
// - Write data using `key`
storage.set(key, data);
}
}
// --- Create KameleoonClient ---
const client = new KameleoonClient({
siteCode: 'my_site_code',
externals: {
storage: new MyStorage(),
},
});
import { KameleoonClient } from '@kameleoon/nodejs-sdk';
// --- External Storage implementation ---
// - JavaScript `Map` is used as an example storage
const storage = new Map();
class MyStorage {
read(key) {
// - Read data using `key`
const data = storage.get(key);
// - Return `null` if there's no data
if (!data) {
return null;
}
// - Return obtained data
return data;
}
write(key, data) {
// - Write data using `key`
storage.set(key, data);
}
}
// --- Create KameleoonClient ---
const client = new KameleoonClient({
siteCode: 'my_site_code',
externals: {
storage: new MyStorage(),
},
});
EventSource
- TypeScript
- JavaScript
import {
IExternalEventSource,
KameleoonClient,
EventSourceOpenParametersType,
} from '@kameleoon/nodejs-sdk';
// --- External EventSource implementation ---
// - Example uses dummy `EventSource` class
class MyEventSource implements IExternalEventSource {
private eventSource?: EventSource;
public open({
eventType,
onEvent,
url,
}: EventSourceOpenParametersType): void {
// - Initialize `EventSource` (use any event source of your choice here)
const eventSource = new EventSource(url);
this.eventSource = eventSource;
// - Add event listener with provided event type and event callback
this.eventSource.addEventListener(eventType, onEvent);
}
public close(): void {
// - Clean up open event source
if (this.eventSource) {
this.eventSource.close();
}
}
public onError(callback: (error: Event) => void): void {
// - Set error callback
if (this.eventSource) {
this.eventSource.onerror = callback;
}
}
}
// --- Create KameleoonClient ---
const client = new KameleoonClient({
siteCode: 'my_site_code',
externals: {
eventSource: new MyEventSource(),
},
});
import { KameleoonClient } from '@kameleoon/nodejs-sdk';
// --- External EventSource implementation ---
// - Example uses dummy `EventSource` class
class MyEventSource {
eventSource;
open({ eventType, onEvent, url }) {
// - Initialize `EventSource` (use any event source of your choice here)
const eventSource = new EventSource(url);
this.eventSource = eventSource;
// - Add event listener with provided event type and event callback
this.eventSource.addEventListener(eventType, onEvent);
}
close() {
// - Cleanup open event source
if (this.eventSource) {
this.eventSource.close();
}
}
public onError(callback) {
// - Set error callback
if (this.eventSource) {
this.eventSource.onerror = callback;
}
}
}
// --- Create KameleoonClient ---
const client = new KameleoonClient({
siteCode: 'my_site_code',
externals: {
eventSource: new MyEventSource(),
},
});
VisitorCodeManager
visitorCodeManager
implementation for NodeJS
/NextJS SSR
:
- TypeScript
- JavaScript
import {
IExternalVisitorCodeManager,
SetDataParametersType,
GetDataParametersType,
KameleoonClient,
KameleoonUtils,
} from '@kameleoon/nodejs-sdk';
// --- External Visitor Code Manager implementation ---
// - Example uses server `request` and `response`
class MyVisitorCodeManager implements IExternalVisitorCodeManager {
public getData({ request, key }: GetDataParametersType): string | null {
// - Get cookie from server request
const cookieString = request.headers.cookie;
// - Return `null` if no cookie was found
if (!cookieString) {
return null;
}
// - Parse cookie using the provided `key`
return KameleoonUtils.getCookieValue(cookieString, key);
}
public setData({
visitorCode,
response,
domain,
maxAge,
key,
path,
}: SetDataParametersType): void {
// - Set cookie to request using provided parameters
let resultCookie = `${key}=${visitorCode}; Max-Age=${maxAge}; Path=${path}`;
if (domain) {
resultCookie += `; Domain=${domain}`;
}
response.setHeader('Set-Cookie', resultCookie);
}
}
// --- Create KameleoonClient ---
const client = new KameleoonClient({
siteCode: 'my_site_code',
externals: {
visitorCodeManager: new MyVisitorCodeManager(),
},
});
import { KameleoonClient, KameleoonUtils } from '@kameleoon/nodejs-sdk';
// --- External Visitor Code Manager implementation ---
// - Example uses server `request` and `response`
class MyVisitorCodeManager {
getData({ request, key }) {
// - Get cookie from server request
const cookieString = request.headers.cookie;
if (!cookieString) {
return null;
}
// - Return `null` if no cookie was found
if (!cookieString) {
return null;
}
// - Parse cookie using the provided `key`
return KameleoonUtils.getCookieValue(cookieString, key);
}
setData({ visitorCode, response, domain, maxAge, key, path }) {
// - Set cookie to request using provided parameters
let resultCookie = `${key}=${visitorCode}; Max-Age=${maxAge}; Path=${path}`;
if (domain) {
resultCookie += `; Domain=${domain}`;
}
response.setHeader('Set-Cookie', resultCookie);
}
}
// --- Create KameleoonClient ---
const client = new KameleoonClient({
siteCode: 'my_site_code',
externals: {
visitorCodeManager: new MyVisitorCodeManager(),
},
});
visitorCodeManager
implementation for Deno
:
- TypeScript
- JavaScript
import {
IExternalDenoVisitorCodeManager,
SetDenoDataParametersType,
GetDenoDataParametersType,
KameleoonClient,
KameleoonUtils,
} from '@kameleoon/nodejs-sdk';
// --- External Visitor Code Manager implementation ---
// - Example uses server `request` and `response`
class MyVisitorCodeManager implements IExternalDenoVisitorCodeManager {
public getData({ request, key }: GetDenoDataParametersType): string | null {
// - Get cookie from server request
const cookieString = request.headers.get('cookie');
// - Return `null` if no cookie was found
if (!cookieString) {
return null;
}
// - Parse cookie using the provided `key`
return KameleoonUtils.getCookieValue(cookieString, key);
}
public setData({
visitorCode,
response,
domain,
maxAge,
key,
path,
}: SetDenoDataParametersType): void {
// - Set cookie to request using provided parameters
let resultCookie = `${key}=${visitorCode}; Max-Age=${maxAge}; Path=${path}`;
if (domain) {
resultCookie += `; Domain=${domain}`;
}
response.headers.set('Set-Cookie', resultCookie);
}
}
// --- Create KameleoonClient ---
const client = new KameleoonClient({
siteCode: 'my_site_code',
externals: {
visitorCodeManager: new MyVisitorCodeManager(),
},
});
import { KameleoonClient, KameleoonUtils } from '@kameleoon/nodejs-sdk';
// --- External Visitor Code Manager implementation ---
// - Example uses server `request` and `response`
class MyVisitorCodeManager {
getData({ request, key }) {
// - Get cookie from server request
const cookieString = request.headers.get('cookie');
// - Return `null` if no cookie was found
if (!cookieString) {
return null;
}
// - Parse cookie finding it by provided `key`
return KameleoonUtils.getCookieValue(cookieString, key);
}
setData({ visitorCode, response, domain, maxAge, key, path }) {
// - Set cookie to request using provided parameters
let resultCookie = `${key}=${visitorCode}; Max-Age=${maxAge}; Path=${path}`;
if (domain) {
resultCookie += `; Domain=${domain}`;
}
response.headers.set('Set-Cookie', resultCookie);
}
}
// --- Create KameleoonClient ---
const client = new KameleoonClient({
siteCode: 'my_site_code',
externals: {
visitorCodeManager: new MyVisitorCodeManager(),
},
});
visitorCodeManager
implementation for NextJS
Server Actions:
- TypeScript
- JavaScript
import {
IExternalNextJSVisitorCodeManager,
SetNextJSDataParametersType,
GetNextJSDataParametersType,
KameleoonClient,
} from '@kameleoon/nodejs-sdk';
// --- External Visitor Code Manager implementation ---
// - Example uses server `cookie` object imported from "next/headers"
class MyVisitorCodeManager implements IExternalNextJSVisitorCodeManager {
public getData({ cookie, key }: GetNextJSDataParametersType): string | null {
// - Get cookie from server request by provided `key`
const cookie = cookies().get(key);
if (cookie) {
return cookie.value;
}
// - Return `null` if no cookie was found
return null;
}
public setData({
visitorCode,
cookie,
domain,
maxAge,
key,
path,
}: SetNextJSDataParametersType): void {
// - Set cookie to request using provided parameters
cookies().set(key, visitorCode, {
path,
domain,
maxAge,
});
}
}
// --- Create KameleoonClient ---
const client = new KameleoonClient({
siteCode: 'my_site_code',
externals: {
visitorCodeManager: new MyVisitorCodeManager(),
},
});
import { KameleoonClient } from '@kameleoon/nodejs-sdk';
// --- External Visitor Code Manager implementation ---
// - Example uses server `cookie` object imported from "next/headers"
class MyVisitorCodeManager {
public getData({ cookie, key }) {
// - Get cookie from server request by provided `key`
const cookie = cookies().get(key);
if (cookie) {
return cookie.value;
}
// - Return `null` if no cookie was found
return null;
}
public setData({
visitorCode,
cookie,
domain,
maxAge,
key,
path,
}){
// - Set cookie to request using provided parameters
cookies().set(key, visitorCode, {
path,
domain,
maxAge,
});
}
}
// --- Create KameleoonClient ---
const client = new KameleoonClient({
siteCode: 'my_site_code',
externals: {
visitorCodeManager: new MyVisitorCodeManager(),
},
});
Custom visitorCodeManager
implementation with arbitrary parameters:
- TypeScript
- JavaScript
import {
IExternalCustomVisitorCodeManager,
SetDataCustomParametersType,
GetDataCustomParametersType,
KameleoonClient,
} from '@kameleoon/nodejs-sdk';
// --- External Visitor Code Manager implementation ---
// - Example uses custom arbitrary `input` and `output` objects
class MyVisitorCodeManager implements IExternalCustomVisitorCodeManager {
public getData({ input, key }: GetDataCustomParametersType): string | null {
// - Get visitor code from `input` object
// `input` is of type `unknown`, so you can provide any structure.
// In Example, we assume `input` is a `Map` object.
const visitorCode = input.get(key);
if (visitorCode) {
return visitorCode;
}
// - Return `null` if no visitor code was found
return null;
}
public setData({
visitorCode,
output,
domain,
maxAge,
key,
path,
}: SetDataCustomParametersType): void {
// - Set visitor code as a cookie to `output` object using provided parameters.
let resultCookie = `${key}=${visitorCode}; Max-Age=${maxAge}; Path=${path}`;
if (domain) {
resultCookie += `; Domain=${domain}`;
}
// - `output` is of type `unknown`, so you can provide any structure.
// In Example, we assume `output` is a `Map` object.
output.set(key, resultCookie);
}
}
// --- Create KameleoonClient ---
const client = new KameleoonClient({
siteCode: 'my_site_code',
externals: {
visitorCodeManager: new MyVisitorCodeManager(),
},
});
import { KameleoonClient } from '@kameleoon/nodejs-sdk';
// --- External Visitor Code Manager implementation ---
// - Example uses custom arbitrary `input` and `output` objects.
class MyVisitorCodeManager {
public getData({ input, key }) {
// - Get visitor code from `input` object
// `input` is of type `unknown`, so you can provide any structure
// In Example, we assume `input` is a `Map` object.
const visitorCode = input.get(key);
if (visitorCode) {
return visitorCode;
}
// - Return `null` if no visitor code was found
return null;
}
public setData({
visitorCode,
output,
domain,
maxAge,
key,
path,
}) {
// - Set visitor code as a cookie to `output` object using provided parameters.
let resultCookie = `${key}=${visitorCode}; Max-Age=${maxAge}; Path=${path}`;
if (domain) {
resultCookie += `; Domain=${domain}`;
}
// - `output` is of type `unknown`, so you can provide any structure.
// In Example, we assume `output` is a `Map` object.
output.set(key, resultCookie);
}
}
// --- Create KameleoonClient ---
const client = new KameleoonClient({
siteCode: 'my_site_code',
externals: {
visitorCodeManager: new MyVisitorCodeManager(),
},
});
Requester
- TypeScript
- JavaScript
import {
RequestType,
IExternalRequester,
KameleoonResponseType,
SendRequestParametersType,
KameleoonClient,
} from '@kameleoon/nodejs-sdk';
// --- External Requester Implementation
export class MyRequester implements IExternalRequester {
public async sendRequest({
url,
parameters,
}: SendRequestParametersType<RequestType>): Promise<KameleoonResponseType> {
// - Using native NodeJS `fetch` (for v18+)
return await fetch(url, parameters);
}
}
// --- Create KameleoonClient ---
const client = new KameleoonClient({
siteCode: 'my_site_code',
externals: {
requester: new MyRequester(),
},
});
import { KameleoonClient } from '@kameleoon/nodejs-sdk';
// --- External Requester Implementation
export class MyRequester {
async sendRequest({ url, parameters }) {
// - Using native NodeJS `fetch` (for v18+)
return await fetch(url, parameters);
}
}
// --- Create KameleoonClient ---
const client = new KameleoonClient({
siteCode: 'my_site_code',
externals: {
requester: new MyRequester(),
},
});
Logger
- TypeScript
- JavaScript
import { KameleoonClient, KameleoonLogger, IExternalLogger, LogLevel } from '@kameleoon/nodejs-sdk';
// --- Custom Logger Implementation
export class CustomLogger implements IExternalLogger {
public log(level: LogLevel, message: string): void {
// Custom log handling logic here.
}
}
// --- Create KameleoonClient ---
const client = new KameleoonClient({
siteCode: 'my_site_code',
externals: {
logger: new CustomLogger(),
},
});
import { KameleoonClient } from '@kameleoon/nodejs-sdk';
// --- Custom Logger Implementation
export class CustomLogger {
log(level, message) {
// Custom log handling logic here.
}
}
// --- Create KameleoonClient ---
const client = new KameleoonClient({
siteCode: 'my_site_code',
externals: {
logger: new CustomLogger(),
},
});
Utilities
The SDK has a set of utility methods that you can use to simplify development. All methods are represented as static members of the KameleoonUtils
class.
- SDK Version 4
- SDK Version 5
getClientConfigurationUrl
Use the getClientConfigurationUrl
method to get the URL for fetching the client configuration. This method is useful for handling edge cases when working with Edge computing integrations.
This method will soon be deprecated and replaced by custom Requester
implementation.
- TypeScript
- JavaScript
import { KameleoonUtils, Environment } from '@kameleoon/nodejs-sdk';
const url = KameleoonUtils.getClientConfigurationUrl(
'my_site_code',
Environment.Production,
'example.com',
);
import { KameleoonUtils, Environment } from '@kameleoon/nodejs-sdk';
const url = KameleoonUtils.getClientConfigurationUrl(
'my_site_code',
Environment.Production,
'example.com',
);
Parameters
Name | Type | Description | Default |
---|---|---|---|
siteCode (required) | string | site code | - |
environment (optional) | Environment | an optional parameter specifying the SDK environment | Environment.Production |
networkDomain (optional) | string | an optional parameter specifying the domain for instances using custom domains. Must be in form example.com | kameleoon.com |
Returns
string
- returns the URL for fetching the client configuration
simulateSuccessRequest
Use the simulateSuccessRequest
method to simulate a successful request to the Kameleoon server. This method can be useful for custom Requester implementations when a developer needs to simulate a successful request.
- TypeScript
- JavaScript
import {
KameleoonUtils,
IExternalRequester,
SendRequestParametersType,
RequestType,
KameleoonResponseType,
} from '@kameleoon/nodejs-sdk';
// - Example of `Requester` with disabled tracking
class Requester implements IExternalRequester {
public async sendRequest({
url,
parameters,
requestType,
}: SendRequestParametersType<RequestType>): Promise<KameleoonResponseType> {
if (requestType === RequestType.Tracking) {
return KameleoonUtils.simulateSuccessRequest<RequestType.Tracking>(
requestType,
null,
);
}
return await fetch(url, parameters);
}
}
import { KameleoonUtils } from '@kameleoon/nodejs-sdk';
// - Example of `Requester` with disabled tracking
class Requester {
async sendRequest({ url, parameters, requestType }) {
if (requestType === RequestType.Tracking) {
return KameleoonUtils.simulateSuccessRequest(requestType, null);
}
return await fetch(url, parameters);
}
}
Parameters
Name | Type | Description |
---|---|---|
requestType (required) | RequestType | A type of request |
data (required) | SimulateRequestDataType[RequestType] | A type of request data, which is different depending on RequestType |
The SimulateRequestDataType
data type is defined as follows:
RequestType.Tracking
-null
RequestType.ClientConfiguration
-ClientConfigurationDataType
RequestType.RemoteData
-JSONType
Returns
Promise<KameleoonResponseType>
- returns a promise with the request's response
getCookieValue
Use the getCookieValue
method to parse a common cookie string (key_1=value_1; key_2=value_2; ...
), and get the value of a specific cookie key. This method is useful when working with a custom implementation of VisitorCodeManager
.
- TypeScript
- JavaScript
import { KameleoonUtils } from '@kameleoon/nodejs-sdk';
const cookies = 'key_1=value_1; key_2=value_2';
const key = 'key_1';
const value = KameleoonUtils.getCookieValue(cookies, key); // = `value_1`
import { KameleoonUtils } from '@kameleoon/nodejs-sdk';
const cookies = 'key_1=value_1; key_2=value_2';
const key = 'key_1';
const value = KameleoonUtils.getCookieValue(cookies, key); // = `value_1`
Parameters
Name | Type | Description |
---|---|---|
cookie (required) | string | Cookie string in form key_1=value_1; key_2=value_2 |
key (required) | string | String representation of a key to find a value by |
Returns
string | null
- returns a string with a cookie value, or null
if the key was not found
Reference
This is the full reference documentation for the Kameleoon JavaScript SDK.
Initialization
initialize()
An asynchronous method for initializing KameleoonClient
that retrieves Kameleoon SDK data either from the server or a local source if the data is still up-to-date or the update interval has not yet elapsed.
- TypeScript
- JavaScript
import {
KameleoonError,
KameleoonClient,
KameleoonException,
} from '@kameleoon/nodejs-sdk';
import { KameleoonVisitorCodeManager } from '@kameleoon/nodejs-visitor-code-manager';
import { KameleoonEventSource } from '@kameleoon/nodejs-event-source';
import { KameleoonRequester } from '@kameleoon/nodejs-requester';
const client = new KameleoonClient({
siteCode: 'my_site_code',
credentials: { clientId: 'my_client_id', clientSecret: 'my_client_secret' },
externals: {
visitorCodeManager: new KameleoonVisitorCodeManager(),
eventSource: new KameleoonEventSource(),
requester: new KameleoonRequester(),
},
});
async function init(): Promise<void> {
try {
await client.initialize();
} catch (err) {
if (err instanceof KameleoonError) {
switch (err.type) {
case KameleoonException.StorageWrite:
// -- Handle error case
case KameleoonException.ClientConfiguration:
// -- Handle error case
default:
break;
}
}
}
}
init();
import { KameleoonClient, KameleoonException } from '@kameleoon/nodejs-sdk';
import { KameleoonVisitorCodeManager } from '@kameleoon/nodejs-visitor-code-manager';
import { KameleoonEventSource } from '@kameleoon/nodejs-event-source';
import { KameleoonRequester } from '@kameleoon/nodejs-requester';
const client = new KameleoonClient({
siteCode: 'my_site_code',
credentials: { clientId: 'my_client_id', clientSecret: 'my_client_secret' },
externals: {
visitorCodeManager: new KameleoonVisitorCodeManager(),
eventSource: new KameleoonEventSource(),
requester: new KameleoonRequester(),
},
});
async function init() {
try {
await client.initialize();
} catch (err) {
switch (err.type) {
case KameleoonException.StorageWrite:
// -- Handle error case
case KameleoonException.ClientConfiguration:
// -- Handle error case
default:
break;
}
}
}
init();
Returns
Promise<boolean>
- A promise resolved to a boolean indicating the SDK's successful initialization. Generally, initialize() will throw an error if an unhandled issue occurs, so the boolean
value will almost always be true
and may not provide much useful information.
Throws
Type | Description |
---|---|
KameleoonException.StorageWrite | Couldn't update storage data |
KameleoonException.ClientConfiguration | Couldn't retrieve client configuration from Kameleoon API |
KameleoonException.MaximumRetriesReached | Maximum retries reached, request failed |
Feature flags and variations
getVariation()
- 📨 Sends Tracking Data to Kameleoon (depending on the
track
parameter) - 🎯 Events:
EventType.Evaluation
Retrieves the VariationType
assigned to a given visitor for a specific feature flag.
This method takes a visitorCode
and featureKey
as mandatory arguments. The track
argument is optional and defaults to true
.
It returns the assigned VariationType
for the visitor. If the visitor is not associated with any feature flag rules, the method returns the default VariationType
for the given feature flag.
Ensure that proper error handling is implemented in your code to manage potential exceptions.
The default variation refers to the variation assigned to a visitor when they do not match any predefined delivery rules for a feature flag. In other words, it is the fallback variation applied to all users who are not targeted by specific rules. It's represented as the variation in the "Then, for everyone else..." section in a management interface.
- TypeScript
- JavaScript
import { KameleoonClient } from '@kameleoon/nodejs-sdk';
import { KameleoonVisitorCodeManager } from '@kameleoon/nodejs-visitor-code-manager';
import { KameleoonEventSource } from '@kameleoon/nodejs-event-source';
import { KameleoonRequester } from '@kameleoon/nodejs-requester';
const client = new KameleoonClient({
siteCode: 'my_site_code',
credentials: { clientId: 'my_client_id', clientSecret: 'my_client_secret' },
externals: {
visitorCodeManager: new KameleoonVisitorCodeManager(),
eventSource: new KameleoonEventSource(),
requester: new KameleoonRequester(),
},
});
async function init(): Promise<void> {
await client.initialize();
// -- Get visitor code
const visitorCode = client.getVisitorCode();
// -- Get variation with tracking
const variation = client.getVariation({
visitorCode,
featureKey: 'my_feature_key',
});
// -- Get variation without tracking
const variation = client.getVariation({
visitorCode,
featureKey: 'my_feature_key',
track: false,
});
// -- An Example variation:
// {
// key: 'variation_key',
// id: 123,
// experimentId: 456,
// variables: Map {
// 'variable_key' => {
// key: 'variable_key',
// type: VariableType.BOOLEAN,
// value: true,
// }
// },
// }
}
init();
import { KameleoonClient } from '@kameleoon/nodejs-sdk';
import { KameleoonVisitorCodeManager } from '@kameleoon/nodejs-visitor-code-manager';
import { KameleoonEventSource } from '@kameleoon/nodejs-event-source';
import { KameleoonRequester } from '@kameleoon/nodejs-requester';
const client = new KameleoonClient({
siteCode: 'my_site_code',
credentials: { clientId: 'my_client_id', clientSecret: 'my_client_secret' },
externals: {
visitorCodeManager: new KameleoonVisitorCodeManager(),
eventSource: new KameleoonEventSource(),
requester: new KameleoonRequester(),
},
});
async function init() {
await client.initialize();
// -- Get visitor code
const visitorCode = client.getVisitorCode();
// -- Get variation with tracking
const variation = client.getVariation({
visitorCode,
featureKey: 'my_feature_key',
});
// -- Get variation without tracking
const variation = client.getVariation({
visitorCode,
featureKey: 'my_feature_key',
track: false,
});
// -- An Example variation:
// {
// key: 'variation_key',
// id: 123,
// experimentId: 456,
// variables: Map {
// 'variable_key' => {
// key: 'variable_key',
// type: VariableType.BOOLEAN,
// value: true,
// }
// },
// }
}
init();
Parameters
An object of type GetVariationParamsType
with the following properties:
Name | Type | Description | Default |
---|---|---|---|
visitorCode (required) | string | Unique identifier of the user. | |
featureKey (required) | string | Key of the feature you want to expose to a user. | |
track (optional) | boolean | An optional parameter to enable or disable tracking of the feature evaluation. | true |
Return value
Type | Description |
---|---|
Variation | An assigned VariationType to a given visitor for a specific feature flag. |
Exceptions thrown
Type | Description |
---|---|
KameleoonException.Initialization | Method was executed before the kameleoonClient completed it's initialize call. |
KameleoonException.VisitorCodeEmpty | The visitor code is empty. |
KameleoonException.VisitorCodeMaxLength | The visitor code exceeded the maximum length (255 characters). |
KameleoonException.FeatureFlagConfigurationNotFound | Exception indicating that the requested feature key wasn't found in the internal configuration of the SDK. This usually means that the feature flag is not activated in the Kameleoon app (but code implementing the feature is already deployed on your application). |
KameleoonException.FeatureFlagEnvironmentDisabled | Exception indicating that feature flag is disabled for the visitor's current environment (for example, production, staging, or development). |
getVariations()
- 📨 Sends Tracking Data to Kameleoon (depending on the
track
parameter) - 🎯 Events:
EventType.Evaluation
Retrieves a map of VariationType
objects assigned to a given visitor across all feature flags.
This method iterates over all available feature flags and returns the assigned VariationType
for each flag associated with the specified visitor. It takes visitorCode
as a mandatory argument, while onlyActive
and track
are optional.
- If
onlyActive
is set totrue
, the methodgetVariations()
will return feature flags variations provided the user is not bucketed with theoff
variation. - The
track
parameter controls whether or not the method will track the variation assignments. By default, it is set totrue
. If set tofalse
, the tracking will be disabled.
The returned map consists of feature flag keys as keys and their corresponding VariationType
as values. If no variation is assigned for a feature flag, the method returns the default VariationType
for that flag.
Proper error handling should be implemented to manage potential exceptions.
The default variation refers to the variation assigned to a visitor when they do not match any predefined delivery rules for a feature flag. In other words, it is the fallback variation applied to all users who are not targeted by specific rules. It's represented as the variation in the "Then, for everyone else..." section in a management interface.
- TypeScript
- JavaScript
import { KameleoonClient } from '@kameleoon/nodejs-sdk';
import { KameleoonVisitorCodeManager } from '@kameleoon/nodejs-visitor-code-manager';
import { KameleoonEventSource } from '@kameleoon/nodejs-event-source';
import { KameleoonRequester } from '@kameleoon/nodejs-requester';
const client = new KameleoonClient({
siteCode: 'my_site_code',
credentials: { clientId: 'my_client_id', clientSecret: 'my_client_secret' },
externals: {
visitorCodeManager: new KameleoonVisitorCodeManager(),
eventSource: new KameleoonEventSource(),
requester: new KameleoonRequester(),
},
});
async function init(): Promise<void> {
await client.initialize();
// -- Get visitor code
const visitorCode = client.getVisitorCode();
// -- Get all feature flag variations with tracking
const variations = client.getVariations({
visitorCode,
});
// -- Get active feature flag variations with tracking
const variations = client.getVariations({
visitorCode,
onlyActive: true,
});
// -- Get active feature flag variations without tracking
const variations = client.getVariations({
visitorCode,
onlyActive: true,
track: false,
});
// -- An Example variations:
// Map {
// 'feature_key' => {
// key: 'variation_key',
// id: 123,
// experimentId: 456,
// variables: Map {
// 'variable_key' => {
// key: 'variable_key',
// type: VariableType.BOOLEAN,
// value: true,
// }
// },
// }
// }
}
init();
import { KameleoonClient } from '@kameleoon/nodejs-sdk';
import { KameleoonVisitorCodeManager } from '@kameleoon/nodejs-visitor-code-manager';
import { KameleoonEventSource } from '@kameleoon/nodejs-event-source';
import { KameleoonRequester } from '@kameleoon/nodejs-requester';
const client = new KameleoonClient({
siteCode: 'my_site_code',
credentials: { clientId: 'my_client_id', clientSecret: 'my_client_secret' },
externals: {
visitorCodeManager: new KameleoonVisitorCodeManager(),
eventSource: new KameleoonEventSource(),
requester: new KameleoonRequester(),
},
});
async function init() {
await client.initialize();
// -- Get visitor code
const visitorCode = client.getVisitorCode();
// -- Get all feature flag variations with tracking
const variations = client.getVariations({
visitorCode,
});
// -- Get active feature flag variations with tracking
const variations = client.getVariations({
visitorCode,
onlyActive: true,
});
// -- Get active feature flag variations without tracking
const variations = client.getVariations({
visitorCode,
onlyActive: true,
track: false,
});
// -- An Example variations:
// Map {
// 'feature_key' => {
// key: 'variation_key',
// id: 123,
// experimentId: 456,
// variables: Map {
// 'variable_key' => {
// key: 'variable_key',
// type: VariableType.BOOLEAN,
// value: true,
// }
// },
// }
// }
}
init();
Arguments
An object of type GetVariationsParamsType
with the following properties:
Name | Type | Description | Default |
---|---|---|---|
visitorCode (required) | string | Unique identifier of the user. | |
onlyActive (optional) | boolean | An optional parameter indicating whether to return variations for active (true ) or all (false ) feature flags. | false |
track (optional) | boolean | An optional parameter to enable or disable tracking of the feature evaluation. | true |
Return value
Type | Description |
---|---|
Map<string, VariationType> | Map that contains the assigned VariationType objects of the feature flags using the keys of the corresponding features. |
Exceptions thrown
Type | Description |
---|---|
KameleoonException.Initialization | Method was executed before the kameleoonClient completed it's initialize call. |
KameleoonException.VisitorCodeEmpty | The visitor code is empty. |
KameleoonException.VisitorCodeMaxLength | The visitor code exceeded the maximum length (255 characters). |
isFeatureFlagActive()
- 📨 Sends Tracking Data to Kameleoon (depending on the
track
parameter) - 🎯 Events:
EventType.Evaluation
The isFeatureFlagActive()
method returns a boolean indicating whether the visitor with visitorCode
has an active featureKey
. This method checks for targeting, finds the variation for the visitor, and saves it to storage. The method also sends a tracking request.
This method has an additional overload that lets you pass a track
parameter, which disables the tracking of feature evaluation.
A visitor must be targeted for feature flags to activate.
- TypeScript
- JavaScript
import { KameleoonClient, CustomData } from '@kameleoon/nodejs-sdk';
import { KameleoonVisitorCodeManager } from '@kameleoon/nodejs-visitor-code-manager';
import { KameleoonEventSource } from '@kameleoon/nodejs-event-source';
import { KameleoonRequester } from '@kameleoon/nodejs-requester';
const client = new KameleoonClient({
siteCode: 'my_site_code',
credentials: { clientId: 'my_client_id', clientSecret: 'my_client_secret' },
externals: {
visitorCodeManager: new KameleoonVisitorCodeManager(),
eventSource: new KameleoonEventSource(),
requester: new KameleoonRequester(),
},
});
async function init(): Promise<void> {
await client.initialize();
// -- Get visitor code using server `request` and `response`
const visitorCode = client.getVisitorCode({
request: req,
response: res,
});
// -- Add CustomData with index `0` containing visitor id to check the targeting
client.addData(visitorCode, new CustomData(0, 'visitor_id'));
// -- Check if the feature flag is active for visitor
const isActive = client.isFeatureFlagActive(visitorCode, 'my_feature_key');
// -- Check if the feature flag is active for visitor without tracking
const isActive = client.isFeatureFlagActive({ visitorCode, featureKey: 'my_feature, track: false});
}
init();
import { KameleoonClient, CustomData } from '@kameleoon/nodejs-sdk';
import { KameleoonVisitorCodeManager } from '@kameleoon/nodejs-visitor-code-manager';
import { KameleoonEventSource } from '@kameleoon/nodejs-event-source';
import { KameleoonRequester } from '@kameleoon/nodejs-requester';
const client = new KameleoonClient({
siteCode: 'my_site_code',
credentials: { clientId: 'my_client_id', clientSecret: 'my_client_secret' },
externals: {
visitorCodeManager: new KameleoonVisitorCodeManager(),
eventSource: new KameleoonEventSource(),
requester: new KameleoonRequester(),
},
});
async function init() {
await client.initialize();
// -- Get visitor code using server `request` and `response`
const visitorCode = client.getVisitorCode({
request: req,
response: res,
});
// -- Add CustomData with index `0` containing visitor id to check the targeting
client.addData(visitorCode, new CustomData(0, 'visitor_id'));
// -- Check if the feature flag is active for visitor
const isActive = client.isFeatureFlagActive(visitorCode, 'my_feature_key');
// -- Check if the feature flag is active for visitor without tracking
const isActive = client.isFeatureFlagActive({ visitorCode, featureKey: 'my_feature, track: false});
}
init();
Parameters
There are two overloads available for this method:
- Two parameters overload:
This overload is deprecated and will be removed in the next major version. Please use the new overload with an object parameter.
Name | Type | Description |
---|---|---|
visitorCode (required) | string | unique visitor identification string, can't exceed 255 characters length |
featureKey (required) | string | a unique key for feature flag |
- Object parameter overload of type
IsFeatureFlagActiveParamsType
:
Name | Type | Description | Default |
---|---|---|---|
visitorCode (required) | string | unique visitor identification string, can't exceed 255 characters in length | - |
featureKey (required) | string | a unique key for a feature flag | - |
track (optional) | boolean | a boolean indicator of whether to track the feature evaluation | true |
Returns
boolean
- a boolean indicator of whether the feature flag with featureKey
is active for the visitor with visitorCode
Throws
Type | Description |
---|---|
KameleoonException.Initialization | Method was executed before the kameleoonClient completed its initialize call |
KameleoonException.VisitorCodeMaxLength | The visitor code exceeded the maximum length (255 characters) |
KameleoonException.VisitorCodeEmpty | The visitor code is empty |
KameleoonException.NotTargeted | Current visitor is not targeted |
KameleoonException.FeatureFlagConfigurationNotFound | No feature flag was found for provided featureKey |
KameleoonException.FeatureFlagVariableNotFound | No feature variable were found for provided visitorCode and variableKey |
KameleoonException.DataInconsistency | Allocated variation was found, but there is no feature flag with the according featureKey . |
getFeatureFlagVariationKey()
- 📨 Sends Tracking Data to Kameleoon
- 🎯 Events:
EventType.Evaluation
This method is deprecated and will be removed in the next major update. Please use the new method getVariation
.
The getFeatureFlagVariationKey()
method retrieves the variation key for the specified visitorCode
in the corresponding feature flag. This method includes a targeting check, finding the appropriate variation exposed to the visitor, saving it to storage, and sending a tracking request.
If a user has not been previously assigned a variation key for the feature flag, the SDK will randomly determine a variation based on the feature flag's rules. If the user is already linked to the feature flag, the SDK will return their previously assigned variation key. If the user does not meet any of the specified rules, the default value defined in Kameleoon's feature flag delivery rules will be returned. This default value is not always a variation key—it can also be a boolean or another data type, depending on the feature flag's configuration.
- TypeScript
- JavaScript
import { KameleoonClient, CustomData } from '@kameleoon/nodejs-sdk';
import { KameleoonVisitorCodeManager } from '@kameleoon/nodejs-visitor-code-manager';
import { KameleoonEventSource } from '@kameleoon/nodejs-event-source';
import { KameleoonRequester } from '@kameleoon/nodejs-requester';
const client = new KameleoonClient({
siteCode: 'my_site_code',
credentials: { clientId: 'my_client_id', clientSecret: 'my_client_secret' },
externals: {
visitorCodeManager: new KameleoonVisitorCodeManager(),
eventSource: new KameleoonEventSource(),
requester: new KameleoonRequester(),
},
});
async function init(): Promise<void> {
await client.initialize();
// -- Get visitor code using server `request` and `response`
const visitorCode = client.getVisitorCode({
request: req,
response: res,
});
// -- Add CustomData with index `0` containing visitor id to check the targeting
client.addData(new CustomData(0, 'visitor_id'));
// -- Get visitor feature flag variation key
const variationKey = client.getFeatureFlagVariationKey(
visitorCode,
'my_feature_key',
);
}
init();
import { KameleoonClient, CustomData } from '@kameleoon/nodejs-sdk';
import { KameleoonVisitorCodeManager } from '@kameleoon/nodejs-visitor-code-manager';
import { KameleoonEventSource } from '@kameleoon/nodejs-event-source';
import { KameleoonRequester } from '@kameleoon/nodejs-requester';
const client = new KameleoonClient({
siteCode: 'my_site_code',
credentials: { clientId: 'my_client_id', clientSecret: 'my_client_secret' },
externals: {
visitorCodeManager: new KameleoonVisitorCodeManager(),
eventSource: new KameleoonEventSource(),
requester: new KameleoonRequester(),
},
});
async function init() {
await client.initialize();
// -- Get visitor code using server `request` and `response`
const visitorCode = KameleoonUtils.getVisitorCode({
request: req,
response: res,
});
// -- Add CustomData with index `0` containing visitor id to check the targeting
client.addData(new CustomData(0, 'visitor_id'));
// -- Get visitor feature flag variation key
const variationKey = client.getFeatureFlagVariationKey(
visitorCode,
'my_feature_key',
);
}
init();
Parameters
Name | Type | Description |
---|---|---|
visitorCode (required) | string | unique visitor identification string, can't exceed 255 characters in length |
featureKey (required) | string | a unique key for a feature flag |
Returns
string
— a string containing the variable key for the visitor's allocated feature flag.
Throws
Type | Description |
---|---|
KameleoonException.Initialization | Method was executed before initialize was completed for kameleoonClient |
KameleoonException.VisitorCodeMaxLength | The visitor code exceeded the maximum length (255 characters) |
KameleoonException.VisitorCodeEmpty | The visitor code is empty |
KameleoonException.NotTargeted | Current visitor is not targeted |
KameleoonException.FeatureFlagConfigurationNotFound | No feature flag was found for the specified featureKey |
KameleoonException.FeatureFlagEnvironmentDisabled | Feature flag is disabled for the current environment |
getFeatureFlags()
🚫 Doesn't send Tracking Data to Kameleoon
This method is deprecated and will be removed in the next major update. Please use the new getVariations
method.
The getFeatureFlags()
method retrieves a list of feature flags that are stored in the client configuration.
- TypeScript
- JavaScript
import { KameleoonClient } from '@kameleoon/nodejs-sdk';
import { KameleoonVisitorCodeManager } from '@kameleoon/nodejs-visitor-code-manager';
import { KameleoonEventSource } from '@kameleoon/nodejs-event-source';
import { KameleoonRequester } from '@kameleoon/nodejs-requester';
const client = new KameleoonClient({
siteCode: 'my_site_code',
credentials: { clientId: 'my_client_id', clientSecret: 'my_client_secret' },
externals: {
visitorCodeManager: new KameleoonVisitorCodeManager(),
eventSource: new KameleoonEventSource(),
requester: new KameleoonRequester(),
},
});
async function init(): Promise<void> {
await client.initialize();
// -- Get all feature flags
const featureFlags = client.getFeatureFlags();
}
init();
import { KameleoonClient } from '@kameleoon/nodejs-sdk';
import { KameleoonVisitorCodeManager } from '@kameleoon/nodejs-visitor-code-manager';
import { KameleoonEventSource } from '@kameleoon/nodejs-event-source';
import { KameleoonRequester } from '@kameleoon/nodejs-requester';
const client = new KameleoonClient({
siteCode: 'my_site_code',
credentials: { clientId: 'my_client_id', clientSecret: 'my_client_secret' },
externals: {
visitorCodeManager: new KameleoonVisitorCodeManager(),
eventSource: new KameleoonEventSource(),
requester: new KameleoonRequester(),
},
});
async function init() {
await client.initialize();
// -- Get all feature flags
const featureFlags = client.getFeatureFlags();
}
init();
Returns
FeatureFlagType[]
- list of feature flags, each feature flag item contains id
and key
Throws
Type | Description |
---|---|
KameleoonException.Initialization | Method was executed before the kameleoonClient completed its initialize call |
getVisitorFeatureFlags()
This method is deprecated and will be removed in the next major update. Please use the new getVariations
method
The getVisitorFeatureFlags()
method returns a list of feature flags that are active for the visitor with the specified visitorCode
, ensuring that the visitor is allocated one of the variations.
- 🚫 Doesn't send Tracking Data to Kameleoon
- 🎯 Events:
EventType.Evaluation
(for each feature flag)
- TypeScript
- JavaScript
import { KameleoonClient } from '@kameleoon/nodejs-sdk';
import { KameleoonVisitorCodeManager } from '@kameleoon/nodejs-visitor-code-manager';
import { KameleoonEventSource } from '@kameleoon/nodejs-event-source';
import { KameleoonRequester } from '@kameleoon/nodejs-requester';
const client = new KameleoonClient({
siteCode: 'my_site_code',
credentials: { clientId: 'my_client_id', clientSecret: 'my_client_secret' },
externals: {
visitorCodeManager: new KameleoonVisitorCodeManager(),
eventSource: new KameleoonEventSource(),
requester: new KameleoonRequester(),
},
});
async function init(): Promise<void> {
await client.initialize();
// -- Get visitor code using server `request` and `response`
const visitorCode = client.getVisitorCode({
request: req,
response: res,
});
// -- Get active feature flags for visitor
const featureFlags = client.getVisitorFeatureFlags(visitorCode);
}
init();
import { KameleoonClient } from '@kameleoon/nodejs-sdk';
import { KameleoonVisitorCodeManager } from '@kameleoon/nodejs-visitor-code-manager';
import { KameleoonEventSource } from '@kameleoon/nodejs-event-source';
import { KameleoonRequester } from '@kameleoon/nodejs-requester';
const client = new KameleoonClient({
siteCode: 'my_site_code',
credentials: { clientId: 'my_client_id', clientSecret: 'my_client_secret' },
externals: {
visitorCodeManager: new KameleoonVisitorCodeManager(),
eventSource: new KameleoonEventSource(),
requester: new KameleoonRequester(),
},
});
async function init() {
await client.initialize();
// -- Get visitor code using server `request` and `response`
const visitorCode = client.getVisitorCode({
request: req,
response: res,
});
// -- Get active feature flags for visitor
const featureFlags = client.getVisitorFeatureFlags(visitorCode);
}
init();
This method only collects the visitor's active feature flags, meaning the result excludes all feature flags for which the visitor is assigned the off
(default or control) variation.
For example:
// -- `getVisitorFeatureFlags` doesn't trigger feature experiments;
// it only returns feature flags where visitors didn't get the `off` variation.
client.getVisitorFeatureFlags('my_visitor').forEach(({ key }) => {
// -- `getFeatureFlagVariationKey` triggers a feature experiment,
// as `off` is already filtered out - visitors will never take part
// in an experiment where the `off` variation was allocated.
client.getFeatureFlagVariationKey('my_visitor', key);
});
Use getFeatureFlags
when you need all of the visitor's feature flags:
// -- Both `off` and other variations are processed as expected.
client.getFeatureFlags('my_visitor').forEach(({ key }) => {
client.getFeatureFlagVariationKey('my_visitor', key);
});
Parameters
Name | Type | Description |
---|---|---|
visitorCode (required) | string | unique visitor identification string, can't exceed 255 characters in length |
Returns
FeatureFlagType[]
- list of feature flags, each feature flag item contains id
and key
Throws
Type | Description |
---|---|
KameleoonException.Initialization | Method was executed before the kameleoonClient completed its initialize call |
KameleoonException.VisitorCodeMaxLength | The visitor code exceeded the maximum length (255 characters) |
KameleoonException.VisitorCodeEmpty | The visitor code is empty |
KameleoonException.NotTargeted | Current visitor is not targeted |
KameleoonException.StorageRead | Error while reading storage data |
getActiveFeatureFlags()
- 🚫 Doesn't send Tracking Data to Kameleoon
- 🎯 Events:
EventType.Evaluation
(for each feature flag)
This method is deprecated and will be removed in the next major update. Use the new getVariations
method instead.
The getActiveFeatureFlags()
method returns a Map
, where the key represents the feature key, and the value contains detailed information about the visitor’s variation and its variables.
- TypeScript
- JavaScript
import { KameleoonClient } from '@kameleoon/nodejs-sdk';
import { KameleoonVisitorCodeManager } from '@kameleoon/nodejs-visitor-code-manager';
import { KameleoonEventSource } from '@kameleoon/nodejs-event-source';
import { KameleoonRequester } from '@kameleoon/nodejs-requester';
const client = new KameleoonClient({
siteCode: 'my_site_code',
credentials: { clientId: 'my_client_id', clientSecret: 'my_client_secret' },
externals: {
visitorCodeManager: new KameleoonVisitorCodeManager(),
eventSource: new KameleoonEventSource(),
requester: new KameleoonRequester(),
},
});
async function init(): Promise<void> {
await client.initialize();
// -- Get visitor code
const visitorCode = client.getVisitorCode();
// -- Get active feature flags for visitor
// with detailed variation and variables data
const activeFeatures = client.getActiveFeatureFlags(visitorCode);
// -- Result example:
// Map {
// 'feature-key-one' => {
// id: 100,
// key: 'variation-key-one',
// experimentId: 200,
// variables: [
// { key: 'variable_bool', type: VariableType.Boolean, value: true },
// ]
// },
// 'feature-key-two' => {
// id: null, // -> `null` because it is default variation
// key: 'default-variation-key',
// experimentId: null, // -> `null` because it is default variation
// variables: []
// }
// }
}
init();
import { KameleoonClient } from '@kameleoon/nodejs-sdk';
import { KameleoonVisitorCodeManager } from '@kameleoon/nodejs-visitor-code-manager';
import { KameleoonEventSource } from '@kameleoon/nodejs-event-source';
import { KameleoonRequester } from '@kameleoon/nodejs-requester';
const client = new KameleoonClient({
siteCode: 'my_site_code',
credentials: { clientId: 'my_client_id', clientSecret: 'my_client_secret' },
externals: {
visitorCodeManager: new KameleoonVisitorCodeManager(),
eventSource: new KameleoonEventSource(),
requester: new KameleoonRequester(),
},
});
async function init() {
await client.initialize();
// -- Get visitor code
const visitorCode = client.getVisitorCode();
// -- Get active feature flags for visitor
// with detailed variation and variables data
const activeFeatures = client.getActiveFeatureFlags(visitorCode);
// -- Result example:
// Map {
// 'feature-key-one' => {
// id: 100,
// key: 'variation-key-one',
// experimentId: 200,
// variables: [
// { key: 'variable_bool', type: VariableType.Boolean, value: true },
// ]
// },
// 'feature-key-two' => {
// id: null, // -> `null` because it is default variation
// key: 'default-variation-key',
// experimentId: null, // -> `null` because it is default variation
// variables: []
// }
// }
}
init();
This method only collects the visitor's active feature flags, meaning the result excludes all feature flags for which the visitor is assigned the off
(default or control) variation.
See the getVisitorFeatureFlags method's CAUTION section for more details.
Parameters
Name | Type | Description |
---|---|---|
visitorCode (required) | string | unique visitor identification string, can't exceed 255 characters in length |
Returns
Map<string, KameleoonVariationType>
- a map of feature flags, where key is feature key and value is detailed information about the visitor's variation and its variables
Throws
Type | Description |
---|---|
KameleoonException.Initialization | Method was executed before the kameleoonClient completed its initialize call |
KameleoonException.VisitorCodeMaxLength | The visitor code exceeded the maximum length (255 characters) |
KameleoonException.VisitorCodeEmpty | The visitor code is empty |
KameleoonException.StorageRead | Error while reading storage data |
KameleoonException.NumberParse | Couldn't parse Number value |
KameleoonException.JSONParse | Couldn't parse JSON value |
setForcedVariation()
The method allows you to programmatically assign a specific VariationType
to a user, bypassing the standard evaluation process. This is especially valuable for controlled experiments where the usual evaluation logic is not required or must be skipped. It can also be helpful in scenarios like debugging or custom testing.
When a forced variation is set, it overrides Kameleoon's real-time evaluation logic. Processes like segmentation, targeting conditions, and algorithmic calculations are skipped. To preserve segmentation and targeting conditions during an experiment, set forceTargeting=false
instead.
Simulated variations always take precedence in the execution order. If a simulated variation calculation is triggered, it will be fully processed and completed first.
A forced variation is treated the same as an evaluated variation. It is tracked in analytics and stored in the user context like any standard evaluated variation, ensuring consistency in reporting.
The method may throw exceptions under certain conditions (e.g., invalid parameters, user context, or internal issues). Proper exception handling is essential to ensure that your application remains stable and resilient.
It’s important to distinguish forced variations from simulated variations:
- Forced variations: Are specific to an individual experiment.
- Simulated variations: Affect the overall feature flag result.
- TypeScript
- JavaScript
import { KameleoonClient } from '@kameleoon/nodejs-sdk';
import { KameleoonVisitorCodeManager } from '@kameleoon/nodejs-visitor-code-manager';
import { KameleoonEventSource } from '@kameleoon/nodejs-event-source';
import { KameleoonRequester } from '@kameleoon/nodejs-requester';
const client = new KameleoonClient({
siteCode: 'my_site_code',
credentials: { clientId: 'my_client_id', clientSecret: 'my_client_secret' },
externals: {
visitorCodeManager: new KameleoonVisitorCodeManager(),
eventSource: new KameleoonEventSource(),
requester: new KameleoonRequester(),
},
});
async function init(): Promise<void> {
await client.initialize();
// -- Get visitor code
const visitorCode = client.getVisitorCode();
// -- Forcing the variation "on" for the "featureKey1" feature flag for the visitor.
client.setForcedVariation({
visitorCode: visitorCode,
experimentId: 9516,
variationKey: 'on',
forceTargeting: false,
});
// -- Resetting the forced variation for the "featureKey1" feature flag for the visitor.
client.setForcedVariation({
visitorCode: visitorCode,
experimentId: 9516,
variationKey: null,
});
}
init();
import { KameleoonClient } from '@kameleoon/nodejs-sdk';
import { KameleoonVisitorCodeManager } from '@kameleoon/nodejs-visitor-code-manager';
import { KameleoonEventSource } from '@kameleoon/nodejs-event-source';
import { KameleoonRequester } from '@kameleoon/nodejs-requester';
const client = new KameleoonClient({
siteCode: 'my_site_code',
credentials: { clientId: 'my_client_id', clientSecret: 'my_client_secret' },
externals: {
visitorCodeManager: new KameleoonVisitorCodeManager(),
eventSource: new KameleoonEventSource(),
requester: new KameleoonRequester(),
},
});
async function init() {
await client.initialize();
// -- Get visitor code
const visitorCode = client.getVisitorCode();
// -- Forcing the variation "on" for the "featureKey1" feature flag for the visitor.
client.setForcedVariation({
visitorCode: visitorCode,
experimentId: 9516,
variationKey: 'on',
forceTargeting: false,
});
// -- Resetting the forced variation for the "featureKey1" feature flag for the visitor.
client.setForcedVariation({
visitorCode: visitorCode,
experimentId: 9516,
variationKey: null,
});
}
init();