Hacker News new | ask | show | jobs
by jgalt212 4947 days ago
The article is interesting, but gives some bad instructions on how to track outbound links and also probably how to track form submissions. Because of the asynchronous nature of communications back/forth from Google Analytics if you load a new page before an event is properly tracked, you won't be able to see it on the GA Dashboard. Bad intel is worse than no intel at all.

Here's what Google itself has to say on the matter: http://support.Google.com/analytics/bin/answer.py?hl=en&...

Here's how to correct one code example from the blog post:

  //this function is OK, but probably an unnecessary abstraction of a one-liner
  function trackEvent(category, action, label) {
    window._gaq.push(['_trackEvent', category, action, label])
  }

  //this event handler will not track some non-negligible percentage of events
  $("article a").click(function(e) {
    var element = $(this)
    var label = element.attr("href")
    trackEvent("Outbound link", "Click", label)
  });


  //corrected outbound link event handler which gives GA 100 ms to register
  //the event.  higher than 100 risks UX degradation, lower increases the %
  //of untracked events.  100 ms is happy medium  
  $("article a").click(function(e) {
    e.preventDefault();  //stay on the page for now
    var element = $(this)
    var label = element.attr("href")
    trackEvent("Outbound link", "Click", label)
    //leave the page after a short delay
    window.setTimeout("window.location.href='" + label + "'", 100);
  });
1 comments

I'm a bit suspicous of that method... will it still work for people right clicking and opening in new window, or shift-clicking, or middle-clicking, or right clicking and copying the URL?