Track outbound links and file downloads with Google Analytics

Track outbound links and file downloads with Google Analytics

One of those not-so-obvious features of Google Analytics is its ability to track outbound links and file downloads. This feature isn’t available by default, because Google Analytics tracks links by way of the tracking script you paste. So when users click from one page to the other on your site, Google Analytics can track the sequence of this visit.


Since your Analytics tracking code doesn’t exist on 3rd party websites you link to, Google Analytics won’t track this (at least not in your account—the site you link to may also have GA, but for privacy reasons, Google won’t share their data with you). Likewise, you can’t embed your GA tracking on a PDF or WMV file.

JavaScript – EventTracking with Google Analytics

Fortunately Google Analytics supports the JavaScript pageTracker function. Inserting this function into the events you want to track (links or file downloads) you can send this information to Google Analytics to track as a pageview.

In order to track events like this without skewing your pageviews, you’ll want to decide on a directory/file name for these virtual “pageviews,” create a duplicate profile track them, and filter out the virtual pages from your main profile. You’ll need to place your Analytics tracking code above these events. Then, you’ll have to embed the actual pageTracker function into the events (links) themselves.

* You’ll need the new GA-specific Analytics tracking for this technique. It won’t work for the legacy Urchin tracking. Click here for instructions on installing the newer GA tracking.

The 4 Steps in tracking outbound links and file plays/downloads

1- Decide on a directory/filename for these events.

Since we’ll be tracking these events as pageviews, we need to decide what we will name these pages (we will be inserting them into the event function). Make sure you give these pageviews a unique file/directory structure, because we’ll want to filter them out of your main profile, and we don’t want to filter out any real pageviews along with them. Also keep in mind that you’ll probably want to easily separate file downloads, video plays, and pdf downloads. So your best best is to give them all a common subdirectory, and within this, organize them under different subdirectories.

For example:

For external links

/eventtracking/external/searchengineland.com

For PDF downloads

/eventtracking/pdf/6-steps-for-better-SEO

This way, we can track all of these events as pageviews in the “/eventtracking/” directory. Keeping them all in the same virtual directory means you only have to filter out one directory from your main profile. It also means you’ll also only have to include one directory in your duplicate profile.

2- Create a duplicate profile where you can track these events as pageviews

-On your Overview page click Add a Website Profile (bottom left link).
-Select Add a Profile for an existing domain, name your new profile, and click Finish
-Find your new profile in your Account Overview and click Edit
-Scroll down a bit and click + Add Filter
-In your Create New Filter page, choose Add new Filter for Profile
-Enter your Filter Name (choose a name that makes sense)
-For Filter Type, choose Include only traffic to a subdirectory
-The Subdirectory field will now appear. Enter the name of the subdirectory you chose earlier, and save your changes.
* The ^ is a regular expression that will show up by default. Just leave it in. I will explain this character in a future post.

3- Filter out this subdirectory out of your main profile to avoid skewing your pageviews

Now that you have a profile where you can track these events as visits to a subcategory, you want to make sure you filter out these “visits” to avoid artificially inflating your pageview numbers.

-In your Overview page, find your main website profile and click Edit
-Scroll down a bit and click + Add Filter
-In your Create New Filter page, choose Add new Filter for Profile
-Enter your Filter Name (choose a name that makes sense)
-For Filter Type, choose Custom Filter and choose Exclude from the new list of options
-For Filter Field, choose Request URI and for Filter Pattern, enter the proper subdirectory name you chose earlier and click Save Changes (again, it won’t hurt to use the ^ regular expression).

Now you can run the event tracking JavaScript without artificially inflating your page views on your main profile.

4- Place your GA tracking above the events

In order to recognize and parse the pageTracker JavaScript, you will need to place your GA tracking above these events, so that it can set the pageTracker._trackPageview() function before the links or file downloads you want to track call to them. If possible, place the tracking just after the opening body tag, like so;

<body>
<script type=”text/javascript”>
var gaJsHost = ((“https:” == document.location.protocol) ? “https://ssl.” : “http://www.”);
document.write(unescape(“%3Cscript src='” + gaJsHost + “google-analytics.com/ga.js’ type=’text/javascript’%3E%3C/script%3E”));
</script>
<script type=”text/javascript”>
try {
var pageTracker = _gat._getTracker(“UA-xxxxxx-x”);
pageTracker._trackPageview();
} catch(err) {}</script>

5- Embed the event tracking function into your links and/or file plays/downloads.

In order to track these events, you’ll need to append TrackPageview to the events you want to track.

Tracking external links

Whereas your standard link would look like this:

a href=”http://www.searchengineland.com/”>Search Engine Land

Your new link will look like this:

a href=”http://www.searchengineland.com/” onClick=”javascript: pageTracker._trackPageview(‘/eventtracking/external/searchengineland.com’);”>Search Engine Land

Tracking file downloads

Whereas your standard link would look like this:

a href=”/pdf/6-steps-for-better-SEO.pdf”>6 Steps for better SEO

Your new link will look like this:

a href=”/pdf/6-steps-for-better-SEO.pdf”” onClick=”javascript: pageTracker._trackPageview(‘/eventtracking/pdf/6-steps-for-better-SEO’);”>6 Steps for better SEO

Wait and See

Now that you’ve implemented the JavaScript function, all that’s left is to wait and watch the metrics roll in. This will give you a deeper look at how visitors are using your website. Which links are people following? Which files are they more interested in downloading. For even deeper analysis, use Advanced Segments to really dive deep into this usage. Perhaps a certain keyword is diving the bulk of a specific download. Or Perhaps visitors from forums tend to show interest in a certain link you’re providing.

Sources for further reading:

8 thoughts on “Track outbound links and file downloads with Google Analytics”

  1. Can GA do this?

    Say I am on a forum and post a message. In the message I put a link to a CNet story. It might look like: http://cnet.com/story.

    Is there a way to track and format the URL so it looked something like: http://savemybutt.com/cnet.com/story?

    I know even Google does this with links from their News site. Click on a news link and it looks like:

    http://news.google.com/news/url?sa=t&ct2=us%2F0_0_s_0_0_t&usg=AFQjCNFm9AnkcP6yEjp8gPfk2pJiI8rF5w&sig2=T0rsjWSwpezqRPYvEhxXDA&cid=1471657680&ei=U2IDS4C-FYzyMtfisSQ&rt=HOMEPAGE&vm=STANDARD&url=http%3A%2F%2Fwww.google.com%2Fhostednews%2Fafp%2Farticle%2FALeqM5gCnXDf-TeQnuXUC8l-ZEGP6XeR9Q

    That is pretty long, but if I paste that into a forum post, Google tracks it because it goes through their site first.

    Thanks in advance,

    Mike

  2. Hi Mike,

    In order to track this click you’d want to create a page that redirects to http://cnet.com/story, and place tracking on that redirecting page so you can track the click. You can read more about this on a previous post of mine;

    http://www.seoracle.com/google-analytics-how-to-tutorial/analytics-redirect-tracking

    This of course, applies to redirects. So you can make “http://savemybutt.com/cnet.com/story?” redirect to http://cnet.com/story and track these clicks. However, I’m not sure what you can do to keep the display URL as “http://savemybutt.com/cnet.com/story?”

  3. This is a great post. I’ve been tracking outbound links for over a year, but never setup the filters. Thanks for the info on how to do that.

    I’m wondering about the necessity of placing the GA code above all the outbound links. My GA code is at the bottom of my page and my outbound links are still tracking found. Have you found certain situations where it is necessary to move the code above the links?

  4. Hi Brent,

    Thanks for stopping by. According to Google;

    Important: if your pages include a call to _trackPageview, _setAllowLinker _trackTrans(), your Analytics tracking code must be placed in your HTML code above any of these calls. In these cases the tracking code can be placed anywhere between the opening tag and the JavaScript call.

    http://www.google.com/support/googleanalytics/bin/answer.py?answer=55521

    So I was going off of this. However, our developers have been implementing these calls on our websites without moving the tracking code above and it seems to work fine.

Leave a Comment