views:

84

answers:

3

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?

+2  A: 
  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.

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.

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 info and calculate the unique hits with similar timed process.

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

Good luck!

//Olli

Ope
I forgot to mention - Dictionary will be stored in a static property.No, I don't have access to IIS logs. I'd like a view counter like on StackOverflow.
denis_n
A: 

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

denis_n
You sure? I think SO updates the view counter when you enter the question's details, like this page.
Guilherme Oenning
Yes, this kind of image is present only on pages which require number of views.
denis_n
A: 

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.

denis_n
Probably I'll use the same approach.
denis_n