NewStatPress corrects the total number of visitors

update Last updated: June 14, 2022 at 1:00 PM

In this site, using wordpress plug-in "NewStatPress", the number of pages and visitors are displayed in the widget of the sidebar at the same time as performing the access analysis of the site.

However, when i analyzed the program of this NewStatPress, it was found that the display of the widget slows down as the data accumulated in DB increases.
Apparently, in order to maintain the performance of the site, it seems necessary to periodically refresh the data accumulated in the database used by NewStatPress.

However, when the data of the DB is cleared, the counter displayed by NewStatPress is also reset, so this process can not be easily performed. 👉 This behavior appears to have been a bug. (Updated May 16) Look at [resolved] access number automatically reset everyday (4 posts)

Therefore, I did not want to do, as a last resort, I was to fix the program of NewStatPress.
As far as searching the net, the latest version of NewStatPress (0.9.7), it seems that there are still no people who made this fix in Japan.
It took me a while to find the fix, but I finally found it.

The site had 7,400 visits between January 15 and March 14, but the next day, by clearing db data, the counter became 0, so we modified the program to add +7400 to the total number of visitors.

For your information, here's how to fix it.
Please note that I am not responsible for any problems that may occur in this process.
If you modify the program based on this, please do so at your own risk.

Modify the API (variables.php) called from the module that controls Ajax shown below.
The content is mostly a collection of SQL ...

* What is Ajax?

<?php
#error_reporting(E_ALL);
#ini_set('display_errors', 1);

if($_SERVER['REQUEST_METHOD'] != "GET") die("API available only from Newstatpress");

require_once('../../../../../wp-load.php');

// get the parameter from URL
$var = $_REQUEST["VAR"];

global $wpdb;
$table_name = $wpdb->prefix . "statpress";


// test all vars
if ($var=='alltotalvisits') {
  $qry = $wpdb->get_results(
  "SELECT count(distinct urlrequested, ip) AS pageview
   FROM $table_name AS t1
   WHERE
    spider='' AND
    feed='' AND
    urlrequested!='';
   ");
   if ($qry != null) {
     echo $qry[0]->pageview;
   }
} elseif ($var=='visits') {
    $qry = $wpdb->get_results(
      "SELECT count(DISTINCT(ip)) AS pageview
       FROM $table_name
       WHERE
        date = '".gmdate("Ymd",current_time('timestamp'))."' AND
        spider='' and feed='';
      ");
   if ($qry != null) {
     echo $qry[0]->pageview;
   } 
} elseif ($var=='yvisits') {
    $qry = $wpdb->get_results(
      "SELECT count(DISTINCT(ip)) AS pageview
       FROM $table_name
       WHERE
        date = '".gmdate("Ymd",current_time('timestamp')-86400)."' AND
        spider='' and feed='';
      ");
   if ($qry != null) {
     echo $qry[0]->pageview;
   }  
} elseif ($var=='mvisits') {
    $qry = $wpdb->get_results(
      "SELECT count(DISTINCT(ip)) AS pageview
       FROM $table_name
       WHERE
        date LIKE '".gmdate('Ym', current_time('timestamp'))."%'
        spider='' and feed='';
      ");  
   if ($qry != null) {
     echo $qry[0]->pageview;
   }
} elseif ($var=='totalvisits') {
    $qry = $wpdb->get_results(
      "SELECT count(DISTINCT(ip)) AS pageview
       FROM $table_name
       WHERE
         spider='' AND
         feed='';
      ");
   if ($qry != null) {
     echo $qry[0]->pageview;
   }
} elseif ($var=='totalpageviews') {
    $qry = $wpdb->get_results(
      "SELECT count(id) AS pageview
       FROM $table_name
       WHERE
         spider='' AND
         feed='';
      ");  
   if ($qry != null) {
     echo $qry[0]->pageview;
   }
} elseif ($var=='todaytotalpageviews') {
    $qry = $wpdb->get_results(
      "SELECT count(id) AS pageview
       FROM $table_name
       WHERE
         date = '".gmdate("Ymd",current_time('timestamp'))."' AND
         spider='' AND
         feed='';
      ");  
   if ($qry != null) {
     echo $qry[0]->pageview;
   }
} elseif ($var=='thistotalvisits') {
    $url = esc_sql($_REQUEST["URL"]);

    $qry = $wpdb->get_results(
      "SELECT count(DISTINCT(ip)) AS pageview
       FROM $table_name
       WHERE
         spider='' AND
         feed='' AND
         urlrequested='".$url."';
      ");
   if ($qry != null) {
     echo $qry[0]->pageview;
   }  
} elseif ($var=='widget_topposts') {
    $limit = intval($_REQUEST["LIMIT"]);
    $showcounts = $_REQUEST["FLAG"];
    
    $res="\n<ul>\n";
    $qry = $wpdb->get_results(
      "SELECT urlrequested,count(*) as totale
       FROM $table_name
       WHERE
         spider='' AND
         feed='' AND
         urlrequested LIKE '%p=%'
       GROUP BY urlrequested
       ORDER BY totale DESC LIMIT $limit;
      ");
   foreach ($qry as $rk) {
     $res.="<li><a href='?".$rk->urlrequested."' target='_blank'>".iri_NewStatPress_Decode($rk->urlrequested)."</a></li>\n";
     if(strtolower($showcounts) == 'checked') { $res.=" (".$rk->totale.")"; }
   }
   echo "$res</ul>\n";
}

?>


Modify "Line 71" of this program as follows. (On this site, +7400)

   if ($qry != null) {
     // The line below has been modified by Senri on 2 April 2015.
     echo $qry[0]->pageview + 7400;
   }


The display results of the widget before and after the modification of the program are shown below.

* Added on August 11
By the way, I set the aggregation period to 3 months to prevent the NewStatPress DB from becoming too large, and use the Perl program aggregation value instead of the NewStatPress counter value for the total counter aggregation. I did it.
(Perl program call from within front-page.php)

2022.06.13 Update
As I checked at the time of writing this article (NewStatPress 1.4.2), offset values such as Visitors, Pageviews, etc., can be set.

2024,06.03 Added
Currently, this plugin has not been updated for a long time and was causing compatibility issues and bugs, so I have uninstalled it and am using the following plugin "SlimStat Analytics" instead.

Add this entry to the hasebookmark
X (post)

Leave a Reply