是否可以使用JavaScript跟踪过去24小时内的页面浏览量? [英] Is it possible to use JavaScript to track how many page views in past 24 hours?

查看:91
本文介绍了是否可以使用JavaScript跟踪过去24小时内的页面浏览量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要跟踪过去24小时内有多少用户访问过某个页面,如果此时用户数超过50,那么我想显示该时间段内动态求和的累计用户数。

I need to track how many users have visited some page in the past 24 hours, and if the number of users goes over 50 in this time, then I want to display the dynamically summed cumulative user count for that timeframe.

我以前在后端使用了JS POST和PHP解析的基本组合,如下所示:

I have previously used a basic combination of JS POST and PHP parsing on the back end like this:

    $.post( "//api.link.here/using.php", { someParameter: 'xxx' }, function(data) { 
        if(data > 50) {
            $('#much-visits').html('Hot Like Fire: '+data);
        }
    });

但我现在正在开发一个不允许我以任何方式编辑PHP的平台,此方法尚未在此平台上开发。

But am now working on a platform that doesn't allow me to edit PHP in any way, and this method has not been developed on this platform.

是否有可能实现此目的/类似的东西但只使用javascript(允许框架/外部API调用)?

Is it possible to accomplish this same/similar thing but with javascript only (frameworks/outside API calls allowed)?

推荐答案

我想出了如何使用帮助用户kabiroberai关于count.io的回答。

I figured out how to do this with the help of user kabiroberai's answer regarding count.io.

每次有人访问该页面时,执行此功能(将usercount更改为更独特的内容):

Every time a person visits the page, execute this function (change usercount to something more unique):

$.ajax({
url:"http://count.io/vb/usercount/users+",
type: "POST"
});

当您想要读取其值时,请调用此函数。 Usercount是您之前创建的唯一字符串:

And when you want to read its value, call this function. Usercount is the unique string you made previously:

$.ajax({
url:"http://count.io/vb/usercount/",
success: function(data) {
    alert(data.counts[0].count);
}
});

注意:重要的是要知道如果有人知道字符串,他们可能会破解系统要小心谨慎。

NOTE: It's important to know that if someone gets to know the string, they might be able to hack the system so be wary of that.

这种方法的问题在于它给我们所有时间的总数,而不是过去24小时内的用户数。

The problem with this method is that it gives us the total count of all time, rather than the count of users from the past 24 hours.

要获得24小时计数,我们可以这样做:
//浏览器的时间戳垫片

To get the 24-hour count, we can do this: // Timestamp shim for browsers

    // Timestamp
    var timestamp = Math.floor(Date.now());
    // 24 hour old timestampt
    var dayAgo = timestamp - 86400000;

    // Record a hit to the count.io DB under DankStopProduct# group, as a timestmped hit of 1
    var productNumber = $('.some-product-element').find('some-unique-identifier').attr('value'); //Doesn't have to be value, could be any unique identifier
    $.ajax({
        url:'http://count.io/vb/YourProduct' + productNumber + '/' + timestamp + '+',
        type: 'POST'
    });

    // Make dynamic HTTP prefix that depends on the prefi of the site's current page protocol
    var httpPrefix = (window.location.protocol === 'http:' ? 'http:' : 'https:');

    // Retrieve current product array
    $.post(
        //I use CORS-anywhere to retrieve the data as otherwise there is a cross-origin problem
        httpPrefix + '//cors-anywhere.herokuapp.com/' + 'http://count.io/vb/YourProduct' + productNumber,
        function (data) {
            var timedViewCounter = 0;
            var myStringArray = data.counts;
            var arrayLength = myStringArray.length;
            // Check each "timestamped hit" in the current product's array to see if it's older than 24 hours
            for (var i = 0; i < arrayLength; i++) {
                var itemTimestamp = data.counts[i].item;
                // Count up all the product hits that are 24 hours old or newer
                if (itemTimestamp > dayAgo) {
                    timedViewCounter++;
                }
            }
            // If total count is greater than 50, then output HTML onto page
            var hotItem = '<div id="hot-item"><strong><i class="fa fa-fire"></i> Hot Like Fire!</strong> - ' + 
                            '<span class="view-count">' + timedViewCounter + '</span> people have viewed this product in the last 24 hours</div>';
            if (timedViewCounter > 50) {
                $(".FreeShippingContainer").before(hotItem);
            }
    });

这就是全部!现在只有在过去24小时内有51次或更高的视图时才会输出产品视图计数。

And that's all! Now the product view count will only be output if it's 51 views or higher in the past 24 hours.

这篇关于是否可以使用JavaScript跟踪过去24小时内的页面浏览量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆