2

I have a site where I want to record how many unique views an entry gets. At the moment I'm storing each view in a table (see below) but I can't help feeling this is too heavy handed.

+--------+---------------+
| postid |  remote_addr  |
+--------+---------------+
|   1    |  192.168.0.0  |
|   2    |  127.0.0.1    |
+--------+---------------+

Obviously this requires at least one query per page, in addition to the rest of the page content, so is there a simpler/lighter way of doing this?

Ross
  • 46,186
  • 39
  • 120
  • 173
  • Why is 1 query per page a problem? It's not that important unless you already have performance issues. – Ward Werbrouck Mar 09 '09 at 18:19
  • @code: sounds trivial, but each query is important, maybe not so much on smaller sites, but it's good practice to follow. – Stiropor Mar 09 '09 at 18:23
  • @code-is-art: True, there aren't many issues. It's just something that looks like overkill from my point of view. Might just be me =P – Ross Mar 09 '09 at 18:28

4 Answers4

3

I strongly suggest you use Google Analytics or any other similar tool, that gives you a very good report and detailed statistics about your visitors.. unique visits, time on site, returning visitors, etc.

It's also easy to install, you just add some javascript code you get on that site.

Stiropor
  • 453
  • 3
  • 7
  • I do use that but this is for publically displaying the view count, such as on Stack Overflow. – Ross Mar 09 '09 at 18:23
  • Than afaik this is the easiest way, just use sessions and cookies to make sure you don't count someone twice. One query will be wasted, but unless you have more than 500.000 pageviews daily, it shouldn't be so horrible :) – Stiropor Mar 09 '09 at 18:28
2

There is some good information here:

Counting number of views for a page ignoring search engines?

Community
  • 1
  • 1
Jon Tackabury
  • 47,710
  • 52
  • 130
  • 168
2

Is this something you can get from your web logs? If it is, you can just get a log parser and filter your pages, and unique visitors.

AaronS
  • 7,649
  • 5
  • 30
  • 56
  • This does sound like an interesting solution an automated script could take care of. – Ross Mar 09 '09 at 18:24
2

This is something that I've tried to do in the past, and gave up in favor of just letting my log analyzer do what it's good at. When I did do it, I used the technique you're describing. It didn't incur much overhead, but I did have to write all of my own logic for reporting and filtering bots...not fun.

I'll also point out that just storing an IP address isn't going to guarantee that you're recording uniques. It's not uncommon for your visitors to be coming from behind a proxy (especially AOL I've noticed in years past).

Boden
  • 4,149
  • 9
  • 43
  • 56
  • I'm debating limiting views to actual userids but this only counts logged in users. I think IPs are probably good enough for now. – Ross Mar 09 '09 at 18:25