Page View Counter Advise

Hi,

I would like to implement a page view counter on some of the pages of my site, and would like to know if anyone has a good method suggestion for it.

I have seen some sites like monster.com that has a page view counter for the number of times a visitor views the page your resume is on, and would love to know how such a thing is achieved. Another perfect example will be the number of views count on this forum board…

I have thought about creating a table to store the IP address of whoever visits such pages so that I can count the unique visitors but I think that this might be an over-kill? Or is this the regular and only way it can be done?

I want to be able create a page view counter for the users who register on my site so that they can see how many time visitors have viewed their pages.
If the above method is true, than that means that I would have to create a table which stores some kind of user id, and also a column that stores ip addresses. This table will store only unique visiting ip addresses for each user id and than a query would do a record count of all ip addresses for a specific user id to give a page view count for that user.

However, would that mean that that single table would grow very huge and may bog down my app? Just wondering…

I do appreciate some suggestions! Thank you! :slight_smile:

One of two ways, YOURS, where the table records unique visitors based on ip. Yes, the table would grow and grow and grow as based on the lifespan and popularity of your site.

Second would be to just add a simple SQL statement that does a currentvalue + 1 to a column based on user_id. Your only going to get total views that way, but i believe you mentioned that you might not care about unique visits.

Thanks downtroden,

Well, I guess I will have to create a table to store the IP addresses as I need to only register the view count to come from unique visitors…I think this means that this table would grow very very very large over time…will this slow down the speed of how fast the page is served as it will have to do a recordcount query to run down all the IPs for a specific user?

Burntfromfusion…

It depends on what you want your personal stats to communicate. If I look at a page today, then come back again 4 days later do you really only want my visit to count once? My opinion is that both hits should count, which means you’re either inserting a record twice, or you’re updating a column in the Pages table each hit. What you really want to avoid is someone hitting a page, and then endlessly reloading it.

You could take a few different approaches.

  1. hitcount column, updated each time the page loads.

  2. hitcount column, updated at specific intervals. For example, you could create an array in the session scope called “page access”, with the user’s IP address as the key. As they browse around, you keep appending unique records into that array. Then, in your onSessionEnd method in Application.cfc you can loop over the array and update the hit column for each page. This way you’re only counting pages they visited during this session.

The drawback of these two methods is that database reads are cheaper than inserts or updates, especially if you happen to be using MySQL.

  1. Create a stats table, and take the same approach as #1. You’d foreign key your stats update to the page id.

  2. Create a stats table, and take the same approach as #2.

  3. Create a stats table and insert records for each visit. Unless your site is seriously high traffic you’d be okay for years…even if you get up to hundreds of thousands of records that’s nothing for a database. My blog (http://andymatthews.net) is moderately well trafficked, and I’ve had this method in place for over a year and I’m still only on 599,978 records.

Hope this helps. Feel free to ask for clarification if need be.

Also, on a side note, you’d never want to run a stats query that returned all records, JUST to get a count. Databases are far more efficient than that.

Something like this would be perfectly acceptable.

SELECT COUNT (pageID) as totalHits
FROM statsTable
WHERE pageID = 1234

=> returns 27