JavaScript API
The snippet exposes window.SplitTesty. All methods can be called immediately after the install snippet: calls made before the runtime has loaded are queued and replayed.
track()
track(event: string, props?: Record<string, string | number | boolean>): voidSends a custom event. Metrics of type “custom event” with the same event name count it as a conversion. Properties may be strings, numbers or booleans; a numeric property can feed sum and average metrics. Never send personal data.
SplitTesty.track('signup_completed', { plan: 'pro', seats: 5 });revenue()
revenue(value: number, props?: Record<string, string | number | boolean>): voidRecords revenue for revenue metrics. Equivalent to track('revenue', { value }).
SplitTesty.revenue(49.9, { currency: 'EUR' });identify()
identify(attributes: Record<string, string | number | boolean>): voidSets custom visitor attributes that can be used in audience conditions (attribute “custom”). Targeting is re-evaluated right away. Use coarse, non-identifying values such as a plan name.
SplitTesty.identify({ plan: 'pro', loggedIn: true });activate()
activate(campaignId: string | number): voidEvaluates a campaign that uses manual activation, for example after a dialog has opened. The campaign ID is the number shown in the dashboard.
SplitTesty.activate(42);getVariation()
getVariation(campaignId: string | number): { id: string; name?: string } | nullReturns the variation the visitor is assigned to, as { id, name? }, or null when the visitor is not in the campaign. The name is omitted when names are hidden for the campaign.
var variation = SplitTesty.getVariation(42);
if (variation) console.log(variation.id, variation.name);onAssigned()
onAssigned(callback: (assignment) => void): voidRegisters a callback that is called for every assignment, including assignments that already happened. Use it to forward the variation to your analytics tool.
SplitTesty.onAssigned(function (assignment) {
console.log(assignment);
});SplitTesty.onAssigned(function (assignment) {
// forward every assignment to your analytics tool
window.dataLayer = window.dataLayer || [];
window.dataLayer.push({ event: 'experiment_assigned', assignment: assignment });
});consent()
consent(granted: boolean): voidGrants (true) or withdraws (false) tracking consent. When your workspace requires consent, nothing is stored or sent until consent is granted; withdrawing consent removes the snippet’s cookies.
SplitTesty.consent(true); // visitor accepted
SplitTesty.consent(false); // visitor declined or withdrewCommand queue
Before the runtime has loaded, window.SplitTesty is a queue. Besides the methods above you can push a function that receives the API once it is ready:
window.SplitTesty = window.SplitTesty || [];
SplitTesty.push(function (api) {
var variation = api.getVariation(42);
if (variation) document.documentElement.dataset.variation = variation.id;
});Debugging
Append ?spt_debug=1 to the page URL to log every decision to the console. SplitTesty.debug() returns the current state (visitor ID, consent, evaluated campaigns).