Skip to main content

Usage with JavaScript and TypeScript

The Divisor SDK is written in TypeScript and provides full type definitions for a superior development experience.

Basic Configuration​

To start using the SDK in a pure JavaScript or TypeScript project:

import { DivisorClient } from '@divisor.dev/sdk';

// Initial configuration
const config = {
tenantId: 'your-tenant-id',
userId: 'user-123' // Optional: provide a unique ID for the user
};

const divisor = new DivisorClient(config);

Fetching Variants​

The getVariant method is asynchronous and returns the assigned variant for a specific experiment.

async function handleExperiment() {
const result = await divisor.getVariant({
experimentName: 'contact-button-text',
variantFallback: 'original' // Optional: Value returned in case of error
});

if (result.variant === 'get-help') {
renderButton('Get Help');
} else {
renderButton('Contact Us');
}
}

Tracking Conversions​

Record events such as sales, clicks, or signups using the conversion method.

async function onOrderPlaced() {
await divisor.conversion({
experimentName: 'contact-button-text',
variant: 'get-help', // The variant the user saw
value: 49.90, // Value of the transaction
itensCount: 1 // Number of items
});
}

Type Reference​

DivisorConfig​

PropertyTypeDescription
tenantIdstringThe unique identifier for your workspace/tenant.
userIdstringOptional: provide a unique ID for the user.

GetVariant (Parameters)​

PropertyTypeRequiredDescription
experimentNamestringYesThe name of the experiment configured in the dashboard.
variantFallbackstringNoVariant to be used if something goes wrong (e.g., offline network).

ExperimentResult​

PropertyTypeDescription
experimentstringName of the requested experiment.
variantstring | nullThe variant assigned to the user.