Page Hit Counter

Permalink 3 users found helpful
As part of an ongoing project I needed to place a simple page counter on all my pages... and I couldn't find any way to do this with C5.

I know I could sign up to some 3rd party counter site but didn't want this for something so simple.

I know I could have got one of the many free php scripts out there and hooked it into my template but I didn't want the extra overhead/work.

What I really wanted was for C5 to do it for me and I dont know if the stats addon does what I want... It doesn't say it can provide a visible counter..

so...

The pageStatistics model in C5 provids a function to count the total hits accross ALL pages but not by individual pages. So I extended the built in pageStatistics model with a simple pageCounter model.

It has a single function getTotalPageViewsForPageID

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));
      }
   }
}


I placed the new page_counter.php model file into my root models folder (not under concrete5)

I then placed the following code
global $c;
Loader::model('page_counter');
echo PageCounter::getTotalPageViewsForPageID($c->cID);

into my footer and hey presto a page counter...

Knowing my luck there is probably already a really easy way to get C5 to produce a page counter but I couldn't find it so just in case there isn't and if anyone else wants this the model is attached...

1 Attachment

JimboJetset
 
Mnkras replied on at Permalink Reply
Mnkras
nice!

want me to make it into a package?
binoy replied on at Permalink Reply
binoy
Great! It will be very useful if you could make it as a package. Thank you JJ.
JimboJetset replied on at Permalink Reply
JimboJetset
To be honest.. putting it into a package isn't a problem... it just didn't seem worth the effort.

However if thats how you want it then I'll package it up and post back here.
tallacman replied on at Permalink Reply
tallacman
I'd use it.
binoy replied on at Permalink Reply
binoy
Count me also.
JimboJetset replied on at Permalink Reply
JimboJetset
"Count me also."

Sorry binoy... it will only count page hits ;-)
murray replied on at Permalink Reply
murray
Works great. Thanks for putting the effort into it.

PS a google search for 'concrete5 counter' put this page at the top.

Murray
adamjohnson replied on at Permalink Reply
adamjohnson
Nice post. Would make a simple and effective add on--despite how easy it is to implement in the theme.

Thanks for the post.
tallacman replied on at Permalink Reply
tallacman
This works great, thanks. I've wrapped it in a "display if logged in" only wrapper. What I'd really like to do is display it only if the user in the page owner. Anybody got any ideas on that?

Steve
JimboJetset replied on at Permalink Reply
JimboJetset
Not tested but should work...
$u = new User();
if($u->getUserId() == $c->getCollectionUserID()) {
    //display counter
    }
dutchforkrunners replied on at Permalink Reply
How do I add this to my footer? I'm using GreenSalad theme right now.
cannonf700 replied on at Permalink Reply
cannonf700
Jimbo pretty much explains how to do it. Find your greensalad theme's footer.php (it in Root> Concrete> Themes> GreenSalad> Elements> footer.php)
Copy the following code into your footer
<?php
global $c;
Loader::model('page_counter');
echo PageCounter::getTotalPageViewsForPageID($c->cID);
?>
somewhere around your sitename php call or login php call function.

then download jimbo's attached file 'page_counter.php' and put it in your sites 'models' folder ( Root> Modals> )

That should do it...
dutchforkrunners replied on at Permalink Reply
Thanks. My footer only has the following content..
<?php defined('C5_EXECUTE') or die(_("Access Denied.")); ?>

<?php Loader::element('footer_required'); ?>

</body>
</html>

So would I put the code before the body closing tag?
Mnkras replied on at Permalink Reply
Mnkras
yep :)
cannonf700 replied on at Permalink Reply
cannonf700
oops,
That means you'll have to add the code to each of greensalad's page types (default, view, home, and full). scroll towards the bottom of each page type and look for this line of code:
<div id="footer">
put the counter code somewhere after that footer tag. by looking at the code you can see where the 'site name' 'login' and other stuff is so you can decide where you want to put the counter and do it.
I would make a copy of the theme before you start messing with it too much, that way if all goes wrong you can start over.
dutchforkrunners replied on at Permalink Reply
I found it - it seems to be a problem with how GreenSalad uses elements/footer.php

I had to manually edit
view, default, full, and home in the
themes/greensalad folder

its as if greensalad them is ignoring the

<?php $this->inc('elements/footer.php'); ?>

I even tried making the elements/footer.php off of root, but it still didn't work.
tallacman replied on at Permalink Reply
tallacman
This code works great. Thanks JimboJetset! Quick question before I take a look, Where is this number stored in the database? I need to zero the page count.
JimboJetset replied on at Permalink Reply
JimboJetset
Well as I see it you have two options...


Option 1: you could simply subtract the current number of hits from the displayed total.
echo PageCounter::getTotalPageViewsForPageID($c->cID) - 1234;
replace the '1234' value with the current number of hits.


Option 2: (ignoring the PHP tags in the code below) run the following mysql query against the database (make sure you backup beforehand)
delete from PageStatistics where cID = '1234'
replace '1234' (but keep the quotes...) with the cID of the page whose count you want to reset.
fastcrash replied on at Permalink Reply
fastcrash
thanks jimbo! you are awesome,
i started searching for 'page hit', and found this helpull thread, thanks.

i want to put,
Profile viewed : PageCounter::getTotalPageViewsForPageID($c->cID)

is the script already on package?

edit: i dont see any file called 'page_counter.php', where is the file?

i use c5 5.4.1.1

thanks
themazza replied on at Permalink Reply
Hello
Can you please direct me to the hit counter package.

I am looking for a counter to put in the footer of my site and found this thread however I cant see the package created for it? I have a recent version of Concrete5 having just downloaded it about a month ago.
Thank you very much.
12345j replied on at Permalink Reply
12345j
look at the original post for an attachment.
themazza replied on at Permalink Reply
Thank you. Because it is over a year old I was worried that it might not work with my later version of Concrete 5, and also thought that perhaps something had been made as an add on more recently. But I will try this as you suggest.
Cheers
themazza replied on at Permalink Reply
Thank you. Worked like a gem.
Cheers
Biged2011 replied on at Permalink Reply
Biged2011
This page counters works very well. I am new at this changing things, how can this be adapted to work in a block added to a page and still show the page data from the query?
I can't get the variables to work!
JimboJetset replied on at Permalink Reply
JimboJetset
I have now generated a free page counter block based on my original code above but modified to include graphical and custom counters as well...

http://www.concrete5.org/marketplace/addons/page-hit-counter/...

enjoy :-)
pendragn replied on at Permalink Reply
pendragn
Wonderful! The counter works great!

Do you know if there is a way to reset the counter on a specific page to "0"?

Thanks!
pppp replied on at Permalink Reply
how to use this feature in a pagelist block to sort pages asc/desc with it?
pppp replied on at Permalink Reply
or is in Concrete5 any feature to sort pagelist by most viewed pages?