views:

242

answers:

4

On any website, such as on StackOverflow, each question has a view count, and user reading a question but has previous read it won't count twice.

I have some ideas about how it is implemented and using what tables to do it.

What do you think is the best way to implement this?

+1  A: 

When most of the visitors to your site are registered it's relatively easy to make sure none of them are counted twice.

I'm not sure if SO counts views by guests. I suppose I could check but it's late.

Spencer Ruport
i just viewed a question multiple times and the count didn't change after the first time.
動靜能量
I think it also doesn't count views by the OP - so if I ask a question and then look at the number of views, I think that's the distinct number of users who've looked at the question.
Dominic Rodger
+5  A: 

You have a couple of options as I see it.

Cookies

You can store a cookie in the users browser for each page you are logging views on. Check for this cookies existence and don't log a view if the cookie already exists.

Downside to this is that it won't work if cookies are disabled or someone is trying to game the system.

On the plus side you don't have to worry about the storage of potentially millions/billions of rows of table data.

Database

You hold a record for each view. Relating that record to a user in some way e.g. MemberID, IP Address; something that should be unique to the user. IP is not ideal but good enough if you are not requiring users to login.

So you would have for example a table with the following columns,

  • ArticleID (Foreign Key)
  • UserID (Foreign Key)
  • Date

The date will be useful for a couple of reasons,

  • Reporting. You can build much better statistics once you know when each view was recorded.
  • View Timeouts. For example, you may only want to store one view per user per hour. With the date column held you can do this.

If your application gets popular in this situation then you will need to deal with the storage implications. I run a popular Facebook app which results in over 100,000 view rows being added each day. Realistically though if you're app gets popular enough that this becomes a problem then you'll have much bigger issues to deal with.

Craig Bovis
+1  A: 

Short answer: It depends!

  • It really depends on how accurate you need your view count to be, is it acceptable that one person might be registered two or three times?
  • It depends on what you're gonna use the data for. If you want do other neat things with the data (statistics, recent view list and so on) you might want to consider storing all the individual views in a database. This might result in a huge table so you have to thing this through before implementing it.

I've previously used cookies combined with an in-memory database to store the individuals view's (for obvious reasons I stored the actual view count in a database table persisted to disk). I could do this because the statistics didn't mean anything.

Kimble
A: 

I'll try to give an answer from the functional point of view.

count views per user - for registered users. for anonymous users - per session.

increment view count on the first view and on any view after a significant update by someone other that the person viewing the item.

view of poster at the time of creation should not count

you can imagine doing it simpler too, but I've tried to think of an ideal solution.

Evgeny