molasseslogo

Browser JavaScript Sdk#

Molasses-JS includes the Browser (with TypeScript support) SDK for Molasses. It allows you to evaluate a user's status for a feature. It also helps simplify logging events for A/B testing.

Molasses-JS - Once initialized, it takes microseconds to evaluate if a user is active.

Install#

bash
1
npm install @molassesapp/molasses-js
2
# or
3
yarn add @molassesapp/molasses-js
4
# or
5
<script src="https://cdn.jsdelivr.net/npm/@molassesapp/molasses-js@0.8.0/dist/index.min.js"></script>

Usage#

To Require:

js
1
import { MolassesClient } from "@molassesapp/molasses-js";
2
// or
3
const { MolassesClient } = require("@molassesapp/molasses-js");
4
// or if using a script tag
5
var MolassesClient = MolassesJS.MolassesClient;

Start by initializing the client with an APIKey. If you decide not to track analytics events (experiment started, experiment success) you can turn them off by setting the sendEvents field to false

js
1
// Initialize with your API Key
2
const client = new MolassesClient({
3
APIKey: "testapikey",
4
});
5
6
// Then init which is a promise
7
await client.init();

Check if feature is active#

You can call isActive with the key name and optionally a user's information. The ID field is used to determine whether a user is part of a percentage of users and uniquely identies a user. If you have other user segments based on user parameterss you can pass those in the params field.

js
1
// Once initialized you can start calling isActive
2
client.isActive("FOO_TEST", {
3
id: "123",
4
params: {
5
isBetaUser: "true",
6
},
7
});

You can check if a feature is active for a user who is anonymous by just calling isActive with the key. You won't be able to do percentage roll outs or track that user's behavior.

js
1
client.isActive("FOO_TEST");

Experiments#

If you are not auto tracking experiments, to track whether an experiment has started you can call experimentStarted. experimentStarted takes the feature's name, the molasses User and any additional parameters for the event.

js
1
client.ExperimentSuccess(
2
"GOOGLE_SSO",
3
{
4
id: "baz",
5
params: {
6
teamId: "12356",
7
},
8
},
9
{
10
version: "v2.3.0",
11
}
12
);

To track whether an experiment was successful you can call ExperimentSuccess. ExperimentSuccess takes the feature's name, the molasses User and any additional parameters for the event.

js
1
client.ExperimentSuccess(
2
"GOOGLE_SSO",
3
{
4
id: "baz",
5
params: {
6
teamId: "12356",
7
},
8
},
9
{
10
version: "v2.3.0",
11
}
12
);

Methods#

new MolassesClient(options)#

Creates a new Molasses client. It takes a set of options.

Options

FieldRequiredTypeDescription
apiKeyrequiredstringThe API Key for the current environment
URLoptionalstringthe base URL for Molasses
autoSendEventsoptionalbooleanWhether to automatically send analytic events back to Molasses. Defaults to false.

init(): Promise<void>#

Fetches configuration of features and begins polling

isActive(featureKey: string): boolean#

Will check if feature is active for ALL users

isActive(featureKey: string, user: User): boolean#

Will check if the feature is active for this particular based on the segment they are in.

experimentStarted(featureKey: string, additionalInformation: string[string], user: User): void#

Will send an event started message when a user starts an a/b tests. This includes, whether user was in the experimental group (control or experiment), the experiment that was being tested and other metadata. If you want to include additional metadata use the additionalInformation argument.

experimentSuccess(featureKey: string, additionalInformation: string[string], user: User): void#

Will send an event success message when a user completes a set goal. This includes, whether user was in the experimental group (control or experiment), the experiment that was being tested and other metadata. If you want to include additional metadata use the additionalInformation argument.

identify(user: User): void#

Sets the user as the default user for isActive and experimentSuccess so those methods can be called without a User

resetUser(): void#

Resets the user from identify to undefined

reinit(): Promise<void>#

Goes to the Molasses server and fetchs a new configuraiton

Browser JavaScript Sdk