Skip to content

Instantly share code, notes, and snippets.

@pstrinkle
Created June 24, 2016 13:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pstrinkle/541ba8ca06c96b8511e2f6ab188f190e to your computer and use it in GitHub Desktop.
Save pstrinkle/541ba8ca06c96b8511e2f6ab188f190e to your computer and use it in GitHub Desktop.
"""
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