简体   繁体   English

使用Ajax跟踪出站链接(javascript)

[英]Tracking outbound links using ajax (javascript)

I have a question regarding this answer here https://stackoverflow.com/a/2078233/560972 我对这里的答案有疑问https://stackoverflow.com/a/2078233/560972

As i understand, the most common problem using JS (Ajax) to track outgoing link clicks is that sometimes user leaves page before (faster) the script can grab the data...? 据我了解,使用JS(Ajax)跟踪传出链接点击的最常见问题是有时用户在(更快)脚本可以抓取数据之前离开页面...?

So maybe it is possible to force some sort of delay in order to let script finish recording and then let user navigate away to other site? 因此,也许可以强制某种程度的延迟以便让脚本完成录制,然后让用户导航到其他站点? Delay when link is clicked and navigation away occurs 单击链接并发生导航时的延迟

Will this help? 请问有帮助吗? I suppose ~200ms/300ms won't be visible for user but it could be enough for ajax call? 我想〜200ms / 300ms对用户不可见,但足以应付ajax调用?

What you do think? 你觉得呢?

Thanks! 谢谢!

With jquery: 使用jQuery:

$('a').click(function(e) {
   //check that it is offsite
   if($(this).attr("href").indexOf("http")==1) {
      //prevent the redirect;
      e.preventDefault();
      //do your tracking 
      $.ajax{
          url: 'yourtracking.php',
          data: "link=" + $(this).attr("href"),
          complete: function(){
              //now do the redirect
              window.location = $(this).attr("href");
          }

     }
  }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM