Custom Events
Custom events let you record a conversion from your own JavaScript code at exactly the right moment. Instead of relying on a click or a URL change, you call avsb.track.event() yourself: inside a form submit handler, after a successful API call, when a user reaches a certain scroll depth, or at any other meaningful point in your application.
When to use custom events
Custom events are the right choice whenever a conversion cannot be captured by a click on a static element or by a URL change. Common use cases include:
- Form submissions: the "submit" event on a form fires before the server processes it, so you can track the submission attempt. Or wait for the API response to confirm success before recording the conversion.
- Purchases and checkouts: track the exact moment an order is confirmed, and pass the order total as a revenue value.
- Scroll depth: fire when a visitor scrolls to 50% or 100% of a long-form content page.
- Time on page: fire after a visitor spends 60 seconds on a page as a proxy for engagement.
- Video completions: fire when a video player emits its
endedevent. - Multi-step flows: fire when a visitor completes a specific step in a wizard or onboarding flow.
The metric key
Every custom event metric has a metric key: a short piece of text you choose when you create it, like signup_completed or added_to_cart. That key is the one argument avsb.track.event() needs, and it is shown on the metric in your project's Metrics page next to a ready-to-copy line of code.
Pick something readable and keep it stable, because it lives in your site's code:
avsb.track.event('signup_completed');Give three metrics the same key (a conversion rate, a count, and a 95th percentile of order value) and one call feeds all three. That is the point of keys: instrument the event once in your site, then add or change the metrics built on it here, with no code change.
If nothing on the project uses the key you passed, the call is dropped and the console tells you, listing the keys that do work:
[avsb] track.event("signup") dropped: no metric has that key. Known: signup_completed, added_to_cartSo create the metric first, or fix the typo the message points at.
How to create a custom event metric
Go to the Metrics page
From your project dashboard, click Metrics in the left navigation.
Click New Metric
Click the New metric button to open the metric creation form.
Choose Custom as the type
Select Custom from the metric type options.
Give it a name
Enter a descriptive name like Checkout Form Submitted, Purchase Completed, or Video Finished.
Set the metric key
Type the key your code will use, for example signup_completed. Letters, digits, and underscores keep it easy to read in both places.
When you leave the box, A vs B checks whether this project received that event in the last 30 days and tells you how often. The check looks for the event under the metric's wire name too (the name the snippet records it under for each experiment that uses the metric), so an event your experiments already count is never reported as missing. Opening an existing metric runs the same check.
Save the metric
Click Save. The metric is created and its key is shown in the metrics list, with a copy-ready snippet.
Calling avsb.track.event()
Place a call to avsb.track.event() in your JavaScript at the moment you want to record a conversion. The function takes two arguments: the metric key and an optional data object.
Basic usage:
// Record a conversion for the metric with the key "signup_completed"avsb.track.event('signup_completed');Inside a form submit handler:
var checkoutForm = document.querySelector('#checkout-form');if (checkoutForm) { checkoutForm.addEventListener('submit', function() { avsb.track.event('checkout_submitted', { revenue: 49.99 }); });}After an API call succeeds:
var formData = new FormData(); // whatever your form collectsfetch('/api/subscribe', { method: 'POST', body: formData }) .then(function(response) { if (response.ok) { avsb.track.event('subscribed'); } });Scroll depth tracking:
var fired = false;window.addEventListener('scroll', function() { if (fired) return; var scrollPct = (window.scrollY + window.innerHeight) / document.body.scrollHeight; if (scrollPct >= 0.5) { avsb.track.event('scrolled_half'); // fired when visitor reaches 50% scroll depth fired = true; }});Time on page:
setTimeout(function() { avsb.track.event('engaged_60s'); // fired after 60 seconds on page}, 60000);Passing revenue data
When the conversion involves a monetary transaction, pass the amount as a revenue property in the second argument. This unlocks Revenue Impact calculations in your experiment results.
avsb.track.event('purchase_completed', { revenue: 49.99 });Revenue should be a number representing the transaction amount in your primary currency. Do not pass currency symbols or formatted strings, only a plain number.
One conversion per visitor
A conversion-rate custom-event metric records at most one conversion per visitor. If you call avsb.track.event() multiple times for the same metric, only the first call moves the conversion rate; subsequent calls do not change it. Count and percentile metrics built on the same event are the exception: they take every call into account, so fire the event each time it genuinely happens.
If you need to track total revenue rather than just whether a conversion happened, keep in mind that only the revenue value from the first conversion call is stored. For experiments where visitors may make multiple purchases, consider whether your primary metric should be "first purchase" rather than total lifetime value.