I recently started using Plausible on my sites as an alternative to Google Analytics.

So far, I’d say the experience has been great and I’m quite happy with the switch.

While I didn’t have anything super custom going on with my Google Analytics setup, Plausible has been able to give me all the essential data I need.

The only thing I was missing was being able to track my affiliate link clicks.

Plausible does provide documentation on how to track outbound link clicks, but my affiliate links follow the URL pattern of newpulselabs.com/go/productname/.

This means it’s technically an internal link click – even though the user immediately gets redirected to the actual affiliate link.

Luckily Plausible’s support team was really helpful, and provided a solution just a few hours later.

Here it is in case you’re looking to track internal link clicks as well.

Tracking Internal/Affiliate Link Clicks With Plausible

1. Ensure you have the custom event tracking script installed

The first thing you want to do is make sure Plausible is already tracking regular visits on your site.

You’ll then need to make sure you have the custom event tracking script added as well, otherwise the ‘plausible’ function will not exist.

If you’re using their WordPress plugin, this step is already taken care of for you.

2. Add the following JavaScript snippet

Next you’ll need to add the following JavaScript snippet to every page where you want your affiliate links tracked.

    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 = !link.target || link.target.match(/^_(self|parent|top)$/i)
        var isRegularClick = !(event.ctrlKey || event.metaKey || event.shiftKey) && event.type === 'click'
        return targetsCurrentWindow && isRegularClick
    }

    var MIDDLE_MOUSE_BUTTON = 1

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

        var link = getLinkEl(event.target)

        if (isAffiliateLink(link)) {
            var eventName = 'Affiliate 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)
            event.preventDefault()
        } else {
            plausible(eventName, { props: eventProps })
        }
    }

    function isAffiliateLink(link) {
        return link && link.href.includes('/go/')
    }

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

You can copy this code into a new .js file and load it onto every page via:

<script src="the_file.js"></script>

If you’re using WordPress, you can use a snippet manager plugin like Code Snippets or WPCodeBox (my personal favorite).

The neat thing about WPCodeBox is that it lets you save each snippet as an external JS file with the click of a button:

Plausible Analytics affiliate link tracking script in WPCodeBox

Whether you want to put it within your <head> tag or just before your </body> tag is up to you.

I chose to put it at the end so that it loads after all the other resources on the page have loaded.

3. Modify the snippet to match your URL structure

If your affiliate links follow the newpulselabs.com/go/productname/ pattern, then you can skip this step.

Otherwise you’ll want to modify line 51 to match whatever pattern you’re using.

4. Create your custom event in Plausible

The last thing is to go to your site settings in Plausible, Create a New Goal > Custom Event, and use the name “Affiliate Link: Click”.

Creating a custom event in Plausible

That’s all there is to it!

You should now start seeing your affiliate link clicks show up in the Goal Conversions section of your report.

Affiliate link goal conversions in Plausible

That’s all for this one. Let me know if you found this tutorial helpful!