How to track that a user visited the site each day for X days?

后端 未结 5 948
情深已故
情深已故 2021-02-04 16:41

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 trac

5条回答
  •  滥情空心
    2021-02-04 17:28

    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.

    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)

    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 )
        ...
    

    (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.

提交回复
热议问题