Google Analyics doesn't track links generated with AJAX

时间秒杀一切 提交于 2019-12-08 11:36:55

问题


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:


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).




回答2:


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?



来源:https://stackoverflow.com/questions/1554486/google-analyics-doesnt-track-links-generated-with-ajax

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!