How to Track Out-Bound Links
with Google Analytics
As I promised to my mastermind group, here is a super simple and reliable way to track when visitors click on a link that leads them away from your website.
Without any modification, Google's Analytics software will only register a page view when that page is first loaded. The software is really pretty powerful, though, and will let you track all sorts of crazy things with just a little extra programming.
An outgoing link configured for tracking will look a bit like the following example. I have split it into multiple lines simple for the sake of readability.
<a href="http://example.com" onclick="javascript: _gaq.push(['_trackPageview', '/example']);" target="_blank" rel="nofollow">
Tracking Virtual Page Views
The portion that begins with "onclick" is what instructs the Google Analytics code to register that a page has been viewed when the link is clicked. Since the page doesn't really exist on your site (thus the name "Virtual Page View") you are free to set whatever path you would like to use for tracking.
<a onclick="javascript: _gaq.push(['_trackPageview', '/example']);" >
In this example, clicks on this page will show up in your analytics reports as if someone has viewed the page '/example'. You can set this parameter to anything you want but keep in mind that it should not conflict with any real pages on your site.
Opening in a New Window
This is the real secret behind making this technique work. This little snippet does nothing other than cause the web browser to open a new tab or window when the link is clicked.
<a target="_blank" >
Since loading a new web page causes everthing on the current page to stop, Google Analytics may not have enough time to properly register the virtual page view. However, when the new page is opened in a new window, the tracking software is far more likely to be able to finish its work.
Hopefully this is a complete enough explanation that you can make use of the technique on your own site. If you have questions, please leave them in the comments.
Here is a super simple and reliable way to track when visitors click on a link that leads them away from your website.
By brett on April 11, 2011
The part that wasn’t clear to me when doing this recently was that I had to set up ‘asynchronous tracking’ - adding a piece of script to my header text that was formerly just my basic Google Analytics tracking code. It’s been a week and already I’ve forgotten exactly where I found it.
Also I selected ‘event tracking’ rather than virtual page view, but don’t really understand the major difference.
I did follow your advice to add the target=“_blank”. Thanks for clarifying and the simple instructions!