Skip to content

runAbExperiments

Declare the tag’s A/B experiments as one list. htag runs the first eligible experiment of the list and prunes the sticky buckets of every experiment that is no longer in it. Call it before start().

window.htag.api('1').runAbExperiments([
{
name: 'lazyload_threshold.v2',
sticky: true,
eligible: () => window.top.innerWidth >= 1024,
variants: [
{ key: 'control', weight: 1 },
{
key: 'eager',
weight: 1,
apply: () => {
window.htag.api('1').setLazyLoadingConfig((current) => current.map((config) => ({ ...config, safetyPx: 800 })));
},
},
],
},
{
name: 'mobile_reload.v1',
eligible: () => window.top.innerWidth < 1024,
attributes: { device: 'mobile' },
variants: [
{ key: 'control', weight: 3 },
{ key: 'no_reload', weight: 1, apply: () => window.htag.api('1').cancelDelayedReloads() },
],
},
]);

A desktop visitor is enrolled in lazyload_threshold.v2; the mobile experiment is never reached because the list stops at the first eligible entry. A mobile visitor skips the first and lands in mobile_reload.v1.

  • definitions (required): Array of experiment definitions. Each entry has the same shape as the argument of runAbExperiment: name, variants, and the optional eligible, attributes and sticky.

The list is walked in order. An invalid definition is logged and skipped, an ineligible one is skipped silently, and the first definition that passes both is run — with the same order of operations and the same error behaviour as runAbExperiment. The walk then stops: only one experiment is ever assigned per page impression, so list them by priority.

Before anything runs, htag enumerates the sticky buckets in the top window’s localStorage and deletes every adnz-ab-experiment-<name> entry whose <name> is not in definitions. That is what keeps buckets of finished experiments from accumulating on a returning visitor.

Pruning is skipped entirely when the list contains no valid experiment name, so an empty array is a no-op rather than a wipe.

Returns the Htag API object, allowing for method chaining.

  • runAbExperiment: Run a single experiment definition, without pruning
  • addRawJs: Deliver the experiment declarations with the tag

For weighting, sticky bucketing, telemetry tagging and the pitfalls to avoid, see the A/B Testing concept documentation.