We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 28042 ☆ A M B ☆
    • 24,524 Posts
    Something has been bugging me for a long time about the visitor statistics, and I just realized that the GetStats snippet has things backwards! A new site, and it was reporting 3 unique visitors with 1 visit! Plus I fixed it to make it more CSS friendly and XHTML valid.

    /*
     *	GetStats 
     *	Fetches the visitor statistics totals from the database
     */
     
    $tmpArray = $modx->getSiteStats();
    $output = "
    <table id='stats' cellspacing='1' cellpadding='5'>
        <tr>
            <td class='toprow'> </td>
            <td class='toprow'>Page Impressions</td>
            <td class='toprow'>Visits</td>
            <td class='toprow'>Visitors</td>
        </tr>
        <tr>
            <th>Today</th>
            <td>".number_format($tmpArray['piDay'])."</td>
            <td>".number_format($tmpArray['visDay'])."</td>
            <td>".number_format($tmpArray['viDay'])."</td>
        </tr>
        <tr>
            <th>This Month</th>
            <td>".number_format($tmpArray['piMonth'])."</td>
            <td>".number_format($tmpArray['visMonth'])."</td>
            <td>".number_format($tmpArray['viMonth'])."</td>
        </tr>
        <tr>
            <th>All Time</th>
            <td>".number_format($tmpArray['piAll'])."</td>
            <td>".number_format($tmpArray['visAll'])."</td>
            <td>".number_format($tmpArray['viAll'])."</td>
        </tr>
    </table>
    ";
    
    return $output;


    The original had ’visAll’ and ’viAll’ reversed. Or else it’s getting put into the wrong fields in the database to begin with. Whatever, this fixes it.
      Studying MODX in the desert - http://sottwell.com
      Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
      Join the Slack Community - http://modx.org
      • 27
      • 117 Posts
      nice one!

      Im just wondering how this is scoped, is it stats for the whole site or just the page the snippet appears on? the later is what im after...

      big shout out to all involved at modx

      regards

      J
        • 27
        • 117 Posts
        Seems to be for the whole site, I should have had the cop on to put the snippet on a different page and see if the results were any different (they werent)

        doh, modx admin site stats are cool anyway, so i aint trippin’

        Cheers

        J
          • 28373
          • 204 Posts
          Hi guys

          We’re using this snippet to show page views on escortsnz.com, seems to tick over quite accurately.

          STEP 1
          Create a snippet, call it
          PageViews

          STEP 2
          Put this code in it
          
          $username="your_db_username";
          $password="your_db_password";
          $database="your_db_name";
          
          mysql_connect(localhost,$username,$password);
          @mysql_select_db($database) or die( "Unable to select database");
          $sql = "SELECT COUNT(*) FROM modx_log_access WHERE document=$pageNum";
          
          $rs = mysql_query($sql);
          $tmp = mysql_fetch_assoc($rs);
          $piAll = $tmp['COUNT(*)'];
          mysql_close();
          echo "<p class='totalpageviews'>This page has been viewed: $piAll times</p>";
          


          STEP 3
          Save the snippet smiley

          USAGE

          [[PageViews? &pageNum=’47’]]

          ’47’ is the page number where you’ve put the snippet.

          Yes, the results returned are ’just’ for the referring page number. The script is easily modified to cover daily, monthly etc...

          You can see it in action at any of the escort profile pages at

          http://www.escortsnz.com

          Warning: shocked:o:o Page contains mild adult content. shocked:o:o

          Peace !

            • 15987
            • 786 Posts
            Starbuck, here is an updated version of your code that uses the modx api so you don’t need all of the mysql conenct stuff and the username and password.

            $pageNum = $modx->documentObject['id'];
            
            $table = $modx->getFullTableName("log_access");
            $results = $modx->db->select("count(*)", $table, "document=$pageNum");
            
            $piAll = mysql_result($results,0);
            
            echo "<p class='totalpageviews'>This page has been viewed: $piAll times</p>";
            


            Kyle
              • 28373
              • 204 Posts
              Quote from: kylej at May 21, 2006, 08:12 AM

              Starbuck, here is an updated version of your code that uses the modx api so you don’t need all of the mysql conenct stuff and the username and password.

              $pageNum = $modx->documentObject['id'];
              
              $table = $modx->getFullTableName("log_access");
              $results = $modx->db->select("count(*)", $table, "document=$pageNum");
              
              $piAll = mysql_result($results,0);
              
              echo "<p class='totalpageviews'>This page has been viewed: $piAll times</p>";
              


              Kyle

              Yes, that’s better!

              Tested works great - has this been done before somewhere? I searched all the docs and found a lot of confusion and 3rd party recommendations. Modx stats work fine for this particular application, advanced stats, we’re using awstats.

              Nice one Kyle smiley
                • 15987
                • 786 Posts
                I don’t know if this stat stuff has been done before. I just thought this would be much easier using the api so I did the modification.

                Kyle
                  • 28373
                  • 204 Posts
                  Quote from: kylej at May 21, 2006, 09:47 AM

                  I don’t know if this stat stuff has been done before. I just thought this would be much easier using the api so I did the modification.

                  Kyle

                  Well - that was very nice of you Kyle, I appreciate it smiley

                  -Starbuck
                    • 15987
                    • 786 Posts
                    No problem, always glad to help when I have time wink