为什么从 Firefox 浏览器访问时,现场时间数据没有保存在数据库中?

Why is time-on-site data not getting saved in DB when accessed from Firefox browser?

我们按照文档

中的说明在我们的网站中包含了 timeonsite 库 timeonsitetracker
<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() {
        // save with XMLHttpRequest or sendBeacon
    var config = {
        trackBy: 'seconds',
        developerMode: true,
        callback: function(data) {
            console.log('***');  
            console.log(data);

            // give your endpoint URL/ server-side URL that is going to handle your TOS data which is of POST method. Eg. PHP, nodejs or python URL which saves this data to your DB

            var endPointUrl = 'http://localhost:4500/tos'; // replace with your endpoint URL

            if (data && data.trackingType) {
                if (data.trackingType == 'tos') {
                    if (Tos.verifyData(data) != 'valid') {
                        console.log('Data abolished!');
                        return; 
                    }
                }
                
                if (navigator && typeof navigator.sendBeacon === 'function') {
                    data.trasferredWith = 'sendBeacon';
                    var blob = new Blob([JSON.stringify(data)], {type : 'application/json'});
                    navigator.sendBeacon(endPointUrl, blob);
                }
                
            }    
        }};


        if(TimeOnSiteTracker) {
            Tos = new TimeOnSiteTracker(config);
        }
    };
    js.src = file;fjs.parentNode.insertBefore(js, fjs);
 } (document, 'script', 'TimeOnSiteTracker', '//cdn.jsdelivr.net/gh/saleemkce/timeonsite@1.1.0/timeonsitetracker.min.js'));
</script>

刷新浏览器后,我在 Firefox Web 控制台中正确地看到记录的数据(启用日志持久性,否则每次刷新都会清除日志)。

***
{
    TOSId: 14650383319214848
    TOSSessionKey: "8808159448467693499978"
    TOSUserId: "anonymous"
    URL: "https://localhost/index.html"
    currentTime: "2021-03-30 16:25:17.908"
    entryTime: "2021-03-30 16:24:36.911"
    timeOnPage: 41
    timeOnPageByDuration: "0d 00h 00m 41s"
    timeOnPageTrackedBy: "second"
    timeOnSite: 41
    timeOnSiteByDuration: "0d 00h 00m 41s"
    title: "home page - rental crown"
    trackingType: "tos"
}

但是这个session数据没有存储在MariaDB中。不知道数据去了哪里。在刷新第二页时,我再次在 Firefox Web 控制台中看到更新的数据对象,但没有在 MariaDB 中捕获任何数据。它在 Chrome 中工作,但似乎没有在数据库中正确存储数据。知道如何解决 Firefox 中的问题吗?

我关注了documentation here。非常感谢您的帮助。

我今天再次测试了这个。 sendBeacon 似乎有一段时间无法在许多浏览器(如 Chrome、Firefox 和其他浏览器)上工作。为此,Chrome & Firefox 中都存在一些未解决的问题。

但是今天,我在 Chrome 和 Firefox 上用 timeOnSite tracker JS 再次测试了它。似乎它在页面刷新、选项卡关闭或浏览器关闭场景中成功地将数据发布到数据库。

更新日期:2021 年 11 月 13 日

浏览器 运行成功 版本:

Chrome: 95.0.4638.69

Firefox: 94.0.1

但请注意,这可能不适用于旧浏览器或不完全支持 sendBeacon 的浏览器。因此,如果无法在此类浏览器上获取实时站点数据,请使用请求对象回退到 localstorage 选项。

var config = {
    trackBy: 'seconds',
    
    request: { // presence of request object denotes that data is to be saved in local storage
        url: 'http://localhost:4500/tos'
    }
}