views:

103

answers:

2

I run a website that has lots of affiliate links. These links are loaded via AJAX. I'd like to be able to track outbound clicks on these links.

The standard approach to using Google Analytics to track links is to use the pageTracker._trackPageview() function. I've tried this to no avail. Here's my code:

<a href=<?php echo $link_loc ?> target = "_blank" class="affiliateLink" onclick="pageTracker._trackPageview('/event/outgoing?');">Link Text</a>

As is suggested, I put my Google Analytics standard tracking code in between the opening body tag and the above code.

Does anyone see anything wrong with my code? Could the problem be the fact that the links are loaded via AJAX?

+1  A: 
pageTracker._trackPageview('/event/outgoing?');

That should be recording a visit to "/event/outgoing?". Did you mean to record a visit to $link_loc? If so, you'll have to put $link_loc as part of the argument to _trackPageview. You should probably create a string containing only the host and path of the outbound link, minus the http://, and put that into your tracking code.

(I also wonder whether you should perhaps be putting quotes around the href emitted by the php code).

Jonathan Feinberg
My understanding is that you can put any link in the _trackPageview argument. So if I put '/event/outgoing', then Google Analytics will track it as if the user visited that page. Great point about the quotes around the href; I'll add that in.
Jack7890
That's right; you can put anything there. You're recording a visit to "/event/outgoing?". Is that what you meant to do?
Jonathan Feinberg
Yup, it is. Usually I attach a parameter at the end of the link to track where it's linking to...I tried adding those quotes around the href; still no luck. Any other ideas about what might fix it?
Jack7890
You'll have to watch what happens in Firebug.
Jonathan Feinberg
A: 

When you write "these links are loaded via AJAX", I assume that you parse the affiliate links via the affiliateLink class name, and then attach and onclick handler to them. In that case, it may happen, that those handlers were run before the _trackPageview was called you defined in the onclick attribute. Why don't you call the _trackPageview function in the same function that handles the outgoing links?

Török Gábor