Google Analytics tracking not working for certain links occasionaly.


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.
Perumal RMAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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.
Perumal RMAuthor Commented:
I dont see the url being sent to redirection to google. IF you can point out???
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.

Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Perumal RMAuthor Commented:
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 :)
Tony McCreathTechnical SEO ConsultantCommented:
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


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Development

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.