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.

Last updated

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 attributesOne click listenerConnect for Umami
Works without writing codePartlyNoYes
Covers every outbound link and button automaticallyNoYesYes
Works with the block editor's Button blockNot without codeYesYes
Recognizes page builder buttonsOnly where the builder has an attribute fieldOnly the selectors you addYes
Data sent with the eventWhat you type in, saved as textWhat your script collectsLink 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.

An outbound link and a button tracked with Umami's data attributes
<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.

  1. Save the script

    Save the JavaScript below as umami-click-events.js in your child theme's folder.

  2. Tell it what a button is

    Edit the BUTTONS selector at the top. It covers real buttons and the block editor's Button block. Add the button class of your theme or page builder.

  3. 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.

  4. Test it

    In a private browser window, click a link to another site and a button. In Umami, outbound_link_click and button_click should appear under Events.

umami-click-events.js: outbound links and button clicks
(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);
})();
functions.php: load the script on every front-end page
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.com and example.com count as the same site.
  • The url is 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_click only.
  • Elements that already carry data-umami-event are 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

  1. Open the Events tab

    In WordPress, go to Settings → WPUmami → Events.

  2. 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.

  3. 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.

EventWhen it is sentData sent with it
outbound_link_clickA click on a link to another website.
url
Address of the link, without the query string
text
The link text
button
true when the link is styled as a button
button_clickA click on a button.
text
The button text
url
Where it leads, if the button is a link
id
The button's ID, if it has one
link_clickA click on a link within the site. Needs the Link clicks switch.
url
Path of the link
text
The link text
type
internal, download, email or phone

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.

See pricing