如何跟踪用户每天访问网站X天? [英] How to track that a user visited the site each day for X days?

查看:108
本文介绍了如何跟踪用户每天访问网站X天?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Stack Overflow上有一个新的徽章。 woot 徽章授予每天访问该网站30天的用户。如何实现这样的功能?你如何跟踪用户以最简单的方式每天访问网站X天?

There is a new badge on Stack Overflow. The "woot" badge is awarded to users visited the site each day for 30 days. How can you implement a feature like this? How can you track that a user visited the site each day for X days in the simplest way?

我想到有两个字段 - 一个用于最后一个时间戳登录,另一个来计算用户持续访问网站的日子。逻辑是首先将计数器设置为1,并存储登录的时间。在下次登录时,请检查自上次登录后是否超过一天,然后递增计数器,或将其设置为1.然后将时间戳记字段更新为当前日期。

I thought of having two fields--one for the timestamp of the last login, and another to count the days on user continuously visited the site. The logic is to first set the counter to 1, and store the time of login as well. On the very next login, check if since the last login no more than one day past, and increment the counter, or set it back to 1. And then update the timestamp field to the current date.

你能做得更简单吗?

推荐答案

你确实需要一个cookie,因为人们可能不会每天登录,例如因为他们自动登录2周,或因为他们在您的网站上不间断地进行休眠,而不用睡眠和午餐50小时:)您可能实际上想要计数用户访问站点时。

You do need to have a cookie, since people might not log in every day -- for example because they are logged in automatically for 2 weeks, or because they are on your site doing things non-stop without sleep and lunch for 50 hours :) You probably actually want to count when user accesses the site.

如上所述,现在可以理论上记录每个访问并执行数据库查询,但是您可能会(像我一样)它在有用性和隐私+简单之间打错了平衡。

Now, one could theoretically record every access and perform database queries, as was suggested above, but you might think (as I do) that it strikes the wrong balance between usefulness and privacy+simplicity.

您指定的算法缺乏明显的方法:由于您只存储整天数,您会错过每12小时登录的用户(您的算法会将天数保持为1)

The algorithm you specified is deficient in an obvious way: since you store only whole number of days, you miss the user who logs in and out every 12 hours (your algorithm would keep the count of days as 1)

这是我发现最简单的解决方案,每个用户两个日期字段,以一种不言而喻的非对象定向Python:

Here's the solution that I find to be cleanest with two date fields per user, in a kind of self-explanatory non-object oriented Python:

# user.beginStreak----user.lastStreak is the last interval when 
# user accessed the site continuously without breaks for more than 25h 

def onRegister (user):
    ...
    user.beginStreak = user.endStreak = time() # current time in seconds 
    ...

def onAccess (user): 
    ...
    if user.endStreak + 25*60*60 < time():
        user.beginStreak = time()
    user.endStreak = time()
    user.wootBadge = ( user.endStreak-user.beginStreak > 30*24*60*60 )
    ...

(请原谅我的Pythonic技能,我是学术界和第一次网站用户)

(Please forgive my Pythonic skills, I'm an academic and first-time site user)

您不能使用一个变量执行此任务。我相信有人可以写一个干净的论据来证明这个事实。

You cannot do this task with one variable. I'm sure somebody can write a clean argument proving this fact.

这篇关于如何跟踪用户每天访问网站X天?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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