From https://raw.github.com/srounet/Flask-Analytics/master/flask_analytics.py