Question

I'm going to create a view counter for articles. I have some questions:

  1. Should I ignore article's author when he opens the article?

  2. I don't want to update database each time. I can store in a Dictionary<int, int> (articleId, viewCount) how many times each article was viewed. After 100 hits I can update the database.

  3. I should only count the hit once per hour for each user and article. (if the user opens one article many times during one hour the view count should be incremented only once).

For each question I want to know your suggestions how to do it right.

I'm especially interested how to do #3. Should I store the time when the user opened the article in a cookie? Does it mean that I should create a new cookie for each page?

Was it helpful?

Solution

I think I know the answer - they are analyzing the IIS log as Ope suggested.

Hidden image src is set to

http://stackoverflow.com/posts/3590653/ivc/[Random code]

[Random code] is needed because many people may share the same IP (in a network, for example) and the code is used to distinguish users.

OTHER TIPS

  1. Sure - I think that is a good idea

  2. and 3. are related: The issue is where would you actually store this dictionary and logic.

An ASP.NET application or session scope are of course the easiest choice, but there you really need to understand the logic of application pools. ASP.NET applications are recycled from time to time: when there is no action on the site for a certain period or in special situations - e.g. if the process starts to take too much memory the application is shut down and a new one is started in the next request. There are events for session and application shut-down, but at least some years ago they were not really reliable: In many special cases they did not always fire. Perhaps they are better now, but it is painful to test. And 1 hour is really a long time: Usually sessions are kept alive only like 20 minutes after last request.

A reliable way would be to have a separate Windows service (a lot of work to program) or always storing to database with double-view analyses (quite a lot of overhead for such a small feature).

Do you have access to IIS logs? How about analyzing IIS logs e.g. every 30 minutes with some kind of timer process and taking the count from there? Or then just store all the hits to the database with user information and calculate the unique hits with a similar timed process.

One final question: Are you really sure none of the thousands of counter applications/services in the Internet wouldn't do the job close enough to your requirements?

Good luck!

This is the screenshot of this page in Firebug. You can see that there is a request which returns 204 status code (No Content).

This is stackoverflow's view counter. They are using a hidden image which point to a controller's action.

I have many articles. How to track which articles the user visited already?

P.S. BTW, why is this request made two times?

alt text

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top