views:

818

answers:

2

I posted on this a while ago, and have since done more research and advanced the problem, but I still don't have a solution...

I have a site (www.seatgeek.com) where a lot of links are loaded via AJAX. When a user clicks on one of these links I want to count it as a goal, so I've tried attaching pageTracker._trackPageview() to the links' onClick attribute. But GA isn't recording 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 is not loaded via AJAX and it works fine, so it's definitely a problem specific to AJAX. Also, in my attempts to solve this problem I've also tried adding the onclick events programmatically, e.g.:

<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 box to the record_goal() function, it's clear that the function is getting run. For example, if I change the function to this:

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

Then the 'Hello' alert box displays when a link is clicked. But the pageview to '/event/outgoing' still is not recorded.

I'm completely baffled by what might be causing this. Any advice would be greatly appreciated.

+1  A: 

Are you using a JavaScript library such as Prototype? document.getElementsByClassName is not a standard JavaScript DOM method.

Do you get any JavaScript errors? E.g. open up Firebug or the Firefox Error Console.

In Firebug, if you type pageTracker; in the console, does it tell you that pageTracker is definitely a function and not undefined?

Premasagar
(1) Yes, I use Prototype. But I'm pretty sure document.getElementByClassName is working in this case because the alert('Hello') works when I stick that in. (2) No, I don't get any JavaScript errors. (3) When I use Firebug to inspect on of the links and then click the DOM tab, pageTracker has the value "Object b=window r=1256426203 s=UA-XXXXXXX-9". Is that what you're referring to?
Jack7890
OK, so that all sounds fine. GA _is_ being initiated and your JavaScript isn't producing errors. The problem then is something else.
Premasagar
Yeah, thanks for the thoughts, you're definitely right. I just can't figure out what that "something else" might be.
Jack7890
Another good debugging tip is to take a look at the network tab Images subtab and see if a new requst for __utm.gif for each click. This beacon is how GA tracks events. Take a look at http://code.google.com/apis/analytics/docs/tracking/gaTrackingTroubleshooting.html
joshperry
A: 

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

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

Our Analytics rep recommended this to me and it worked like a charm. "return false" usually says, "hey don't pay attention to the href" (I thought, anyway), but my implementation is working just fine.

Hope this helps!

Lauren