Tracking Outbound Link Clicks on readthedocs Platform

Sherry Wei
2 min readApr 30, 2021

Aviatrix documentation site docs.aviatrix.com is hosted on the readthedocs platform. We have enabled Google Analytics on the site by following the instructions here. With this enabled, we are able to track page views on the Google Analytics.

On some of the pages, we have outbound links that take the visitor to, say AWS Marketplace, to launch the Aviatrix Controller, and we like to track the stats on these link clicks.

This article shares with you how to accomplish that.

In order to track the outbound link clicks, you need to use Google Tag Manager. It turns out that when you enable Google Analytics on readthedocs platform, the Google Tag Manager is also installed. So all you need to do is to embed some javascript in the specific page where the outbound link is.

Copy and insert the following text to the top of the example.rst file where there is an outbound link.

.. raw:: html<script>
/**
* Function that registers a click on an outbound link in Analytics.
* This function takes a valid URL string as an argument, and uses that URL string
* as the event label. Setting the transport method to 'beacon' lets the hit be sent
* using 'navigator.sendBeacon' in browser that support it.
*/
var getOutboundLinkAndOpen = function(url) {
gtag('event', 'click', {
'event_category': 'outbound',
'event_label': url,
'transport_type': 'beacon',
'event_callback': function() {}
});
}
</script>

The following code is a demonstration that when “Aviatrix Secure Networking Platform Metered with Copilot” is clicked, a new browser window opens to take the visitor to “https://aws.amazon.com/marketplace/pp/B08NTSDHKG?qid=1616801289672&sr=0-2" and in the meantime it sends an event with category “outbound”, action “click” and url to Google Analytics. You can change the event category, action and url names to suit your needs.

.. |marketplace_metered_link| raw:: html<a href="https://aws.amazon.com/marketplace/pp/B08NTSDHKG?qid=1616801289672&sr=0-2" target="_blank" onclick="getOutboundLinkAndOpen('https://aws.amazon.com/marketplace/pp/B08NTSDHKG?qid=1616801289672&sr=0-2');">Aviatrix Secure Networking Platform Metered with Copilot</a>|marketplace_metered_link|

On Google Analytics, Realtime -> Events, you should be able to immediately see the “outbound” event if the link is clicked.

To view the complete source code, refer to this github link.

--

--