I'm not looking for an specific answer but more of a general direction.
Recently I decided to make my own stats plugin. I know there are tons out there, but I wanted to make my own. I figured, how hard can it be? Just create a table, log the IP, and on every page refresh, store data in a table.
The problem I'm having is that the stats seem to be too high. I've deployed my plugin on a test batch of sites with PR of ~ 1, and the plugin is showing thousands of visits, and I know there aren't that many uniques coming.
Here's the entire plugin: https://gist.github.com/1469300
Here's my process visitor function.
/**
* Process visitor
*
* Checks whether used is "logged" or not,
* Adds the user to the unique table if not logged,
* adds a hit using the user_id if she is logged
*
*/
function vsp_process_visitor() {
global $wpdb;
$visits_table = $wpdb->prefix . "vsp_visits";
if ( vsp_visitor_logged_in() ) {
vsp_update_count();
}
if ( !vsp_visitor_logged_in() ) {
vsp_new_record( $visits_table );
}
}
Here's the check to see if the session is set or not.
/**
* Checks user state
*
* Checks whether a user session is set
* Logged in if set
*
*/
function vsp_visitor_logged_in() {
$logged_in = false;
if ( isset( $_SESSION[ 'vsp_user' ] ) ) {
$logged_in = true;
}
return $logged_in;
}
Here's the function that inserts data.
/**
* Creates new unique and hit
*
* Inserts data into the unique table and the
* hits table, if allowed.
*
*/
function vsp_new_record( $table ) {
$ip = get_ip();
global $wpdb;
$hits_table = $wpdb->prefix . "vsp_hits";
/*
Insert data into the visitors table
*/
$data = array ( 'ip' => $ip );
if ( valid_time($table) ) {
$wpdb->insert ( $table, $data );
}
$userID = $wpdb->get_var( 'select `id` from ' . $table . ' where `ip` = "' . $ip . '" order by `id` desc limit 1;');
$_SESSION[ 'vsp_user' ] = $userID;
/*
Insert data into the hits table
*/
$data = array( 'visit_id' => $userID );
if ( valid_time($hits_table) ) {
$wpdb->insert ( $hits_table, $data );
}
}
The code is executed at a refresh. My question is, when a search robot visits my website, will it "trigger" a page refresh, and create a table in my stats database, skewing my results.
App Logic
A user comes to the site Question: Is the session set? If no, then create a session with user's IP, and add a hit and unique If yes, then add a hit with user's IP
Am I missing something else?