Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question
add comment

1 Answer

You've got one very large issue here - you're using $_SESSION.

WordPress, by default, doesn't use session variables. So isset( $_SESSION['vsp_user'] ) will always return false because WP isn't keeping track of things.

There are some additional changes you'll need to make and hooks you'll need to tie in to in order to enable sessions in WP. Just to get you started, here are some resources:

Once you've actually got sessions turned on, you should see far less "false positives" in your stat tracking.

share|improve this answer
    
OK thank you for pointing these out. –  21zna9 Dec 13 '11 at 0:05
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.