Google Analytics Async: Events Tracking Callback?
Asked Answered
O

3

13

I wish to use event tracking to record clicks on a specific type of link to another website. I am using jQuery, and the code I currently have is:

$('a.website').click(function(event) {
    var href = $(this).attr('href');
    try {
        _gaq.push(['_trackEvent', 'website', 'click', href]);
    } catch(err) {}
});

However, after seeing referrer information from the other site, I don't belive this is accurately tracking the clicks, probably because _gaq.push is asynchronous, and the request has not been received before the browser navigates to the url, and terminates any javascript running on the current page.

Is there any way to detect that the _gaq.push function has completed successfully, so I can use event.preventDefault() and document.location to navigate to the link after the event has been recorded?

Obviate answered 16/11, 2011 at 5:16 Comment(0)
T
9

How about solution from this answer?

  // Log a pageview to GA for a conversion
  _gaq.push(['_trackPageview', url]);
  // Push the redirect to make sure it happens AFTER we track the pageview
  _gaq.push(function() { document.location = url; });
Tara answered 16/11, 2011 at 5:23 Comment(5)
Thanks for the suggestion. I'm going to attempt a test using this, and I'll report back.Obviate
I'm going to go with this method. I was unable to collect enough data to prove that this method was more accurate, but it didn't result in any more missing events.Obviate
This actually does not work. Watch your network panel in your Firebug or Webkit Inspector. You'll see __utm.gif gets interrupted by the form submit and never gets tracked.Ataractic
@EricD.Fields i'm wondering if the request gets executed anyway, and the tracking succeeds, even if the __utm.gif is not sent back to the client. is it possible?Unemployed
@Unemployed is correct, it does not matter if the client receives a response as long as the server received the request.Petite
G
12

Use Google Analytics hitCallback

You can set a custom callback on the tracker object by using:

_gaq.push(['_set', 'hitCallback', function(){}]);

I described the code for it a little more in-depth here: Track event in Google Analytics upon clicking form submit

Grim answered 17/9, 2012 at 14:39 Comment(1)
I don't see a _set method in Google's tracker documentation. Can you provide more information on this method?Obviate
T
9

How about solution from this answer?

  // Log a pageview to GA for a conversion
  _gaq.push(['_trackPageview', url]);
  // Push the redirect to make sure it happens AFTER we track the pageview
  _gaq.push(function() { document.location = url; });
Tara answered 16/11, 2011 at 5:23 Comment(5)
Thanks for the suggestion. I'm going to attempt a test using this, and I'll report back.Obviate
I'm going to go with this method. I was unable to collect enough data to prove that this method was more accurate, but it didn't result in any more missing events.Obviate
This actually does not work. Watch your network panel in your Firebug or Webkit Inspector. You'll see __utm.gif gets interrupted by the form submit and never gets tracked.Ataractic
@EricD.Fields i'm wondering if the request gets executed anyway, and the tracking succeeds, even if the __utm.gif is not sent back to the client. is it possible?Unemployed
@Unemployed is correct, it does not matter if the client receives a response as long as the server received the request.Petite
U
1

I've faced similar issue with the external link tracking. My salvation was using OnMouseDown event instead of OnClick.

Ulu answered 17/11, 2011 at 7:47 Comment(3)
Although this may get better accuracy through a 'head-start', I'd think this method still wouldn't guarantee that the event track request has been sent before the browser navigates elsewhere? Also this may cause false readings because the user may mouse-down and then move off the link.Obviate
You can try testing it out with the Real-time tracking in the new interface of GA. OnMouseDown works for me, try OnMouseUp or something more complicated.Ulu
What about users who press the Enter button instead of using the mouse?Enedina

© 2022 - 2024 — McMap. All rights reserved.