[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


Google Analytics tracking not working for certain links occasionaly.

Posted on 2011-10-04
Medium Priority
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
  • 3
  • 2
LVL 27

Expert Comment

ID: 36931205
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

ID: 37009138
I dont see the url being sent to redirection to google. IF you can point out???
LVL 27

Expert Comment

ID: 37009191
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.

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 37009366
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

Tony McCreath earned 2000 total points
ID: 37009868
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', 'example.com', '');return false;"
function trackLink(link, category, action, label, window) {
    _gat._getTrackerByName()._trackEvent(category, action, label);
    if (window)
        setTimeout(window.open(link.href), 100);
        setTimeout('document.location = "' + link.href + '"', 100);

Open in new window

LVL 27

Expert Comment

ID: 37009935
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.

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Dramatic changes are revolutionizing how we build and use technology. Every company is automating, digitizing, and modernizing operations. We need a better, more connected way to work together as teams so we can harness the insights from our system…
When it comes to security, close monitoring is a must. According to WhiteHat Security annual report, a substantial number of all web applications are vulnerable always. Monitis offers a new product - fully-featured Website security monitoring and pr…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses
Course of the Month20 days, 1 hour left to enroll

873 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