How to track user time on site

前端 未结 5 1743
渐次进展
渐次进展 2021-01-30 14:49

I\'m looking to track users average time on a website (in the same way that Google analytics does) for internal administration.

What\'s the easiest way to do this?

5条回答
  •  佛祖请我去吃肉
    2021-01-30 15:39

    $(window).unload is a very good function to tracking user leaving page.

    but one point is when window unload to send ajax sometime it now work, the ajax not finished when user leave page. so you need add one attribute (async: false) to ajax, code is blow:

    $.ajax({
                      type: 'POST',
                      async: false,
                      url: '/collect-user-time/ajax-backend.php',
                      data: {time: time}
                  });
    

提交回复
热议问题