Google Analytics: Track Outbound Links And Special Pages

Google analytics has lots of functionality but it does not provide an easy way of tracking outbound links on click. MyBlogLog provides this functionality is very useful way but that is free only 10 entry each days. That also has restriction on free version that you cannot see report after a week. So, you cannot look past the analytics data and analyze the outgoing links.

MyBlogLog.com Analytics Interface (Free one):
MyBlogLog Analytics

The data part is empty as I have stopped using that from quite some time. If you have not used that then you will like to see what it does. It looks good.

Code for implementing Outbound links on your website for new Google Analytics:

On new Google analytics code, you will see - var _gaq


<script>
jQuery(function() {
	jQuery("a[href^=http]").not('a[href*="satya-weblog.com"]').click(function() {
		_gaq.push(['_trackEvent', 'outgoing', 'Links' ,"'" + this.href + "'"]);
	});
});
</script>

Just replace the satya-weblog.com with your site address and it will started working. It is using jQuery library to traverse all links and put onClick event on matching links.

Now, little explanation of the code. What is it doing?

It check for links starts with "http" (a[href^=http]). As you may be using relative address for your own website or blog. After that it checks the the link is not an internal link. Then add Click event to those links.

Here is a syntax of the _trackEvent function:

_trackEvent(category, action, opt_label, opt_value);

I am using first three only (category, action, opt_label). Category is Outgoing, Action is Links and opt_label is outgoing URL. It is full URL.

It was all for event tracking in Google analytics. For more details on _trackEvent(), please visit here.

Now, Tracking code for tracking special pages:

I have planned to put few tools at this location: www.satya-weblog.com/tools/*

and I want to see how if anyone is visiting or not! For that tracking event could be good but somehow I thought to use pageView with special tag for those pages visit. Because those pages are hosted on my blog itself, so I thought to use pageView instead of tracking event. But now I believe that tracking event with new tag would be more effective for my purpose. I may change it latter.

Anyway for some reason you want to track few page view separately.  For that reason, following code will work:


<script type='text/javascript'>
_gaq.push(['_trackPageview', '/tools/htmlencode-blogger']);
</script>

Use both versions of code after these lines:

<script type='text/javascript'>
 var _gaq = _gaq || [];
 _gaq.push(['_setAccount', 'UA-1240823-5']);
</script>

UA-1240823-5 is my Account Id for this site. Your Account will be different.
If you have any doubt in implementation, then you can see the source code of my pages and see if it helps you!

  • # 1 - by Jesse

    Hi Satya,

    I am wondering if this would have any problems working with a dynamic php template based site? Like wordpress or something similar?

Comments are open for an year period. Please, write here on Facebook page.