views:

89

answers:

2

What is the best method of tracking page views (uniques specially) for a certain page?

Example: Threads in a forum, Videos in a video website, questions in a Q/A script(SO).

Currently, I'm taking the approach of just a simple "Views" column for each row I'm trying to count the views for, however, I know this is not the most efficient way.

And for unique views, I have a separate table that holds a row with the "QuestionID" and "UserID". When a user visits a question/page, my code attempts find a row in the views table with the "UserID" and "QuestionID", if it can't, it adds a row, then increments the Views value of the Question in the "Questions" table.

+1  A: 

your solution for storage seems to be the best way to track for users. The tables don't have redundant data and your many to many relationship is represented by its own table.

On another note: For anonymous users you would need to record their IP address, and the you can use the COUNT() sql function to get the number of unique visitors that way, but even and IP address not "unique" per se.

Scott M.
A: 

First of all when you have a table that stores userid-quesitonid pairs, it means you can count them, adding a views column is against the rules of normalisation I suppose.

Other thing is that I can F5 as much as I want, if you do not implement a cookie solution, if not then add a row to the table.

when it comes to ip address solution, which is far form being a solution at all, it will blcok people behind routers.

I think of (also implementing right now) a solution that checks cookies, sessionIds, DB table for registered users, and if none found, adds a row to the table. Itr also records SessionID and IP addresses anyway, but don't really rely on them.

Emre