Skip to main content

Tracking clicks on cloaked affiliate links and other pretty URLs


If you track link clicks, then these count towards your billable monthly pageviews.

Many sites use link cloaking to make affiliate links cleaner and easier to manage. So rather than linking to you link to a page on your domain name such as and the visitor gets redirected to the actual affiliate link after clicking.

The same system is used to make pretty URLs for file names. You have links to that actually redirect to

Instructions below can be used to start tracking every <a> (i.e. link) element on your site with some specified rules. It's very similar to our automated tracking of outbound link clicks and file downloads but you can choose based on the link URL (href attribute) which links to track.

Here's how to automatically track clicks on cloaked affiliate links and other pretty URLs:

1. Trigger custom events with JavaScript on your site

First, make sure your tracking setup includes the second line as shown below:

<script defer data-domain="" src=""></script>
<script>window.plausible = window.plausible || function() { (window.plausible.q = window.plausible.q || []).push(arguments) }</script>

You need to add the following code to all of the pages where you want to track your links. You should insert the code below into your HTML page <head> section just under the custom event snippet.

On the line that says var toBeTracked = '/go/' change /go/ to what you want to match with. With this example, any link with a URL that contains /go/ will be tracked. If simply containing a string value is not enough, see more flexible URL matching below.

function getLinkEl(link) {
while (link && (typeof link.tagName === 'undefined' || link.tagName.toLowerCase() !== 'a' || !link.href)) {
link = link.parentNode
return link

function shouldFollowLink(event, link) {
// If default has been prevented by an external script, Plausible should not intercept navigation.
if (event.defaultPrevented) { return false }

var targetsCurrentWindow = ! ||^_(self|parent|top)$/i)
var isRegularClick = !(event.ctrlKey || event.metaKey || event.shiftKey) && event.type === 'click'
return targetsCurrentWindow && isRegularClick


function handleLinkClick(event) {
if (event.type === 'auxclick' && event.button !== MIDDLE_MOUSE_BUTTON) { return }

var link = getLinkEl(

if (link && shouldTrackLink(link)) {
var eventName = 'Cloaked Link: Click'
var eventProps = { url: link.href }
return sendLinkClickEvent(event, link, eventName, eventProps)

function sendLinkClickEvent(event, link, eventName, eventProps) {
var followedLink = false

function followLink() {
if (!followedLink) {
followedLink = true
window.location = link.href

if (shouldFollowLink(event, link)) {
plausible(eventName, { props: eventProps, callback: followLink })
setTimeout(followLink, 5000)
} else {
plausible(eventName, { props: eventProps })

function shouldTrackLink(link) {
var toBeTracked = '/go/'
return !!link.href.match(toBeTracked)

document.addEventListener('click', handleLinkClick)
document.addEventListener('auxclick', handleLinkClick)

To keep things cleaner in your code, you can also copy the code above into a new .js file and load it onto every page via the script src attribute. <script src="path/to/your/file.js"></script>. If you do this, make sure to copy the code into the .js file without the surrounding <script> tags.

More flexible URL matching

If simply containing a substring is not enough to differentiate between links you want and do not want to track, you can also use a JavaScript regular expression as the toBeTracked value. For example, say you want to track links like, but not links like In this case, you can do:

  var toBeTracked = /products\/.*\/details/

where \/ stands for a forward slash (escaped with \) and .* will match any (or empty) string in the middle. It will basically match anything that contains the format products/<anything>/details.

3. Create a custom event goal in your Plausible Analytics account

You'll have to configure the goal for the click numbers to show up in your Plausible dashboard. To configure a goal, go to your website's settings in your Plausible Analytics account and visit the "Goals" section. You should see an empty list with a prompt to add a goal.

Add your first goal

Click on the "+ Add goal" button to go to the goal creation form.

Select Custom event as the goal trigger and enter this exact name: Cloaked Link: Click.

Next, click on the "Add goal" button and you'll be taken back to the Goals page. When you navigate back to your Plausible Analytics dashboard, you should see the number of visitors who have completed your new custom event. Goal conversions are listed at the very bottom of the dashboard. Note that at least one click is required for this to show in your dashboard.

That's it. You're now tracking all link clicks on your site with custom URL matching rules!