如何跟踪用户在网站上的时间

发布于 2024-09-15 07:48:31 字数 67 浏览 6 评论 0原文

我希望跟踪用户在网站上的平均时间(与谷歌分析的方式相同)以进行内部管理。

做到这一点最简单的方法是什么?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

心如荒岛 2024-09-22 07:48:31

您可以通过以下方式获取时间:

  1. 一旦用户访问您的网站,将当前时间保存在 cookie 中作为“访问”,并且在下次访问时您可以获取它(如果已设置)。
  2. 更昂贵的方法:当页面加载时,启动js计时器,并在页面卸载时将用户发送的时间发送到服务器并将其保存到数据库。
  3. 如果 window.unload 在 Opera 上不起作用,您可以每 5 秒向服务器发送一次时间,并将其存储到数据库中。

如果您需要,我可以写一个示例脚本。

更新:

<!DOCTYPE html>
<html>
    <head>
        <title>Collect time</title>
        <script type="text/javascript" src="jquery-1.4.2.min.js"></script>
        <script type="text/javascript">
        $(function()
        {
            var start = null;
            $(window).load(function(event) {
                start = event.timeStamp;
            });
            $(window).unload(function(event) {
                var time = event.timeStamp - start;
                $.post('/collect-user-time/ajax-backend.php', {time: time});
            })
        });
        </script>
    </head>
    <body>

    </body>
</html>

和后端脚本:

<?php 
$time = intval($_POST['time']);
if (!file_exists('data.txt')) {
    file_put_contents('data.txt', $time . "\n");
} else {
    file_put_contents('data.txt', $time . "\n", FILE_APPEND);
}

但正如我所说,它在 Opera 浏览器上不起作用

You can get the time in next ways:

  1. Once user visit your site, save current time at cookie as "visited", and at next visit you can grab it, if it was set.
  2. And more expensive method: when the page loads, start js timer, and on page unload send to server time which user sent and save it to db.
  3. And if window.unload does not work at Opera, you can send time to server every 5 seconds, and stores it to DB.

If you need, I can write an example script.

UPDATE:

<!DOCTYPE html>
<html>
    <head>
        <title>Collect time</title>
        <script type="text/javascript" src="jquery-1.4.2.min.js"></script>
        <script type="text/javascript">
        $(function()
        {
            var start = null;
            $(window).load(function(event) {
                start = event.timeStamp;
            });
            $(window).unload(function(event) {
                var time = event.timeStamp - start;
                $.post('/collect-user-time/ajax-backend.php', {time: time});
            })
        });
        </script>
    </head>
    <body>

    </body>
</html>

And backend script:

<?php 
$time = intval($_POST['time']);
if (!file_exists('data.txt')) {
    file_put_contents('data.txt', $time . "\n");
} else {
    file_put_contents('data.txt', $time . "\n", FILE_APPEND);
}

But as I said it wouldn`t work at Opera browser

方觉久 2024-09-22 07:48:31

我能想到的主要方式是:

当用户第一次点击页面时,您会记录他们的 IP 地址、加载的页面和时间。然后,使用一些 Javascript 和 AJAX,当它们离开页面时,您可以使用 unload 事件发送到 AJAX 处理程序,该处理程序记录页面以及它们离开的时间。

除了会话之外,您还需要使用某种 ID 来存储页面访问。假设我打开了 5 个主页实例,您需要单独记录每个实例。所以,像这样:

  1. 访问页面,生成一个代码(假设页面:index.php代码:2345
  2. 将其及其IP存储在数据库表中,然后访问时
  3. Unload事件触发,调用AJAX,传递页面& code
  4. 在数据库中查找 IP、页面和代码,并记录离开时间

如果他们再次访问 index.php,您将生成另一个代码,例如 36789。最好使用生成随机 GUID 的工具,这样您就可以(本质上)忽略同一 IP/页面/代码组合上发生冲突的任何可能性。

Main way I can think of:

When the user first hits a page, you log, say, their IP address, the page loaded, and the time. Then, using some Javascript and AJAX, when they leave the page, you use the unload event to send to an AJAX handler that records the page and when they leave.

You would need to use some sort of ID, apart from a session, to store the page visit. Say I have 5 instances of the homepage open, you'd want to log each one individually. So, something like this:

  1. Access the page, generate a code (let's say page: index.php code: 2345)
  2. Store this in a database table with their IP, and visit time
  3. Unload event fire, call the AJAX, passing the page & code
  4. Look up in the DB for the IP, page, and code, and log the leave time

If they visit index.php again, you would generate another code, say, 36789. Use something that generates a random GUID is best, so you can (essentially) ignore any possibilities of collisions on the same IP/page/code combination.

ヤ经典坏疍 2024-09-22 07:48:31

对网络和移动浏览器使用 timeonsite JS。它准确地跟踪现场时间。

<script type="text/javascript">
        var Tos;
        (function(d, s, id, file) {
            var js, fjs = d.getElementsByTagName(s)[0];
            if (d.getElementById(id)) return;
            js = d.createElement(s);
            js.id = id;
            js.onload = function() {
                var config = {
                    trackBy: 'seconds' 
                };
                if (TimeOnSiteTracker) {
                    Tos = new TimeOnSiteTracker(config);
                }
            };
            js.src = file;fjs.parentNode.insertBefore(js, fjs);
        } (document, 'script', 'TimeOnSiteTracker', 'https://cdnjs.cloudflare.com/ajax/libs/timeonsite/1.2.0/timeonsitetracker.min.js'));
    </script> 

在页面末尾,调用

<script>
    Tos.getTimeOnPage();
        //Response ->
        {
            TOSId: 1129620185532,
            TOSSessionKey: "14802525481391382263",
            TOSUserId: "anonymous",
            title: "Test application - TimeOnSiteTracker",
            URL: "http://tos-localdata.chennai/home.php"
            entryTime: "2016-11-27 13:15:48.663",
            currentTime: "2016-11-27 13:17:31.663",
            timeOnPage: 103,
            timeOnSite: 0,
            timeOnPageTrackedBy: "second",
            timeOnPageByDuration: "0d 00h 01m 43s",
            timeOnSiteByDuration: "0d 00h 00m 00s",
            trackingType: "tos",
        }
</script>

就这么简单,

即使在不支持的iPhone、iPad、移动Safari等移动浏览器中,它似乎也可以工作window.unload() 原生事件。

Use timeonsite JS for web and mobile browsers. It tracks time on site accurately.

<script type="text/javascript">
        var Tos;
        (function(d, s, id, file) {
            var js, fjs = d.getElementsByTagName(s)[0];
            if (d.getElementById(id)) return;
            js = d.createElement(s);
            js.id = id;
            js.onload = function() {
                var config = {
                    trackBy: 'seconds' 
                };
                if (TimeOnSiteTracker) {
                    Tos = new TimeOnSiteTracker(config);
                }
            };
            js.src = file;fjs.parentNode.insertBefore(js, fjs);
        } (document, 'script', 'TimeOnSiteTracker', 'https://cdnjs.cloudflare.com/ajax/libs/timeonsite/1.2.0/timeonsitetracker.min.js'));
    </script> 

At the page end, call

<script>
    Tos.getTimeOnPage();
        //Response ->
        {
            TOSId: 1129620185532,
            TOSSessionKey: "14802525481391382263",
            TOSUserId: "anonymous",
            title: "Test application - TimeOnSiteTracker",
            URL: "http://tos-localdata.chennai/home.php"
            entryTime: "2016-11-27 13:15:48.663",
            currentTime: "2016-11-27 13:17:31.663",
            timeOnPage: 103,
            timeOnSite: 0,
            timeOnPageTrackedBy: "second",
            timeOnPageByDuration: "0d 00h 01m 43s",
            timeOnSiteByDuration: "0d 00h 00m 00s",
            trackingType: "tos",
        }
</script>

As simple as that,

It seems to work even in mobile browsers like IPhone, IPad, mobile Safari etc. that doesn't support window.unload() events natively.

那些过往 2024-09-22 07:48:31

实际上没有一种有效的方法可以使用 PHP 来执行此操作,因为 PHP 是服务器端的,并且无法提供确定页面何时关闭的方法。您需要使用 javascript 来确定这一点。

我要做的是使用 javascript 在 window.onload 上启动计时器,然后在 window.onunload 上结束计时器。然后您可以存储数据并用它做您想做的事情。

There really isn't an effective way to do this with PHP, as PHP is server-side and provides no way of determining when the page was closed. You need to use javascript to determine this.

What I would do is use javascript to start a timer on window.onload and then end the timer on window.onunload. Then you can store the data and do what you want with it.

寄意 2024-09-22 07:48:31

$(window).unload 是跟踪用户离开页面的一个非常好的函数。

但有一点是,当窗口卸载发送 ajax 时,它现在可以工作,当用户离开页面时,ajax 尚未完成。所以你需要为ajax添加一个属性(async: false),代码如下:

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

$(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}
              });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文