Forum Moderators: DixonJones

Message Too Old, No Replies

Google Analytics - unable to track redirects

onclick="pageTracker._trackPageview('foo', 'bar', '/location');" not workin

         

caribguy

11:22 am on Sep 1, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm a bit mystified by my inability to follow the simple directions in Google's event tracking guide [code.google.com]. And there are too many conflicting answers about this topic in the Google help forums...

I have moved the ga.js initialization script to the top of the page, but this does not seem to make any difference. LiveHTTPHeaders shows that utm.gif is never loaded - i.e. the _trackPageview() function never gets called...

Here's what I have on my page:


<a href="http://www.example.com/links/track_01"
onclick="pageTracker._trackPageview('Sponsors', 'AdPos_2', '/links/track_01');">
<img src="/images/ad-track_0.jpg" alt="">
<br>Buy my widgets</a>

edit/add When I replace onclick with onmousover the call triggers just fine and I can see utm.gif being loaded with a utmp having the 'Sponsor' value (where are the other two?)

caribguy

12:34 pm on Sep 1, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Looks like the redirect is called before the request to Google is complete.

I placed the following in the <head> section and used Firebug to set a breakpoint before window.location is set to its new value...


<script type="text/javascript">
function redirPage(redir_id,redir_link) {
pageTracker._trackPageview(redir_id);
window.location = redir_link;
}
</script>

Lo and behold, utm.gif is requested and Google sends it back.

I'm almost tempted to pull the image with an AJAX request and then wait until the callback to load the redirect. There must be something easier, right?

caribguy

3:00 pm on Sep 1, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Just in case it will help someone else in the future...

The solution, as presented by the "Official" (?) Google Website Optimizer blog, relies on creating a specific pageTracker object, adding a 100ms setTimeout before loading the external URL, and the customary return false; to prevent the non-javascript link to be loaded.

Resulting in something like:


<script type="text/javascript">
function doGoal(that) {
try {
var pageTracker=_gat._getTracker("UA-123456-1");
pageTracker._trackPageview("http://www.example.com");
setTimeout('document.location = "' + that.href + '"', 100)
}catch(err){}
}
</script>

<a href="www.example.com" onclick='doGoal(this);return false;'>Click me</a>

Am I the only one who thinks this is a quite minger solution?

CainIV

12:55 am on Sep 10, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Caribguy, is this for an affiliate website?

I have often wondered how this is handled as well, when the redirect takes place before the code can execute.

The code makes sense because it executes very quickly after the link is clicked and then attributes the referrer information manually. (I am assuming you had to add this)

caribguy

1:42 am on Sep 10, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I guess it can be used for an affiliate site too.

On one of my sites, I use it for external links to local resources to get a general feel for what people do on that section of the site. On another I want to track banner click-thru ratios from Google analytics, in addition to server logs.

The set timeout function delays the request long enough for the click to be registered. I increased it to 500ms however. My own tests showed that 100 was not enough in all cases.

Still think that if you are using Ajax on the site anyway, you might as well load the Google tracking gif with it, and then use the callback function that follows it to jump off the site.