使用Javascript和PHP跟踪传出链接 [英] Tracking outgoing links with Javascript and PHP

查看:242
本文介绍了使用Javascript和PHP跟踪传出链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试使用jQuery,但它不工作。

I have tried it using jQuery but it is not working.

<script>
    $("a").click(function () { 
      $.post("http://www.example.com/trackol.php", {result: "click"
  }, "html");
    });
</script>
<a href="http://www.google.com">out</a>


推荐答案

要获得最好的结果,您的方法

To get the best results you should change two things in your approach


  1. 使用 onmousedown 而不是 code> - 这种方式,您需要额外的几毫秒才能完成跟踪请求,否则浏览器可能无法启动与您的跟踪器的连接,因为它已经离开原始页面。缺点是你可能会得到一些假阳性计数,因为点击用户可能不会完成点击(例如,保持鼠标按钮和移动光标远离链接),但总体来说它是一个牺牲你应该做的 - 考虑更好的跟踪质量。

  2. 而不是Ajax调用( $。post('...'))使用image pre-fetcher( new Image()。src ='...')。跟踪器不是图像的事实在这种情况下是不相关的,因为你不想使用结果图像,你只是想向服务器发出请求。 Ajax调用是一种双向连接,所以它需要更多的时间,如果浏览器已经离开,但图像预取器只是发送请求到服务器可能会失败,这并不重要,如果你回来了,不是。

  1. Use onmousedown instead of click - this way you get a few extra milliseconds to complete the tracking request, otherwise the browser might not start the connection to your tracker at all as it is already navigating away from the original page. The downside is that you might get some false-positive counts, since the clicking user might not finish the click (eg. keeps the mousebutton down and moves the cursor away from the link) but overall it's a sacrifice you should be willing to make - considering the better quality of tracking.
  2. Instead of an Ajax call ($.post('...')) use an image pre-fetcher (new Image().src='...'). The fact that the tracker is not an image is not relevant in this case because you don't want to use the resulting "image" anyway, you just want to make a request to the server. Ajax call is a two way connection so it takes a bit more time and might fail if the browser is already navigating away but the image pre-fetcher just sends the request to the server and it doesn't really matter if you get something back or not.

所以解决方案是这样的:

So the solution would be something like this:

<script>
$(document).ready(function() {
    $("a").mousedown(function (){
        new Image().src= "http://www.example.com/trackol.php?result=click";
    });
});
</script>

<a href="http://www.google.com">out</a>

这篇关于使用Javascript和PHP跟踪传出链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆