Skip to main content

Node.js

This category contains topics explaining how to configure Bucketeer's Node.js SDK.

caution

The Node.js SDK is currently in its Beta stage. If you find any issues or have suggestions for improvement, feel free to open an issue.

Getting started

Before starting, ensure that you follow the Getting Started guide.

Installing dependency

Install the dependency in your application.

npm install @bucketeer/node-server-sdk

Importing SDK

Import the Bucketeer SDK into your application code.

import { initialize } from '@bucketeer/node-server-sdk';

Configuring client

Configure the SDK config and user configuration.

info

The tag setting is the tag you configure when creating a Feature Flag. It will evaluate all the Feature Flags in the environment when it is not configured.
We strongly recommend using tags to speed up the evaluation process and reduce the response latency.

const config = {
host: 'YOUR_API_URL',
token: 'YOUR_API_KEY',
tag: 'YOUR_TAG',
};
Custom configuration

Depending on your use, you may want to change the optional configurations available.

  • pollingIntervalForRegisterEvents (Default is 1 minute)
  • logger (Default is logger.DefaultLogger)

Initializing client

Initialize the client by passing the configurations in the previous step.

const client = initialize(config);

Supported features

Evaluating user

The variation method determines whether or not a feature flag is enabled for a specific user.
To check which variation a specific user will receive, you can use the client like below.

const showNewFeature = await client.getBoolVariation(
user: User({
id: 'USER_ID',
data: {}, // The user attributes are optional
}),
featureId: 'YOUR_FEATURE_FLAG_ID',
defaultValue: false
);
if (showNewFeature) {
// The Application code to show the new feature
} else {
// The code to run when the feature is off
}
note

The variation method will return the default value if the feature flag doesn't exist, or is archived, or if the request fails.

Variation types

The Bucketeer SDK supports the following variation types.

getStringVariation(user: User, featureId: string, defaultValue: string): Promise<string>;

getBoolVariation(user: User, featureId: string, defaultValue: boolean): Promise<boolean>;

getNumberVariation(user: User, featureId: string, defaultValue: number): Promise<number>;

getJsonVariation(user: User, featureId: string, defaultValue: object): Promise<object>;

Reporting custom events

This method lets you save user actions in your application as events. You can connect these events to metrics in the experiments or in the kill switch (auto operation) on the console UI.

In addition, you can pass a number value to the goal event. These values will sum and show as
Value total on the experiments console UI. This is useful if you have a goal event for tracking how much a user spent on your application buying items, etc.

client.track(
user: {
id: 'USER_ID',
data: {}, // The user attributes are optional
},
goalId: 'YOUR_GOAL_ID',
value: 10.50,
);

Destroy client

This method will send all pending analytics events to the Bucketeer server as soon as possible and stop the workers. The application should call this before the application stops. Otherwise, the remaining events can be lost.

await client.destroy();

User attributes configuration

This feature will give you robust and granular control over what users can see on your application. You can add rules using these attributes on the console UI's feature flag's targeting tab. See more.

import { User } from '@bucketeer/node-server-sdk';

const user = User({
id: 'USER_ID',
data: {
app_version: '1.0.0',
os_version: '11.0.0',
device_model: 'pixel-5',
language: 'english',
genre: 'female'
},
});
const showNewFeature = await client.getBoolVariation(user, 'YOUR_FEATURE_FLAG_ID', false);