Google Analytics GoogleTracker feature not working when loaded via AJAX

I wrote about this a while ago and have done more research and cutting edge problem since then, but I still don't have a solution ...

I have a website (www.seatgeek.com) where a lot of links are loaded via AJAX. When a user clicks on one of these links, I want to treat it as a target, so I tried to bind pageTracker._trackPageview () to the onClick attribute of the links. But GA does not record these clicks and I have no idea why. Here's the code for one of these links:

<a href="<?php echo $tickets[$x][3] ?>" target = "_blank" class="buyTicketsLink" onClick="pageTracker._trackPageview('/outgoing/event4.php');">BUY TICKETS</a>

      

I've tried the above code in situations where the link won't load via AJAX and it works great, so this is definitely an AJAX specific issue. Also, when trying to solve this problem, I also tried to add onclick events programmatically, for example:

<script>
function attach_goal_tracking() {
var links = document.getElementsByClassName("buyTicketsLink");
for(var i=0; i<links.length; i++) {
links[i].onclick = record_goal;
}
}

function record_goal() {
pageTracker._trackPageview('/event/outgoing');
}
</script>

      

This doesn't work either. But when I add a test alert field to the record_goal () function, it clears that the function is being run. For example, if I changed the function to this:

function record_goal() {
alert('Hello');
pageTracker._trackPageview('/event/outgoing');
}

      

Then the "Hello" dialog box appears when the link is clicked. But the pageview on "/ event / outgoing" is still not logged.

I am completely puzzled as to what might be causing this. Any advice would be greatly appreciated.

+2


source to share


3 answers


Are you using a JavaScript library like Prototype? document.getElementsByClassName

is not a standard JavaScript DOM method.

Do you have JavaScript errors? For example. open Firebug or Firefox Error Console.



In Firebug, if you type pageTracker;

in the console, will it tell you what is pageTracker

definitely a function, not undefined

?

+1


source


Try adding "return false" after your call to trackPageView (), so ...

onClick="pageTracker._trackPageview('/outgoing/event4.php'); return false">

      



Our analyst presented me with this recommendation and it worked like a charm. "return false" usually says "hey, ignore the href" (I thought anyway), but my implementation works fine.

Hope this helps!

+1


source


If you are still using the Google Analytics Asynchronous Load method , the following snippet will accomplish the same task:

_gaq.push (['_ trackPageview', '/ anything']);

or (@Lauren code)

onClick = "_ gaq.push (['_ trackPageview', '/outgoing/event4.php']); return false">
+1


source







All Articles