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:
- user visits a page
- 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...
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).
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.
精彩评论