views:

150

answers:

3

I want to count the visits on a web page, and this page represents an element of my model, just like the Stack Overflow question page views.

How to do this in a reliable (one visit, one pageview, without repetitions) and robust (thinking on performance, not just a new table attribute 'visits_count')

+6  A: 

You can't.

  • Multiple people can visit your site from the same IP (makes ip storage useless)
  • Multiple people can visit your site from the same PC
  • Multiple people can visit your site from the same browser (makes cookies useless)

The closest you can get is to store the visitors IP in combination with a cookie to not count those in the future. Here is a tradeoff, if they clear the cookie, they are a new visitor. If you only store the IP, you count whole proxies as one visitor.

Another option is to use user accounts and track exactly which user viewed what page, but this is not really a good option for public sites.

Tomh
A: 

One way to handle the performace issue, is to do the calculations on insert. Something like

UPDATE stat SET viewcount = viewcount+1 WHERE date = CURDATE()

It isn't the perfect solution but could get you some of the way.

To do it without repetitions is potentially a tricky problem. I would simply rely on using sessions or cookies, but you could come up with all kinds of strategies for filtering crawlers, clients with out cookie-support and so on.

Emil Rasmussen
A: 

I would suggest, actually, using Google Analytics. While nothing will ever be truly accurate, they do take a pretty gosh-darn good stab at it, and the level of reporting and useful information you can extract is amazing.

Not to mention being free for up to 4,000,000 page views a month.

http://www.google.com/analytics

Fritz H