且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何跟踪用户每天访问网站X天?

更新时间:2023-12-01 15:53:16

你确实需要一个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.