runAbExperiment
Run one A/B experiment: pick a weighted variant, apply it, and label every collector event of this page impression with the assignment. Call it before start() — the variant has to be in effect before htag schedules ads.
For QA, adnz_ab can force a matching variant while still respecting eligible(). The forced run bypasses sticky selection and carries ab_forced: 'true' in telemetry.
Example
Section titled “Example”window.htag.api('1').runAbExperiment({ name: 'image_size.v1', sticky: true, eligible: () => window.top.innerWidth >= 1024, attributes: { device: 'desktop' }, variants: [ { key: 'control', weight: 1 }, { key: 'large', weight: 1, apply: () => { window.htag.api('1').setLazyLoadingConfig((current) => current.map((config) => ({ ...config, safetyPx: 800 }))); }, }, ],});Parameters
Section titled “Parameters”- definition (required): The experiment.
- name: Experiment identifier matching
^[a-z0-9_.-]{1,64}$. Also the sticky storage key and theab_experimenttag value. - variants: At least two variants with unique keys.
- key: Variant identifier matching
^[a-z0-9_.-]{1,64}$. Becomes theab_varianttag value. - weight: Finite number greater than zero. Weights are relative, so
1against3is a 25/75 split. - apply (optional): Called when this variant is selected. Omit it for the control arm.
- key: Variant identifier matching
- eligible (optional): Enrolment predicate. A falsy result — or a throw — means the visitor is not enrolled and their events stay untagged.
- attributes (optional): Extra measurement tags, or a function returning them. Keys are strict identifiers, values are strings of 1 to 256 characters.
- sticky (optional): Remember the variant per visitor in the top window’s
localStorageunderadnz-ab-experiment-<name>.
- name: Experiment identifier matching
Order of Operations
Section titled “Order of Operations”- Validate the definition. An invalid one is logged and the call does nothing.
- Single-run guard: if an experiment is already assigned on this page impression, log and stop. The first experiment to apply wins.
- Eligibility: run
eligible(); stop if it is falsy or throws. - Resolve
attributesand validate them, before anything is applied. - Select the variant — the stored sticky bucket if there is a valid one, otherwise a weighted draw.
- Apply the variant.
- Record the assignment, and persist the sticky bucket.
Error Behaviour
Section titled “Error Behaviour”Every failure is contained; nothing here throws into your RawJS.
- Invalid definition — bad name, fewer than two variants, a duplicate variant key, a non-positive or non-finite weight: logged, nothing runs.
eligible()throws: treated as not eligible.attributesthrow, or the bag is invalid: an error is logged and the experiment runs without the extras. It is still tagged withab_experimentandab_variant. The bag is dropped as a unit — one malformed key loses all of them.apply()throws: logged; no assignment is recorded and no sticky bucket is written, so the page runs untreated and untagged rather than treated-but-unmeasured.
Return Value
Section titled “Return Value”Returns the Htag API object, allowing for method chaining.
Related Methods
Section titled “Related Methods”- runAbExperiments: Run the first eligible experiment of a set and prune stale sticky buckets
- addRawJs: Deliver the experiment declaration with the tag
Related Concepts
Section titled “Related Concepts”For weighting, sticky bucketing, telemetry tagging and the pitfalls to avoid, see the A/B Testing concept documentation.