Sometimes it is required to display the number of page views of blog posts, discussions or other pages. So this how to elaborate the procedure.
Firstly you need to create a page called page_counter.php and put the below code there:
print("<?php
defined('C5_EXECUTE') or die(_("Access Denied."));
Loader::model('page_statistics');
class PageCounter extends PageStatistics {
public static function getTotalPageViewsForPageID($cID = null, $date = null) {
$db = Loader::db();
if ($date != null) {
return $db->GetOne("select COUNT(pstID) FROM PageStatistics where date = ? and cID = ?", array($date,$cID));
} else {
return $db->GetOne("select COUNT(pstID) FROM PageStatistics where cID = ?", array($cID));
}
}
public static function getRegisteredPageViewsForPageID($cID = null, $date = null) {
$db = Loader::db();
if ($date != null) {
return $db->GetOne("SELECT COUNT(pstID) FROM PageStatistics WHERE date = ? AND cID = ? AND uID != 0", array($date,$cID));
} else {
return $db->GetOne("SELECT COUNT(pstID) FROM PageStatistics WHERE cID = ? AND uID != 0", array($cID));
}
}
public static function getVisitorsPageViewsForPageID($cID = null, $date = null) {
$db = Loader::db();
if ($date != null) {
return $db->GetOne("SELECT COUNT(pstID) FROM PageStatistics WHERE date = ? AND cID = ? AND uID = 0", array($date,$cID));
} else {
return $db->GetOne("SELECT COUNT(pstID) FROM PageStatistics WHERE cID = ? AND uID = 0", array($cID));
}
}
}
");
Then upload the page to roots models folder. Here I've defined 3 different methods which counts total page views, page views by registered users and page views by visitors. After that you need to put the below code where you want to display the page view statistics.
print("Loader::model('page_counter');
echo '<strong>Total Page Views: '.PageCounter::getTotalPageViewsForPageID($cobj->getCollectionID()).'</strong>';
echo 'Registered User <strong>('.PageCounter::getRegisteredPageViewsForPageID($cobj->getCollectionID()).')</strong>, ';
echo 'Visitors <strong>('.PageCounter::getVisitorsPageViewsForPageID($cobj->getCollectionID()).')</strong>';");
Note: $cobj->getCollectionID() is the page ID, might be changed if any other variable is used instead of $cobj.
Hope this Helps.
Loading Conversation