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
1avsb.track.event(shortId: number, properties?: { revenue?: number }) => voidParameters
shortIdnumberRequiredThe 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.
propertiesobjectOptionalAn optional object containing additional data to attach to the conversion event.
properties.revenuenumberOptionalA 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:
- Open your project in A vs B.
- Click Metrics in the left navigation.
- 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. - 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.
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
submitevent 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
endedevent callback. - Time on page — inside a
setTimeout()callback after a meaningful dwell time like 30 or 60 seconds.
Code examples
1// Track when a visitor submits a lead form2document.querySelector('#lead-form').addEventListener('submit', function() {3 avsb.track.event(12345);4});1// Track a completed purchase and include the order total2avsb.track.event(12345, { revenue: 49.99 });1// Track when a visitor clicks the buy button2document.querySelector('.buy-btn').addEventListener('click', function() {3 avsb.track.event(67890, { revenue: 29.99 });4});1// Only track when the server confirms the action2fetch('/api/subscribe', { method: 'POST', body: formData })3 .then(function(response) {4 if (response.ok) {5 avsb.track.event(12345);6 }7 });1// Track when a visitor reaches 50% scroll depth2var 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 once9 }10});1// Track after the visitor has spent 60 seconds on the page2setTimeout(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.
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.
1// Correct — plain number2avsb.track.event(12345, { revenue: 49.99 });3avsb.track.event(12345, { revenue: 100 });4
5// Incorrect — will not work6avsb.track.event(12345, { revenue: '$49.99' }); // string with symbol7avsb.track.event(12345, { revenue: '49.99' }); // string number