EllisLab text mark
Advanced Search
     
Page views counter + caching?
Posted: 06 September 2007 10:23 AM   [ Ignore ]
Avatar
Joined: 2007-08-22
27 posts

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.

 Signature 
$config['log_threshold'2
 
Posted: 07 September 2007 09:32 AM   [ Ignore ]   [ # 1 ]   [ Rating: 0 ]
Avatar
Joined: 2007-08-22
27 posts

What, nobody in CI community is counting page views?

 Signature 
$config['log_threshold'2
 
Posted: 07 September 2007 09:52 AM   [ Ignore ]   [ # 2 ]   [ Rating: 0 ]
Joined: 2006-07-14
4237 posts

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

// 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.

 
Posted: 07 September 2007 10:10 AM   [ Ignore ]   [ # 3 ]   [ Rating: 0 ]
Avatar
Joined: 2007-02-14
95 posts

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…

 Signature 

Trying to do it right the first time! whoops guess that didn’t work out so well…

 
Posted: 07 September 2007 12:24 PM   [ Ignore ]   [ # 4 ]   [ Rating: 0 ]
Avatar
Joined: 2006-08-06
918 posts

I just added a question and answer to the FAQ

 Signature 

peeker email (imap/pop) | site_migrate | OOCalendar | PhotoBox2 | word_limiter

 
Posted: 07 September 2007 01:41 PM   [ Ignore ]   [ # 5 ]   [ Rating: 0 ]
Avatar
Joined: 2007-02-08
409 posts

I think that the way that Code Arachn!d suggested is probably you best bet.

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

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

Then you would have

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…

 Signature 

you’ve got that sexy Canadian thing working for you… - Derek Allard


Pancake Payments | http://DearIE6.com
http://twitter.com/thatleeguy

 
Posted: 07 September 2007 01:42 PM   [ Ignore ]   [ # 6 ]   [ Rating: 0 ]
Avatar
Joined: 2007-02-08
409 posts

weird…. wont show my js in code blocks and double post…


Sorry all

Yea post count increase :D

 Signature 

you’ve got that sexy Canadian thing working for you… - Derek Allard


Pancake Payments | http://DearIE6.com
http://twitter.com/thatleeguy

 
Posted: 07 September 2007 01:53 PM   [ Ignore ]   [ # 7 ]   [ Rating: 0 ]
Avatar
Joined: 2006-08-06
918 posts

aren’t js scripts called like that cached in the browser? if so, the problem remains.

 Signature 

peeker email (imap/pop) | site_migrate | OOCalendar | PhotoBox2 | word_limiter

 
Posted: 07 September 2007 02:33 PM   [ Ignore ]   [ # 8 ]   [ Rating: 0 ]
Avatar
Joined: 2007-02-14
95 posts
sophistry - 07 September 2007 05:53 PM

aren’t js scripts called like that cached in the browser? if so, the problem remains.

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:

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

you would call:

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.

 Signature 

Trying to do it right the first time! whoops guess that didn’t work out so well…

 
Posted: 07 September 2007 02:44 PM   [ Ignore ]   [ # 9 ]   [ Rating: 0 ]
Avatar
Joined: 2007-02-08
409 posts

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….

Dino Riders 2
Actual Images of toys

 Signature 

you’ve got that sexy Canadian thing working for you… - Derek Allard


Pancake Payments | http://DearIE6.com
http://twitter.com/thatleeguy

 
Posted: 07 September 2007 05:39 PM   [ Ignore ]   [ # 10 ]   [ Rating: 0 ]
Avatar
Joined: 2007-02-14
95 posts

Whoa! I never had those growing up - que ponderment - what else have I missed out on? ;D

 Signature 

Trying to do it right the first time! whoops guess that didn’t work out so well…