Skip to content

HTML variants

usertrax controls HTML element visibility via data attributes. You can test content versions without writing custom show/hide JavaScript.

The HTML element visibility feature:

  • Shows/hides elements based on A/B test variants
  • Handles fallback content when no variant is assigned yet
  • Works with multiple simultaneous tests
  • Observes dynamically inserted elements (e.g. in SPAs)
  • Requires no JavaScript knowledge for the basic case

data-usertrax-ab-test-group must match the test key, and data-usertrax-ab-test-variant must match the variant key from the dashboard.

AttributeDescriptionRequired
data-usertrax-ab-test-groupUnique identifier for the test group (= test key)Yes
data-usertrax-ab-test-variantThe variant this element belongs toYes
data-usertrax-ab-test-fallbackMark as fallback (visible until a variant is assigned)No
<!-- Fallback element (shown by default) -->
<div
data-usertrax-ab-test-group="pricing-display"
data-usertrax-ab-test-variant="single-price"
data-usertrax-ab-test-fallback
>
<span class="price">$99/month</span>
</div>
<!-- Alternative variant (hidden by default) -->
<div
style="display: none"
data-usertrax-ab-test-group="pricing-display"
data-usertrax-ab-test-variant="price-range"
>
<span class="price">$89 – $129/month</span>
</div>

<h1
data-usertrax-ab-test-group="hero_headline"
data-usertrax-ab-test-variant="control"
data-usertrax-ab-test-fallback
>
Track conversions. Without cookies.
</h1>
<h1
style="display: none"
data-usertrax-ab-test-group="hero_headline"
data-usertrax-ab-test-variant="benefit_first"
>
More attribution. Less banner friction.
</h1>

Both headlines should be meaningful for users and SEO — not keyword spam. See Best practices.


<!-- Test group 1: Header style -->
<div
data-usertrax-ab-test-group="header-style"
data-usertrax-ab-test-variant="minimal"
data-usertrax-ab-test-fallback
>
<header class="header-minimal">
<nav>Home | About | Contact</nav>
</header>
</div>
<div
style="display: none"
data-usertrax-ab-test-group="header-style"
data-usertrax-ab-test-variant="featured"
>
<header class="header-featured">
<nav>Home | About | Contact</nav>
<div class="announcement">New features available!</div>
</header>
</div>
<!-- Test group 2: CTA button -->
<div
data-usertrax-ab-test-group="cta-button"
data-usertrax-ab-test-variant="primary"
data-usertrax-ab-test-fallback
>
<button class="btn-primary">Get started</button>
</div>
<div
style="display: none"
data-usertrax-ab-test-group="cta-button"
data-usertrax-ab-test-variant="secondary"
>
<button class="btn-secondary">Start free trial</button>
</div>

<!-- Bad: both variants hidden, no fallback → brief empty space -->
<div style="display: none" data-usertrax-ab-test-group="hero" data-usertrax-ab-test-variant="a">
</div>
<div style="display: none" data-usertrax-ab-test-group="hero" data-usertrax-ab-test-variant="b">
</div>
<!-- Good: control visible as fallback until assignment is ready -->
<div
data-usertrax-ab-test-group="hero"
data-usertrax-ab-test-variant="a"
data-usertrax-ab-test-fallback
>
</div>
<div style="display: none" data-usertrax-ab-test-group="hero" data-usertrax-ab-test-variant="b">
</div>

More tips against flicker: Troubleshooting.

<!-- Bad: group ≠ test key in dashboard (button_color_test) -->
<div data-usertrax-ab-test-group="buttonColor" >
<!-- Good: exact same string as test and variant keys -->
<div
data-usertrax-ab-test-group="button_color_test"
data-usertrax-ab-test-variant="variant_red"
>

Case and underscores matter. getAssignments() in the console helps you verify the match.


Best practices for HTML element visibility

Section titled “Best practices for HTML element visibility”
<!-- Good: Clear and descriptive -->
data-usertrax-ab-test-group="hero-headline-test"
<!-- Avoid: Too generic -->
data-usertrax-ab-test-group="test1"

  1. Check A/B test assignment:

    console.log("Current variants:", window.usertrax.getAssignments());
  2. Verify data attributes:

    <!-- Ensure group and variant match exactly -->
    data-usertrax-ab-test-group="my-test"
    data-usertrax-ab-test-variant="my-variant"

More detail: Troubleshooting.


<!-- HTML visibility control -->
<div
data-usertrax-ab-test-group="layout"
data-usertrax-ab-test-variant="compact"
data-usertrax-ab-test-fallback
>
<div class="content-compact">
<h2>Compact layout</h2>
</div>
</div>
<!-- CSS from the A/B test config is applied in addition -->
<div
style="display: none"
data-usertrax-ab-test-group="layout"
data-usertrax-ab-test-variant="expanded"
>
<div class="content-expanded">
<h2>Expanded layout</h2>
</div>
</div>