Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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.

If you are using ASP.NET MEmbership and the Anonymous Provider for anonymous users, then each anonymous user gets a row created in aspnet_Users table as soon as you say Profile.Save(). In that case, you can track both anon and registered users viewing certain Page. All you need to do is record the aspnet_user's UserID and the QuestionID.

However, I strongly discourage doing this at database level since it can blow up your database. If you have 10,000 questions and 1,000 registered users and 100,000 anonymous users, and assuming each user visits 10 questions on an average, then you end up having 1M rows in the tracking table. Quite some load.

Moreover, doing a SELECT COUNT() on the tracking table is quite some load on the database, especially you are doing this almost every page view on your forum. Best is to keep a total counter at the Question table against each question. Whenever you get a unique user looking at a page, you just increase the counter.

Also don't create a FK relationship to the user table from the tracking table. You will need to cleanup the aspnet_users table as it piles up a lot of anonymous users over time who will most likely never come back. So, the tracking page needs to just have the userID field, and no FK. Moreover, you will have to cleanup the tracking table over time as well as it will keep getting millions of rows. That's why the TotalView counter needs to be at the Question table and never use SELECT COUNT() to calculate the number of views while displaying the page.

Does this answer your question?

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