tags:

views:

59

answers:

2

Firstly I should mention that I am aware of (and use some) statistics packages, however in this instance i want to write my own ulta-lightweight solution.

what i want to achieve:

  1. user visits a page
  2. once all content loading, parsing etc is complete, a request is made to .php file which increments a mysql db

what I'd like your thoughts on:

  • best event to bind to when ALL content is finished loading? (ie doc.ready?)
  • best jquery 'ajax' function to use to make this request?

I will store my hits in a separate 2 col table to the content (col1="page_id" col2="hits"), to prevent cache flushing

I should also mention that I am not looking to start a discussion on the 'value' of hits, I am simply interested in which jquery events and ajax methods to use...

+2  A: 

best event to bind to when ALL content is finished loading? (ie doc.ready?)

window.load event, by the time all resources including DOM, images, frames, etc have loaded.

best jquery 'ajax' function to use to make this request?

The $.ajax method (all ajax functions of jquery are inheriting from this method).

Sarfraz
+1  A: 

First, I would recommend putting the script to increment page loads straight in the PHP, why call it from JS? You're numbers will be off depending on how many of your site' visitors have Javascript disabled. Putting the script right in the PHP eliminates that problem.

Second, I am not totally sure about this, but I feel that storing something like this might be faster in a file. A database like this, could get pretty big pretty fast. It may be more efficient to store the data like this:

1    2303
2    4548
3    2865
4    6412

Where the first col is the page_id and the second is the number of hits for a specific timeframe.

Again, I don't have metrics on this, but I feel like using a database for this sort of thing is generally frowned upon because of how quickly the tables can get extremely large and unmanageable.

jordanstephens
Not counting site visitors without JavaScript has its advantages - it doesn't log the search engine robots and spam crawlers. The vast majority of human users have it enabled, and the vast majority of automated users don't.
ceejayoz
Why is the table going to become unmanageable, there may be other reasons to do it other ways, but the database has two columns and 1 row per page. The script simply increments the count in each row. He didn't say he was creating a new row per page request, simply one row per page and increment the count!
yes, i would say maintaining and parsing a file could be much more troublesome, mysql should be much more efficient?
Haroldo
@ceejayoz good point, @MrXexxed sorry, I don't know where I got the idea that the OP was adding a new row for every load. I re-read the question and updated my answer. And like i said, I don't have metrics to prove this, but I am pretty sure there are obvious speed increases in using files over database in these situations. And I do think it's something that should be atleast looked into--considering the question title is "FASTEST way to store hits..."
jordanstephens
@jordanstephens - also write/insert operations are (proportionately) slow, so i want to avoid them delaying page load, hence pinging the increment.php only when all other items are loaded
Haroldo
@jordanstephens - i will look into files, thanks for the suggestion
Haroldo