Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
"""
Python that records information to compute daily active users for your site, assuming your site is running with python. :D (obviously)
Assumes you're concerned about the eastern timezone.
"""
from redis import StrictRedis
from datetime import datetime
from pytz import timezone
def log_user(user_id):
"""
Log the user activity for the purpose of a daily active user system.
"""
today = datetime.now(timezone('US/Eastern'))
# daily:user_id:YYYY-MM-DD
key = 'daily:%s:%s' % (user_id, str(today.date()))
r = StrictRedis(host=REDIS_HOST, port=6379)
r.incr(key)
r.expire(key, 60 * 60 * 24 * 2) # expires after two days after last update
return
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.