Guide
How to track outbound links and button clicks in Umami on WordPress
Umami records pageviews by itself. Clicks are events, and you decide which ones to send. In WordPress there are three ways to do it, from one attribute per element to one switch for the whole site.
Short answer
To track outbound links and button clicks in Umami on WordPress, add Umami's data-umami-event attribute to each link or button, or add one JavaScript click listener that calls umami.track for links to other hostnames and for buttons. The attribute is awkward in the block editor, which has no field for custom attributes. Connect for Umami does both with two switches on its Events tab.
Three ways to track clicks, compared
| data-umami-event attributes | One click listener | Connect for Umami | |
|---|---|---|---|
| Works without writing code | Partly | No | Yes |
| Covers every outbound link and button automatically | No | Yes | Yes |
| Works with the block editor's Button block | Not without code | Yes | Yes |
| Recognizes page builder buttons | Only where the builder has an attribute field | Only the selectors you add | Yes |
| Data sent with the event | What you type in, saved as text | What your script collects | Link address and text, button text and ID |
Method 1: data-umami-event attributes
Umami records a click on any element that carries a data-umami-event attribute, and uses the attribute's value as the event name. Attributes that start with data-umami-event- are sent along as event data.
<a href="https://example.org/" data-umami-event="outbound_link_click" data-umami-event-url="https://example.org/">Visit our partner</a>
<button type="button" data-umami-event="button_click" data-umami-event-text="Get a quote">Get a quote</button>Two notes from Umami's documentation: event data sent this way is always saved as text, and other event listeners inside the element will not be triggered. Test any element that also runs JavaScript of its own.
Why attributes are awkward in WordPress
- The block editor has no field for custom attributes. A Button block offers an HTML anchor and extra CSS classes, not
data-attributes. If you add one by editing the block as HTML, the editor usually reports the block as invalid. - A Custom HTML block works, but you then write and maintain the link by hand.
- Page builders differ. Some have a field for custom attributes (in Elementor it is part of Elementor Pro), others have none.
- Every element is tagged one by one, including the links and buttons added later.
Method 2: track outbound links and buttons with one click listener
One script watches every click and sends an event for outbound links and for buttons. Nothing is added to individual elements.
Save the script
Save the JavaScript below as
umami-click-events.jsin your child theme's folder.Tell it what a button is
Edit the
BUTTONSselector at the top. It covers real buttons and the block editor's Button block. Add the button class of your theme or page builder.Load it on the front end
Add the PHP below to the child theme's
functions.php. A code-snippets plugin that runs JavaScript on the front end works as well: paste the script there instead.Test it
In a private browser window, click a link to another site and a button. In Umami,
outbound_link_clickandbutton_clickshould appear under Events.
(function () {
// What counts as a button. Add your theme's or page builder's button class.
var BUTTONS = 'button, input[type="submit"], [role="button"], .wp-block-button__link, .wp-element-button';
// Umami's script is deferred: wait for window.umami (up to 10 seconds), then send.
function send(name, data, tries) {
if (window.umami && window.umami.track) {
window.umami.track(name, data);
} else if ((tries || 0) < 100) {
setTimeout(function () { send(name, data, (tries || 0) + 1); }, 100);
}
}
function label(el) {
return (el.innerText || el.value || el.getAttribute('aria-label') || '').replace(/\s+/g, ' ').trim().slice(0, 100);
}
function bare(host) {
return host.replace(/^www\./, '');
}
// One listener on the document covers every link and button, including ones added later.
document.addEventListener('click', function (event) {
var target = event.target;
if (!target || !target.closest) return;
// Elements with Umami's own attribute are already tracked by Umami.
if (target.closest('[data-umami-event]')) return;
var link = target.closest('a[href]');
var button = target.closest(BUTTONS);
var outbound = link && /^https?:$/.test(link.protocol) && bare(link.hostname) !== bare(window.location.hostname);
if (outbound) {
send('outbound_link_click', { url: link.origin + link.pathname, text: label(link) });
} else if (button) {
send('button_click', { text: label(button), id: button.id || undefined });
}
}, true);
})();add_action( 'wp_enqueue_scripts', function () {
wp_enqueue_script(
'umami-click-events',
get_stylesheet_directory_uri() . '/umami-click-events.js', // The file in your child theme.
array(),
'1.0',
true // Load in the footer.
);
} );What the script does, and what it leaves out
- A link is outbound when it points to a different hostname than the page.
www.example.comandexample.comcount as the same site. - The
urlis sent without its query string, so nothing in the query string reaches Umami. - Each click is one event. An outbound link that is styled as a button is sent as
outbound_link_clickonly. - Elements that already carry
data-umami-eventare skipped, so nothing is counted twice. - It waits for
window.umami, because Umami's script is deferred, and gives up after 10 seconds if the tracker never loads. - It hears normal clicks only. A middle-click that opens a link in a new tab is a different browser event (
auxclick) and is not counted.
Method 3: two switches in Connect for Umami
Open the Events tab
In WordPress, go to Settings → WPUmami → Events.
Switch on what you want to track
Tick Outbound links and Button clicks, then save. A third switch, Link clicks, adds clicks on links within your own site, including downloads, email and phone links.
Check Umami
Click a few links and buttons in a private browser window, because logged-in administrators are not tracked by default. Then look under Events in Umami.
What each event carries
One event per click, by the most specific thing it was: outbound link first, then button, then internal link.
| Event | When it is sent | Data sent with it |
|---|---|---|
outbound_link_click | A click on a link to another website. |
|
button_click | A click on a button. |
|
link_click | A click on a link within the site. Needs the Link clicks switch. |
|
Which buttons the plugin recognizes
Button click tracking recognizes real <button> elements, submit inputs and anything with role="button". It also recognizes the link-styled buttons of the block editor's Button block, block themes, Elementor, Divi, Beaver Builder, Bricks, Kadence Blocks, GenerateBlocks, Spectra, Stackable, Oxygen, WPBakery, Avada, Brizy, Thrive Architect, SiteOrigin and Astra. Developers can add selectors with the cfumami_button_selectors filter. Elements that already carry data-umami-event are left to Umami. The page builders page has the details.
Frequently asked questions
Does Umami track outbound links automatically?
No. Umami records pageviews automatically. A click on a link or a button is recorded only when it is sent as an event: with a data-umami-event attribute, with umami.track in JavaScript, or through a plugin such as Connect for Umami.
How do I add data-umami-event to a button in the block editor?
The Button block has no field for custom attributes. You can write the link yourself in a Custom HTML block, or skip attributes and use a click listener or Connect for Umami. Both recognize the Button block by its CSS class, so nothing has to be added to the button.
Is the click still recorded when the link opens in the same tab?
Normally, yes. Umami's tracker sends events as keepalive requests, which the browser is allowed to finish while the next page loads.
Where do I see which links and buttons were clicked in Umami?
Open the website in Umami and go to Events. The Properties tab breaks each event down by its data, such as url and text.
Can I track only certain buttons?
Yes. With attributes, tag only those buttons. With the script, narrow the BUTTONS selector, for example to a single CSS class. In Connect for Umami, developers can change the list of selectors with the cfumami_button_selectors filter.
Does this also track internal links and file downloads?
The script on this page does not. Connect for Umami has a separate Link clicks switch that sends link_click for links within your site, with a type of internal, download, email or phone.
Connect WordPress to Umami in about two minutes
One plugin: the tracking script, events, store revenue and your stats inside WordPress. Plans start at $12 a year, with a 14-day money-back guarantee.