CodeIgniter Forums
Page views counter + caching? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Page views counter + caching? (/showthread.php?tid=3042)



Page views counter + caching? - El Forum - 09-06-2007

[eluser]#1313[/eluser]
I want to count how many times each of my pages are loaded and also want to use caching. I didn't dig deeply into CI caching mechanisms and i don't know exactly how they are working, so i need an advice. Where is the best place in the code for my counter incrementing routines? I want to avoid situations when a page is loaded from cache and counter isn't triggered.

And second question in this topic: what should i do if i want to additionally display the number of page views on the page (i.e. "This page was viewed 123 times")? I read in the guide that some info such as benchmarking does not cache — how can i mimic this behavior?

Thanks in advance.


Page views counter + caching? - El Forum - 09-07-2007

[eluser]#1313[/eluser]
What, nobody in CI community is counting page views?


Page views counter + caching? - El Forum - 09-07-2007

[eluser]xwero[/eluser]
Who counts pages if you have ajax Smile

If you would want to count the loading of a page you can add a log function of an autoloaded library in every controller function

Code:
// controller
function page1()
{
  $this->testlog->add($pageid);
}
// library
class Testlog
{
  function add($id)
  {
    // database stuff
  }
}

As for the displaying of the number i don't know if the native cache functionality has the possibility to not cache certain pageparts but i have seen a library where it was possible but i don't remember if it was in the wiki or on the forum.


Page views counter + caching? - El Forum - 09-07-2007

[eluser]Code Arachn!d[/eluser]
y not use a separate controller to handle your logging - and then you can just pass the urls through js (think Google analytics)... And in that vein is there any reason an existing tracking product wouldn't work? Especially if all you're trying to do is count hits...


Page views counter + caching? - El Forum - 09-07-2007

[eluser]sophistry[/eluser]
I just added a question and answer to the FAQ


Page views counter + caching? - El Forum - 09-07-2007

[eluser]CI Lee[/eluser]
I think that the way that Code Arachn!d suggested is probably you best bet.

Code:
script src="http://yourdomain.com/update/109" type="text/javascript"

with 109 being the page/article/blog id number....

Then you would have

Code:
function update()
  {
    $id = (int) $this->uri->segment(3);
  
      //Do some database query's with that $id

     $sql = "UPDATE blah with stuff and more stuff WHERE article_id = $id LIMIT 1";
     $query  = $this->db->query($sql);
  
  }
}
Not even close to working but you probably get the point...


Page views counter + caching? - El Forum - 09-07-2007

[eluser]CI Lee[/eluser]
weird.... wont show my js in code blocks and double post...


Sorry all

Yea post count increase Big Grin


Page views counter + caching? - El Forum - 09-07-2007

[eluser]sophistry[/eluser]
aren't js scripts called like that cached in the browser? if so, the problem remains.


Page views counter + caching? - El Forum - 09-07-2007

[eluser]Code Arachn!d[/eluser]
[quote author="sophistry" date="1189201984"]aren't js scripts called like that cached in the browser? if so, the problem remains.[/quote]

You should be able to "bust" caching with .js files using the same way you do it with banner ad codes, by appending a random number to the end of the .js call. For example, instead of calling:

Code:
script src="http://mysite/test.js"

you would call:

Code:
script src="http://mysite/test.js?randomnum=xxx"

where "xxx" is a random number. There are many ways to generate xxx obviously, including via JavaScript itself.


Page views counter + caching? - El Forum - 09-07-2007

[eluser]CI Lee[/eluser]
I think this is something I am going to have to try to see for myself....

I'll let you know if I actually complete it or my ADD kicks in and I go searching ebay for DinoRiders action figures again....

[url="http://7am.ca/assets/dinoriders2.jpg"]Dino Riders 2[/url]
[url="http://7am.ca/assets/dinoriders.jpg"]Actual Images of toys[/url]