How to Track Clicks with Google Analytics

by jim on February 24th, 2010

Google Analytics makes it dead simple to track outbound clicks on your site.

First, you need to put your Google Analytics code above the content of your page (or at the very least, before any link you want to track). The function call you will add to the OnClick javascript hook will reference a function in the Analytics code so it needs to be loaded by the time you get to the link. Page speed principles recommend that you put javascript at the bottom, since there can be a little lag in loading it, but in this case there’s no way around it. Google is pretty fast anyway so I consider this a fair tradeoff for additional data.

Then, simply add this bit to your links (if you use Google Website Optimizer, you’ll notice it uses the same mechanism to track clicks):

onClick='javascript: pageTracker._trackPageview("/click/tracking")'

You can change /click/tracking to anything you want. I like to bucket my tracking into different areas and change the term tracking to something that makes sense to me. If it’s affiliate clicks I might put /click/aff/company/tag and programmatically change the company name or the tag based on the page. (you can use php to do this).

Let’s say you want to track a click on an affiliate link on a particular page, you could do this if you were running WordPress:

onClick='javascript: pageTracker._trackPageview("/click/affiliate/<?php echo $post->ID; ?>")'

which would resolve to:

onClick='javascript: pageTracker._trackPageview("/click/affiliate/XXXX")'

with XXXX as the ID of the post.

(remember, to run PHP in a post on WordPress you’ll need to download a php execution plugin and turn off comments for security)

How do you review the data? Go into your Google Analytics and each of the clicks is considered a pageview. I review it by going to Content -> Top Content – View full Report and then searching on the term /click/. Advanced users of Analytics now recognize that you can start assigning goal values and whatnot but I generally avoid that because I don’t find that adds value.

RSS Subscribe Like this article? Get all the latest articles sent to your email for free every day. Just click "Subscribe" and enter your email. Your email will only be used for this daily subscription and you can unsubscribe anytime.

4 Responses to “How to Track Clicks with Google Analytics”

  1. Ben Says:

    Thanks Jim! Wow, I asked you about using Google Analytics to track outbound clicks and the next day this post shows up, thanks!

  2. jim Says:

    I aim to please! 🙂

  3. Barb Friedberg Says:

    Help-I have really been enjoying reading your posts. This one was right on the money for me as it tapped into something I’m working with RIGHT NOW. (As an aside, I am quite accomplished in personal finance, investing etc., not so much in webmastering). So, maybe you can help me… I signed up for a google analytics account just fine. But, when they told me to paste the code in, I HAVE NO IDEA WHERE TO POST IT on my wordpress site and I’m totally terrified of crashing my site. Any advice?

  4. jim Says:

    I’d stick in the footer right before the BODY tag.

Leave a Reply