PDA

View Full Version : Counting pageviews



Mike
08-03-2006, 06:27 AM
Hi all,

I want to count the number of pageviews on particular pages. Previously, everytime a user visits a page I've just added a number to a column in the table. However, the site I want to do it on has got a lot of traffic, so I'm wondering what will be the best method?

I was thinking log everything in a text file then update the database every so often through that. But has anyone else got a more efficient way?

Cheers,
Mike

Chris
08-03-2006, 09:43 AM
Do you not have logging enabled already?

Using a script like AXS you can easily pull up hit counts for individual pages, and more scripts that rely on your default server logs can do that too, and if not a php script that parsed your server logs would be an easy thing to code.

Mike
08-03-2006, 09:58 AM
Erm... say I had an article, I would want some text on it saying how many people had viewed that article. Would I be able to do that with AXS?

Chris
08-03-2006, 10:12 AM
Technically yes, but not efficiently.

The database method wouldn't be a bad option.

The other way I think to do it would be to load it in a text file. A text file with nothing but the number in it, you load it then rewrite it on every page view.

Mike
08-03-2006, 10:21 AM
So say if you had 100 articles, would you need 100 text files? Or would you make it so the text file was set like:

ArticleID = 1245

With the amount of pageviews after the equals sign.

Chris
08-03-2006, 11:30 AM
100 text files.

For utmost efficiency you want to avoid any need for conditional parsing (where article id = ). Name your text files articleid.txt and that way its just straightforward for the code, read file articleid.txt, write file articleid.txt. No conditionals.

Also, remember if you use a larger file the entire file has to be loaded into memory to be read, not just the line you want, that will be a resource hog.

Mike
08-03-2006, 01:06 PM
Sounds easy...

What if there are thousands and thousands of text files? Will it not matter as they are small in size (for my storage) and are not being accessed all the time?

Chris
08-03-2006, 01:09 PM
Yup, because they're small and not all accessed at once, and they're all explicitly named and explicitly referenced, number is irrelevant.

For management sake though if there really are multiple thousands I would maybe categorize them. For each article category on the site have a separate directory with the files in it.

Mike
08-03-2006, 01:11 PM
What you said sounds perfect then, I'll give it a go. Thanks for the guidance :)

Mike
09-17-2006, 02:20 AM
I'm about to start work on this... is there a way you could order by pageviews? If all the stats are in a text file, I'm not sure if you could do it.

AndyH
09-17-2006, 02:46 AM
I would go with a database. With text files you cannot order or get totals.

Mike
09-17-2006, 02:55 AM
Would you just add to the figure in the database everytime someone views a page? Would it use a lot of resources?

AndyH
09-17-2006, 05:00 AM
"page_views" table. A row for each article. Increment the figure for each page load.

It shouldn't use up much resources. Try it out and see.

Chris
09-17-2006, 06:13 AM
you could do a total count, you'd merely have to open up all the files and get the count.

Mike
09-17-2006, 10:22 AM
"page_views" table. A row for each article. Increment the figure for each page load.

It shouldn't use up much resources. Try it out and see.

Or could you just add a column to the articles table, counting the pageviews?

KelliShaver
09-17-2006, 10:48 AM
Or could you just add a column to the articles table, counting the pageviews?

That would be the easiest way to keep it organized, I would think.

Emancipator
09-17-2006, 01:39 PM
kelli is right.

AndyH
09-17-2006, 06:07 PM
Or could you just add a column to the articles table, counting the pageviews?
Definatly.

Mine was a combination of not fully thinking and taking into consideration "a lot of traffic" and the lock created every update. Unless you are doing 600+ queries/sec I don't think it should matter as we are talking milliseconds.

Mike
09-19-2006, 10:00 AM
Great, I'll do that now. Cheers guys.