Google Analytics tracking not working for certain links occasionaly.

Posted on 2011-10-04
Last Modified: 2013-12-08

I am having a issue with google analytics tracking my events from time to time.

We use asynchronous code for all of our tracking.

When i click the HOME button on header of my webpage, we wanted to have the click of HOME button tracked and browser redirected to HOME page.

jQuery basically rewrites the links to add google custom tracking code.

	/* Function to automatically add tracking to links on header Home */
	$("#header #btn-home").each(function(){
		var linkValue=$(this).text();
		var onClickValue="_gaq.push(['_trackEvent', 'MAIN_NAV', 'CLICK_HOME_BUTTON', '"+linkValue+"']);"
		$(this).attr("onClick", onClickValue);

Open in new window

The above jquery rewrites the link

<a class="nav-btn active" id="btn-home" href="/group/gi/index.htm">Home</a>

Open in new window


<a class="nav-btn active" id="btn-home" href="/group/gi/index.htm" onclick="_gaq.push(['_trackEvent', 'MAIN_NAV', 'CLICK_HOME_BUTTON', 'Home']);">Home</a>

Open in new window

When i say it works time to time, it means that some time when i click it tracks and some other time it doesn't. So when digging deep into the issue, some one figured out that the page refreshes even before Google could receive the tracking code. So they are asking me to introduce a delay of 100 milliseconds for each anchor within the website, which i feel is not right. There should be some other way for this. So wanted to turn out to experts here and get an opinion on this.
Question by:Perumal_RM
    LVL 26

    Expert Comment

    JustAMod: the zone which often answers this sort of question is the WebTrends/Stats zone.  Misc Web Dev and Javascript are two good other places.

    This is an excellent short writeup on using jquery to modify links for Google Analytics:

    They don't mention timing delays, so I don't think that's a normal requirement.  

    However, the redirection to home could well screw up the timing on javascript execution.  

    In the article above, they send the url for redirection to Google, so there's no need for redirection in your own page.  It's odd, but that might be the thing to do for your home page redirection -- have Google do it after they record the Goodle Analytics click.

    Author Comment

    I dont see the url being sent to redirection to google. IF you can point out???
    LVL 26

    Expert Comment

    I was thinking of where the page says:
    "This code makes the call to Google Analytics and passes a page path of “/outgoing/” and the url of where the click was headed."

    You're right -- Google Analytics isn't doing the actual redirection.

    If you can take out your redirection to home on the page, then a lot of things will probably become easier.

    If you want help, you probably should try some things and ask questions.  It's more likely to work if you don't leave weeks between your responses.


    Author Comment

    The issue is still open though. So even within weeks response or months response doesnt makes sense. The approach what we did though was introduce 100ms delay on every anchor link and figured out that that didnt even helped us in anything.

    I think there should be some other way to attack this, I am being pushed to a corner on a wrong direction by my company architects :)
    LVL 23

    Accepted Solution

    I looked into this issue in the past and it does seem the only solution is to add a delay before the href is processed.

    I wrote a function to handle it, in case a better solution came up...
    // onclick="trackLink(this, 'Outbound Links', '', '');return false;"
    function trackLink(link, category, action, label, window) {
        _gat._getTrackerByName()._trackEvent(category, action, label);
        if (window)
            setTimeout(, 100);
            setTimeout('document.location = "' + link.href + '"', 100);

    Open in new window

    LVL 26

    Expert Comment

    Perumal_RM: my point was that if you don't respond to suggestions or answers for weeks, the question looks abandoned and is less likely to get further attention.

    If you also don't choose to act on answers you get, then we can't help further.  This question is only open because you are not working on it.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Read about why website design really matters in today's demanding market.
    I've been asked to discuss some of the UX activities that I'm using with my team. Here I will share some details about how we approach UX projects.
    This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
    The viewer will learn how to dynamically set the form action using jQuery.

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now