简体   繁体   English

使用CloudFlare背后的NodeJS跟踪页面查看时间

[英]Track page view time using NodeJS behind CloudFlare

I want to know how much time user is watching a specific page (Like Google Analytics). 我想知道用户观看特定页面的时间(例如Google Analytics(分析))。

I thought to open a xmlHttpRequest to my server and set timeout to about 30 minutes. 我以为对我的服务器打开一个xmlHttpRequest并将超时设置为大约30分钟。 On the server, listen to request end, and log it on the server. 在服务器上,侦听请求结束,然后将其记录在服务器上。

Client Side 客户端

$(function($.get('/checktime')))  // start tracking on page load

Server Side 服务器端

app.use('/checktime',function(req){
    var start=new Date()
    req.on('end',log)
    req.on('disconnect',log)
    function log(){
        logToServer(new Date()-start) // Time the user spent on the page
    }

}) })

The Questions: 问题:

  1. There is any other better way to track the time the user spent on the page? 还有其他更好的方法来跟踪用户在页面上花费的时间吗?
  2. There is any reason that the NodeJS server will end the connection, before the user leave the page? 有什么原因让NodeJS服务器在用户离开页面之前终止连接?
  3. The app is behind cloudflare . 该应用程序背后存在cloudflare There is any timeout for cloudflare, so the connection will end before the user leave the page? cloudflare超时,因此连接将在用户离开页面之前结束吗?
  4. It is make any different to make the XMLHttpRequest using get or post ? 使用getpost来制作XMLHttpRequest有什么不同?

Thank you 谢谢

The implementations I have seen lately open a WebSocket (fallback to long polling) and keep sending data ever few seconds to the server on the other end. 我最近看到的实现打开了一个WebSocket(回退到长时间轮询),并且每隔几秒钟就将数据发送到另一端的服务器。 This also gives you the ability to gather more data using javascript and send that up to the logging server as well 这也使您能够使用javascript收集更多数据并将其发送到日志记录服务器

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

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