/Docs

Tracking Events

avsb.track.event() is how you tell A vs B that a visitor has converted on a custom event metric. You call it from your own JavaScript at exactly the right moment — after a form submission, when a purchase confirms, when a scroll threshold is crossed, or at any other meaningful point in your application.

Method signature

typescript
1avsb.track.event(shortId: number, properties?: { revenue?: number }) => void

Parameters

NameDescription
shortIdnumberRequired

The numeric ID of the custom event metric. Find this ID in the Metrics page of your project — it appears next to the metric name in the list and inside the metric detail panel when you click on a metric.

propertiesobjectOptional

An optional object containing additional data to attach to the conversion event.

properties.revenuenumberOptional

A monetary amount associated with this conversion. Used for Revenue Impact calculations in experiment results. Pass a plain number with no currency symbol — for example 49.99, not "$49.99". Only meaningful for metrics that represent monetary transactions.

Finding the short ID

Every custom event metric in A vs B has a unique numeric short ID. You need this ID to call avsb.track.event() correctly. To find it:

  1. Open your project in A vs B.
  2. Click Metrics in the left navigation.
  3. Find the metric you want to track. Its short ID is displayed next to the metric name in the list — it is a number like 12345.
  4. You can also click the metric to open its detail panel, where the short ID is shown more prominently alongside a ready-to-copy code snippet.
The short ID is a number, not a string
Always pass the short ID as a plain number. Writing avsb.track.event('12345') with quotes around the ID will not work — only avsb.track.event(12345) without quotes is correct.

When to call it

Call avsb.track.event() at the precise moment the conversion happens. The right moment depends on what you are measuring:

  • Form submissions — in the form's submit event handler, or better yet, after a successful server response confirming the submission was processed.
  • Purchases — after your payment API confirms the transaction. Never fire before the transaction is confirmed, as failed payments would be counted as conversions.
  • Button clicks — in a click event listener. This counts the intent to act, not the outcome. Good for measuring engagement with a CTA when there is no server-side confirmation step.
  • Scroll milestones — inside a scroll event listener once the visitor has scrolled past a threshold (50%, 100%, etc.). Remember to fire only once per page load using a boolean guard.
  • Video completions — in the video player's ended event callback.
  • Time on page — inside a setTimeout() callback after a meaningful dwell time like 30 or 60 seconds.

Code examples

Track a form submission
javascript
1// Track when a visitor submits a lead form
2document.querySelector('#lead-form').addEventListener('submit', function() {
3 avsb.track.event(12345);
4});
Track a purchase with revenue
javascript
1// Track a completed purchase and include the order total
2avsb.track.event(12345, { revenue: 49.99 });
Track on button click
javascript
1// Track when a visitor clicks the buy button
2document.querySelector('.buy-btn').addEventListener('click', function() {
3 avsb.track.event(67890, { revenue: 29.99 });
4});
Track after a successful API call
javascript
1// Only track when the server confirms the action
2fetch('/api/subscribe', { method: 'POST', body: formData })
3 .then(function(response) {
4 if (response.ok) {
5 avsb.track.event(12345);
6 }
7 });
Track scroll depth
javascript
1// Track when a visitor reaches 50% scroll depth
2var scrollFired = false;
3window.addEventListener('scroll', function() {
4 if (scrollFired) return;
5 var pct = (window.scrollY + window.innerHeight) / document.body.scrollHeight;
6 if (pct >= 0.5) {
7 avsb.track.event(12345);
8 scrollFired = true; // only fire once
9 }
10});
Track time on page
javascript
1// Track after the visitor has spent 60 seconds on the page
2setTimeout(function() {
3 avsb.track.event(12345);
4}, 60000);

How event batching works

A vs B does not send each event immediately as it happens. Instead, it buffers events and flushes them in batches to reduce network overhead and avoid disrupting the user's browsing experience.

Events are flushed automatically when either of these conditions is met:

  • The batch reaches 10 events.
  • 2 seconds have elapsed since the first event in the current batch was recorded.

Events are sent using navigator.sendBeacon(), which is a browser API designed for exactly this kind of analytics data. Unlike a normal fetch() request, a beacon is guaranteed to be delivered even if the visitor navigates away from the page immediately after the event fires. This means you do not need to worry about losing conversions that happen right before a redirect.

One conversion per visitor per metric
A vs B records at most one conversion per visitor per metric. If you call avsb.track.event() multiple times for the same metric in the same session — or in any future session — only the first call is counted. Subsequent calls are silently discarded. This is by design: conversion rates in A/B testing are measured as the proportion of visitors who converted, not the total number of events fired.

Revenue tracking

When you pass a revenue value, A vs B records it alongside the conversion event. This unlocks the Revenue Impact section on your experiment results page, which estimates the annualised revenue difference between your control and variant based on observed conversion rates and average order values.

Revenue should be a plain JavaScript number representing the transaction amount. Do not include currency symbols, formatting, or strings.

Revenue format examples
javascript
1// Correct — plain number
2avsb.track.event(12345, { revenue: 49.99 });
3avsb.track.event(12345, { revenue: 100 });
4
5// Incorrect — will not work
6avsb.track.event(12345, { revenue: '$49.99' }); // string with symbol
7avsb.track.event(12345, { revenue: '49.99' }); // string number
Revenue from the first conversion only
Because only the first conversion is counted per visitor, only the revenue value from the first call is stored. For experiments where visitors might make multiple purchases, design your metric around the first purchase event rather than trying to sum revenue across multiple calls.