Extend Core Statistic system

Concrete5 comes with a simple statistic system, can keep track of number of visits in your site. If you enable statistics (Dashboard>System & settings>Statistics), for every page view, concrete insert a record into the pageStatistics table. If you take a look at this table you would see it contains these fields:

cID: visited page id
date
timestamp
uID: default=0, user id, if a logged-in user visit the page

At the Dashbord>Reports>Statistics you only can see number of page views at last 7 days. In this how-to I will show you how you can add other reports (that can extracted from the inserted data) to statistic page. Suppose you want to add a list that show top 10 most popular pages of your site at the statistic page.

1- Tweaking Model: first we add a method to page statisic model. For this copy /concrete/models/page_statistics.php to /models/page_statistics.php. this will override the main file. Now add this method that run a query, get data we need for our report:

public static function getTotalPageViewsByPage() {
     $db = Loader::db();
     return $db->GetAll("select cID,count(pstID) AS num from PageStatistics GROUP BY cID HAVING num>0 ORDER BY num DESC LIMIT 10");
}


2- Tweaking Controller: copy /concrete/controllers\dashboard/reports/statistics.php to /controllers\dashboard/reports/statistics.php. this will override the main file. Now add this method that get the required data from the method added in previous step:

public function view() {
        $this->addHeaderItem(Loader::helper('html')->javascript('jquery.visualize.js'));
        $this->addHeaderItem(Loader::helper('html')->css('jquery.visualize.css'));
        $this->setLatestPageViews();
        $this->setLatestPagesCreated();
        $this->setLatestRegistrations();
        $this->setDownloadStatistics();
 
        $this->set('totalVersions', PageStatistics::getTotalPageVersions());
        $this->set('totalEditMode', PageStatistics::getTotalPagesCheckedOut());
 
        $this->set('totalPageViewsByPage', PageStatistics::getTotalPageViewsByPage());
}

Note: this method copied/pasted from /concrete/core/controllers/single_pages/dashboard/reports/statistics.php, I just added last line.

3- Tweaking Single Page: copy /concrete/single_pages/dashboard/reports/statistics.php to /single_pages/dashboard/reports/statistics.php. this will override the main file. Now add this table at the end of the file, befor script tag, that print data into the statistic single page:

<div class="row">
<div class="span-pane-half">
 
    <h3><?php echo t('Top 10 Popular Pages') ?></h3>
        <table border="0" cellspacing="1" cellpadding="0" class="ccm-results-list">
            <thead>
                <tr>
                    <th class="subheader"><?php  echo t('#') ?></th>
                    <th class="subheader"><?php  echo t('ID') ?></th>
                    <th class="subheader" width="100%"><?php  echo t('Page') ?></th>
                    <th class="subheader"><?php  echo t('Visits') ?></th>
                </tr>
            </thead>
 
            <?php  
            $i=0;
            foreach($totalPageViewsByPage as $key=>$value) { 
              $p = Page::getByID($value['cID']);
              if(!$p->cIsSystemPage){
            ?>
                <tr>
                    <td style="text-align:center;"><?php echo ++$i ?></td>
                    <td style="text-align:center;"><?php echo $value['cID'] ?></td>
                    <td><?php  echo $p->vObj->cvName; ?></td>
                    <td style="text-align:center;"><?php echo $value['num'] ?></td>
                </tr>
            <?php  }} ?>
        </table>
</div>
</div>

Now go to the statistic, you should see table, list top 10 popular pages of your site.

Comments