I am developing a website, where I need to track number of views for a particular content (lets say articles).
Its very similar to stackoverflow’s views for a question.
I don’t know how stackoverflow does it. Basically it should
- Increment the view when a user other than the article author visits the article. If he keeps hitting refresh button, it should not increment the view.
- Not increment the view if a visitor hits the refresh button continuously.But it should increment for the first visit.
I have a field in the database to hold number of views.
Anybody know how stackoverflow does it? Is there any plugin in rails which I can use? I guess I should use sessions and IP address to keep track… Should I make use of Logs? OR Google Analytics?
help…
Depending on how accurate you need it to be, you’ll probably need to have a table containing records for which user has read which article. Their MAC address isn’t sent with their HTTP request, and their IP(v4) address is probably not unique enough.
If you want to display the data (like stackoverflow does), you can’t really use analytics.
It will be easier to add code to your controller methods to increment a database field than parsing logfiles and hooking into the database outside of Rails.
Pseudo-ish code (check API for syntax):
You can use a plugin for this – I’ve used one before for marking things as ‘read’, which would probably be suitable (or at least adaptable). Have a read of this question, and hit up google.