Reputation: 4436
I would like to count the number of unique visitors and store that number in my database. I know that I can get the ip address with
request.remote_addr
so my default plan would be to store this address with a timestamp in my database. For any new visitor I would than compare whether this ip address has already been added with a timestamp less than 1 day old. This would give me the number of visitors per day.
However, I have the feeling this is a problem where others probably came up with better solutions already? Any suggestion how to do this better?
p.s. I just need a rough estimate for the number of unique visits, so the fact that ip addresses are not a good measure is not that concerning to me... but if you have better suggestions I am definitely interested
Upvotes: 9
Views: 5921
Reputation: 2808
To get a faster more reliable solution, simply offload this functionality to another service like Google Analytics. They provide a very detailed/in-depth view of the traffic on your website without you having to worry about the intricate details.
Regardless, if you're dead set on using flask, take a look at this wonderful blog post explaining how to build an analytics app in Flask. Here is the complete source code of that app.
Upvotes: 9