Capturing the direct links for google analytics.

Harsha

Capturing the direct links for google analytics.

There are many occasions where one wants to know how many clicks were attracted by a direct link . It could be a link to download some app or pdf or any other direct link. It is general assumption that we need to have a page with google analytics code for google to capture the visit to the page.

Often to achieve this, a page with google analytics code is created for all direct links and then that page redirects to the actual target. However, there is a simpler way to achieve this, using javascript.

If the page having the link already has google’s analytics code embedded, then all you have to do is to use onclick event on <a> tag to call pagetracker function, for example,

<a href=”http:\\www.yoursite.com\docs\help.pdf” onclick=”javascript: pageTracker._trackPageview(‘help_pdf’);” >Help</a>

In the snippet above, help_pdf will be your reference to search for when looking over reports in the google analytics admin page.

If you want to do more than just track the link, then you can have a javascript function in the page with your logic and a call to above function, for example,

function trackDirectLinks(linkName) {

	//your logic here

	//if analytics code is not embedded in the page, then paste it here.

	var pageTracker=_gat._getTracker("UA-xxxxx-x");
    	pageTracker._trackPageview(page);
}

And call this function in your links,

<a href=”http:\\www.yoursite.com\docs\help.pdf” onclick=”javascript: trackDirectLinks(‘help_pdf’);” >Help</a>

Check in the analytics if the link is getting captured. The results are almost realtime, for me, I had to wait for 15-20 mins before the results in the analytics got updated.

If you don’t see them, then one probable reason apart from hundred other syntax related errors could be that you are using the older version of google analytics code. Copy the latest version (its been around for quite a while now) of analytics code from analytics page and replace all your old code. The advantage of using newer code is you can place it anywhere in your page not necessarily at the bottom, as it uses asynchronous calls which does not block loading of your page content until the call is finished. All browsers support these calls.

about the author

Harsha

  1. Canada Goose Promotion

    December 23, 2011

    that it was quite interesting to read I must line this page in my web site. It could possibly? Therefore you et business relationship in Youtube?