1

I have site with thousands of user and logins each day. I need to track each page views of each user to generate a report.

If a user 'A' visits a page 'P' today, a new entry daycount is added to the DB. If the same user 'A' visits the same page 'P' on same day, the daycount will increment by one. If a user 'A' visits page 'P' tomorrow, a new entry daycount is added to the DB.

I am using drupal 7. The insert and update works properly if there is no date comparison. Once i added if($TimeNow > $UpdateTime), the page load never ends. Can anyone help me to fix this.

if(db_query('SELECT daycount FROM {report_page_view} WHERE id = 9999999990 AND uid = :uid )', array(':uid' => $GLOBALS['user']->uid))->rowCount() == 0) {

    $ChQry = "(SELECT timestamp FROM {report_page_view} WHERE id = 9999999990 AND uid = :uid )', array(':uid' => $GLOBALS['user']->uid)"; // Get the previous timestamp
    $ChResult = db_query($ChQry);

    while ( $ChGetResult = $ChResult->fetchAssoc() ) {
       $TimeStm = $ChGetResult['timestamp']; 
    }   
        $UpdateTime = $TimeStm + 86400; //Get the next day 
        $TimeNow = time();
        if($TimeNow > $UpdateTime) { // Compare current time and tomorrows date
        // insert if condition works
        $insert_page = db_insert('report_page_view')->fields(array('title', 'memberorg', 'id', 'type', 'timestamp', 'daycount', 'uid', 'fname', 'lname'));
            $insert_page->values(array(
            'title' => $Title,
            'memberorg' => $MemberOrg,
            'id' => 9999999990,
            'type' => 'views',
            'timestamp' => REQUEST_TIME,
            'daycount' => 1,
            'uid' => $UserId,
            'fname' => $FName,
            'lname' => $LName,

          ));               
        $insert_page->execute();
    }
    } // else update 
    db_update('report_page_view')
    ->fields(array(
            'title' => $Title,
            'memberorg' => $MemberOrg,
            'id' => 9999999990,
            'type' => 'views',
            'timestamp' => REQUEST_TIME,
            'daycount' => 1,
            'uid' => $UserId,
            'fname' => $FName,
            'lname' => $LName,
    ))
    ->expression('daycount', 'daycount + 1')`
0

Consider using the Node view count module for this. Here is a quote about it (from its project page, bold markup added here):

... allows to count node views. Unlike statistics module, this module records each view of node with user uid and timestamp. So now you can know how many times a user viewed node by day, week, month and etc.

This module is integrated with Views module. You can use it to create different statistics pages, output new not viewed nodes for user and many more.

By using this module, and to the extend it should be needed, you can then further refine / extend what you get (out-of-the-box) from this module via a custom module(s) and/or view(s).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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