Code snippets
How to Track Clicks on A/B Test Elements with JavaScript
★★★ Technical level 3 of 3
Written by Neil Webley · Last updated
Attach event listeners safely to buttons and links.
What you need to know
Replace the Attach Event Handler sample selector with a stable selector. Use polling or event delegation for elements rendered later.
How to use it safely
Prevent duplicate listeners so one action is recorded once. Preserve normal navigation and test keyboard activation.
Before publishing
- Use Staging to test control and every variant.
- Check matching and non-matching locations.
- Verify mobile, desktop, accessibility and analytics behaviour.
- Ask someone else to review the setup.