Integrate ABlyftReferenceJavaScript API

ablyft.push()

Send goals, page commands, listeners and consent changes to ABlyft.

ablyft.push(event) takes an object with an eventType and the parameters of that type. It can also be called before the snippet has loaded, see queueing.

eventTypePurpose
customTrigger a custom goal
revenueTrigger a revenue goal with a value
reevaluateAudiencesRe-check audiences of experiments on active pages
forceVariationForce a variation for a visitor
activatePageActivate a page with the API trigger
deactivatePageDeactivate a page
addListenerRun a function when the snippet finished its run
enableTrackingConsentStore that the visitor consented to tracking
disableTrackingConsentStore that the visitor declined tracking

custom

Triggers a goal of type custom, identified by its API name.

ParameterTypeRequiredDescription
eventType'custom'Yes
eventNamestringYesAPI name of the goal. Unknown names are ignored silently.
eventValuenumberNoDefaults to 0. Only meaningful for goals with the measurement type Value.
window.ablyft = window.ablyft || [];

window.ablyft.push({
  eventType: 'custom',
  eventName: 'newsletter-signup',
});

revenue

Triggers a goal of type revenue. Same parameters as custom; pass the revenue as eventValue, a number (not a string) in the currency you use. Without it, the value is 0.

window.ablyft = window.ablyft || [];

window.ablyft.push({
  eventType: 'revenue',
  eventName: 'purchase',
  eventValue: 49.9, // order value
});

reevaluateAudiences

Re-evaluates the audiences of all experiments on the currently active pages, for example after a data layer value or cookie changed.

ablyft.push({ eventType: 'reevaluateAudiences' });

forceVariation

Forces a variation for an experiment. Queued calls of this type are applied immediately when the snippet loads, before the experiments are evaluated.

ParameterTypeDescription
experimentIdnumberID of the experiment
variationIdnumberID of the variation to show
window.ablyft = window.ablyft || [];

window.ablyft.push({
  eventType: 'forceVariation',
  experimentId: 12345678,
  variationId: 98765432,
});

The call can be placed before or after the ABlyft snippet, but it has to happen before the experiment is evaluated for the visitor, that is before the audience check and before the visitor has been assigned to a variation. It is useful for developers who test the real flow, including goals, in a browser or an automated test.

Only for visitors without an assignment

The forced variation is used when the visitor is assigned to the experiment for the first time. If this browser already has an assignment for the experiment (stored in the configured storage, see Storage & privacy), that assignment stays. Clear the storage or use a private window. The visitor still has to pass the other checks (environment, audiences, exclusion groups). Forced visitors are real participants: their data counts in your results. For a quick look at a variation without any code, use the preview mode.

activatePage

Activates a page, for example one configured with the API trigger. Identify the page by ID or API name; if pageId is given, it takes precedence. If the page does not exist, an error is logged and an ablyftError event is dispatched.

ParameterTypeDescription
pageIdnumberID of the page
pageApiNamestringAPI name of the page
ablyft.push({ eventType: 'activatePage', pageApiName: 'checkout-step-2' });

Setup of the page and the effect of the call: Page API.

deactivatePage

Deactivates a page. Same parameters and error behavior as activatePage. The effect depends on the page's deactivation mode, see Page API.

ablyft.push({ eventType: 'deactivatePage', pageApiName: 'checkout-step-2' });

addListener

Registers a listener that is called (without arguments) after the snippet finished a run, including runs triggered by reInit().

ParameterTypeDescription
listener.type'lifecycleEnd'The only supported listener type
listener.handlerfunctionCalled without arguments
ablyft.push({
  eventType: 'addListener',
  listener: {
    type: 'lifecycleEnd',
    handler: () => console.log('ABlyft finished its run'),
  },
});

There is only one handler slot. Registering a second lifecycleEnd listener replaces the first one.

enableTrackingConsent

Stores the visitor's consent (ablyft_tracking_consent with value 1) in the configured storage. Data that was held in temporary storage until now is moved to the configured storage as well. When consent is granted for the first time, ABlyft re-initializes with reInit({ resetStates: true }).

Call this from your consent manager. See Consent manager.

// Example: call when the visitor accepts statistics cookies
ablyft.push({ eventType: 'enableTrackingConsent' });

disableTrackingConsent

Stores that the visitor declined (ablyft_tracking_consent with value 0) in the configured storage.

ablyft.push({ eventType: 'disableTrackingConsent' });

To read the current state, use ablyft.get('trackingConsentEnabled').

On this page