Google Analyics doesn't track links generated with AJAX
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?
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).
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?
精彩评论