I am a Python beginner working on a very basic real-time analytics tool using Python and MongoDB. I have a function that updates a collection of URLs and pageviews stored in MongoDB:
def track(url):
hour = datetime.utcnow().replace(minute = 0, second = 0, microsecond = 0)
db.hourly.update({"hour": hour, "url":url}, {"$inc": {"views": 1}}, upsert = True)
db.hourly_totals.update({"hour": hour}, {"$inc": {"views": 1}}, upsert = True)
Whenever track gets executed for a given URL, the collection of documents gets updated accordingly with pageviews for each URL incremented for collection "hourly", and total pageviews for all urls incremented for collection "hourly_totals".
What is the best way to automate this process such that track gets executed every time somebody visits a page on my website? Could I do this in Python or do I have to embed this in a Javascript tag?