How to Track Outbound Links using Google Analytics
How do I track outbound links in Google Analytics 4?
Quick Answer: In the latest version of Google Analytics (GA4), outbound clicks are automatically tracked. To view this data, click on the “Real Time” section of your Google Analytics dashboard. Then, choose “Events,” then “Overview.” Next, click “outbound-link,” and set the secondary dimension to “Source.” This will show you all the sources of outbound link traffic.
Author’s Note: The process outlined below is not applicable to the current Google Analytics anymore. To know the latest process on tracking outbound links clicks, read How to Track Outbound Link Clicks with Google Tag Manager.
Overview
For sure you have some outbound links in your website going to other websites for various reasons. We sometimes cite other websites as sources of fun, information, or attribution, etc. You can track which outbound links are clicked by your users by adding a short, simple code in your Google Analytics Tracking code.
Where are you Going?
For sure you’d like to see where your visitors are headed to after they’ve been through your website. Tracking where all your outbound traffic is headed can tell you which outbound links are doing well in your website and which are not. Here’s the code to track all outbound links in your website:
<script type=”text/javascript“>
var a = document.getElementsByTagName(‘a’);
for(i = 0; i < a.length; i++){
if (a[i].href.indexOf(location.host) == -1 && a[i].href.match(/^https:///i)){
a[i].onclick = function(){
_gaq.push([‘_trackEvent’, ‘outgoing_links’, this.href.replace(/^https:///i, ”)]);
}
}
}</script>
What this code does is it gets all your <a> tags (which is what your links are made out of) and appends an onClick function to it. The onClick function will create an Event Category which will be outgoing_links, and the event action will be the URL.
Put this below your Google Analytics Tracking code like this (highlighted in blue):
<!–THIS IS YOUR GOOGLE ANALYTICS TRACKING CODE–>
<script type=”text/javascript”>
var _gaq = _gaq || [];
_gaq.push([‘_setAccount’, ‘UA-xxxxxxxx-x’]);
_gaq.push([‘_trackPageview’]);(function() {
var ga = document.createElement(‘script’); ga.type = ‘text/javascript’; ga.async = true;
ga.src = (‘https:’ == document.location.protocol ? ‘https://ssl’ : ‘https://www’) + ‘.google-analytics.com/ga.js’;
var s = document.getElementsByTagName(‘script’)[0]; s.parentNode.insertBefore(ga, s);
})();
var a = document.getElementsByTagName(‘a’);
for(i = 0; i < a.length; i++){
if (a[i].href.indexOf(location.host) == -1 && a[i].href.match(/^https:///i)){
a[i].onclick = function(){
_gaq.push([‘_trackEvent’, ‘outgoing_links’, this.href.replace(/^https:///i, ”)]);
}
}
}
</script><!–END RECORD OUTBOUND LINKS CODE–>
This should track all your outbound links. You can check this at the Content -> Events section when you go to your Google Analytics account.
You will be able to see something like this:
Event Category is the name of the event. In this example, it is the name of my outbound links – if you noticed, I customized my Events Categories giving them names like ‘SEO Services SB’, ‘SEO School SB’, ‘Facebook Group SB’, etc. You can also customize your Event Categories by using the onClick function for Google Analytics Events tracking here.
Total Events is the times the Event occurred – in this case, it’s the number of times my outbound link was clicked by a user.
Unique Events is the times the Event occurred for a Unique IP address – in this case, it’s the number of times my outbound link was clicked by a Unique user.
Event Value and Avg Value can be set on Goals Settings. As of now, we will not use these.
Where are You Going?
If you check out my Event Categories, I name my links to my understanding. For example, I append an ‘SB’ label to my sidebar links to see if they are clicked more often than their in-content or image counterparts. For this timeframe, it strongly suggests that my sidebar links are the most clicked links – particularly my SEO Services link and SEO School link.
If you want to check your outbound links without the categories simply click the ‘outbound-article’ Event Category and it should take you to the raw URLs of your outbound links.
The results should look like this:
One More Trick
When you’re tracking site-wide outbound links, you can check which pages prove most effective in compelling your users to click-through. Simply add a secondary dimension like this:
Then filter the results such that it won’t show you anything but your site-wide links (you should manually put a customized onClick Events Tracking code in your Site-wide links to effectively track them.) First, click on the Advanced Filter link:
Then Exclude all non site-wide links (Event Category) in my case it is labeled as an ‘outbound-article’ and an ‘outbound-menu’ :
Your Google Analytics should show you data that looks like this:
This data tells me on which landing page has any of my site-wide links have been clicked. This is especially helpful if you are testing sidebar ads, banners, links or email signup forms.
Tips for Keeps: Implementing this Google Analytics Outbound Links Tracking code is a piece of cake – paste the code beside your Google Analytics Tracking code to start gathering your outbound user activity data.