Google Analyics不会跟踪使用AJAX生成的链接

时间:2022-08-22 15:13:59

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.

我经营一个有很多会员链接的网站。这些链接通过AJAX加载。我希望能够跟踪这些链接的出站点击次数。

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:

使用Google Analytics跟踪链接的标准方法是使用pageTracker._trackPageview()函数。我试过这个无济于事。这是我的代码:

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

如我所知,我将Google Analytics标准跟踪代码置于开始正文标记和上述代码之间。

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

有人看到我的代码有什么问题吗?问题可能是链接是通过AJAX加载的吗?

2 个解决方案

#1


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.

那应该记录访问“/ event / outgoing?”。你的意思是记录访问$ link_loc?如果是这样,你必须将$ link_loc作为参数的一部分放到_trackPageview中。您应该创建一个仅包含出站链接的主机和路径的字符串,减去http://,并将其放入跟踪代码中。

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

(我也想知道你是否应该在PHP代码发出的href周围加上引号)。

#2


0  

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?

当你写“通过AJAX加载这些链接”时,我假设你通过affiliateLink类名解析联盟链接,然后附加和onclick处理程序给他们。在这种情况下,可能会发生这些处理程序在onclick属性中定义的_trackPageview被调用之前运行。为什么不在处理传出链接的同一函数中调用_trackPageview函数?

#1


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.

那应该记录访问“/ event / outgoing?”。你的意思是记录访问$ link_loc?如果是这样,你必须将$ link_loc作为参数的一部分放到_trackPageview中。您应该创建一个仅包含出站链接的主机和路径的字符串,减去http://,并将其放入跟踪代码中。

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

(我也想知道你是否应该在PHP代码发出的href周围加上引号)。

#2


0  

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?

当你写“通过AJAX加载这些链接”时,我假设你通过affiliateLink类名解析联盟链接,然后附加和onclick处理程序给他们。在这种情况下,可能会发生这些处理程序在onclick属性中定义的_trackPageview被调用之前运行。为什么不在处理传出链接的同一函数中调用_trackPageview函数?