0
[cross-posted on Google Products Forum http://productforums.google.com/d/topic/analytics/ZrB14a-6gqI/discussion ]

I am using the following code at http://www.cs.bris.ac.uk/Research/Algorithms/

<script type="text/javascript">

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-XXXXXXXX-X']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();

</script>
<script type="text/javascript">
function recordOutboundLink(link, category, action) {
try {
var myTracker=_gat._getTrackerByName();
_gaq.push(['myTracker._trackEvent', category , action ]);
setTimeout('document.location = "' + link.href + '"', 100)
}catch(err){}
}
</script>

which I just copied directly from http://support.google.com/analytics/bin/answer.py?hl=en&answer=1136920 .

However, it doesn't actually seem to report any clicks on the links where I have added onClick="recordOutboundLink(this, 'Outbound Links', 'Postdoc advert');return false;", for example. I have seen a number of complaints about this online but I haven't found a solution that works.

What am I doing wrong?

P.S. The closest related online complaint seems to be http://productforums.google.com/forum/#!topic/analytics/4oPBJEoZ8s4 which just claims the code is broken.

3 Answers 3

4

Here's what I'm using, which has been working for me. I'm using jQuery to add the onclick handler to any link with a class of "referral", but I'd expect adding it directly in the HTML to work as well.

  $(function() {
    $('.referral').click(function() {
      _gaq.push(['_trackEvent', 'Referral', 'Click', this.href]);
      setTimeout('document.location = "' + this.href + '"', 100);
      return false;
    });
  });

edit: I believe your syntax for invoking a tracker by name is wrong. Since you aren't using a named tracker when you set up tracking at page load, you shouldn't try to name it later either. See the documentation for _gaq.push.

More precisely:

  1. The var myTracker declaration is unused, so you can just delete that line. Variables declared within the scope of recordOutboundLink aren't visible when other functions, such as _gaq.push, are running, so it can't be relevant.
  2. You should simply use '_trackEvent' instead of 'myTracker._trackEvent'.
4
  • Thanks. As I have just copied the code in my question verbatim from the Google web page I don't fully understand it. How should it be fixed do you think?
    – Simd
    Nov 3, 2012 at 20:20
  • 1
    @Raphael, I've edited my suggestion to more clearly state what I'm recommending. Nov 3, 2012 at 20:29
  • Thanks. I have applied your suggestion and will wait to see if it works. I think google only updates the stats once a day online.
    – Simd
    Nov 3, 2012 at 20:36
  • 1
    @Raphael, I used to think so too, but as it turns out, you can change the date range in Analytics to include today's date. There's still some small delay before clicks show up, but you don't have to wait a whole day. Nov 3, 2012 at 20:40
2

You can also try this automated external link script

0
0

Set a longer timeout 2 seconds maybe, as it takes a certain amout of time for the _gaq.push to actually push to the server, and 100 milliseconds isnt long enough for it to send (the push gets cancelled as soon as the document.location changes). Unless _gaq.push uses a blocking call (doesnt execute the next line till the push is complete), but i dont think that is the case i think most of that uses asynchronous requests.

3
  • 2
    Waiting two seconds seems extreme--users won't like that amount of delay at all. Nov 3, 2012 at 16:39
  • Its either that or come up with a way of detecting when .push is done as not everyone has the same connection speed or has a longer route between servers so 100 milliseconds might not be long enough for every user. i was just using 2 seconds as an example Nov 3, 2012 at 16:41
  • _gaq.push with 100 ms is probably enough. If the code in ga.js hasn't loaded yet (unlikely) the push is just a push to an array, and indeed it might take more than 100 ms for ga.js to load, but if ga.js was already loaded, the push immediately (sync) sends the hit.
    – Barak
    Nov 7, 2012 at 23:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.