Google Analytics does not track links generated with AJAX

I am running a website with a lot of affiliate links. These links are loaded via AJAX. I would like 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 I said, I put my Google Analytics tracking code between the body open tag and the above code.

Does anyone see something wrong with my code? Could the problem be with the links being loaded via AJAX?

0


source to share


2 answers


pageTracker._trackPageview('/event/outgoing?');

      

This should be the entry "/ event / outgoing?" Did you want to record your visit to $link_loc

? If so, you will have to put $ link_loc as part of the argument in the _trackPageview. You should probably create a string containing only host and outbound link path, minus http://

, and put that in your tracking code.



(I'm also wondering if you should put quotes around the href emitted by PHP code).

+1


source


When you write "these links are loaded via AJAX" I am assuming that you parse the affiliate links through the class name affiliateLink

and then attach the onclick handler to them. In this case, it may happen that these handlers were executed before the one _trackPageview

you defined in the attribute was called onclick

. Why don't you call the function _trackPageview

in the same function that handles outbound links?



0


source







All Articles