5

How do I count the number of views(Hits) in magento? Are there any built in methods available in magento?

EDIT from the comment:

I need total views for the entire site. I got the online users count from this code:

$visitor_count = Mage::getModel('log/visitor_online')
                    ->prepare()
                    ->getCollection()
                    ->count(); 
if(!empty($visitor_count) && $visitor_count > 0) {
    $cnt = $visitor_count; 
    echo 'Visitors online :'.$cnt; 
} 
Jason
  • 15,017
  • 23
  • 85
  • 116
Gopesh
  • 3,882
  • 11
  • 37
  • 52
  • Total views for your entire site, or views of a single page? Over what period of time? Have you tried _anything_ yourself, or done any research? – Bojangles Jan 04 '12 at 09:02
  • 5
    I know this might not be what you're asking, but wouldn't you be better off using Google Analytics? – Max Jan 04 '12 at 10:12

3 Answers3

0

The problem with the code that you put in the top is that it will result in a table scan, which you probably don't want. Additionally, you don't want to be writing any SQL. So might want to try something like this in a block class.

$model = Mage::getModel('log/visitor_online');
$select = $model->getCollection()->getSelect();
/* @var $select Varien_Db_Select */
$select->reset(Varien_Db_Select::COLUMNS);
$select->columns(
    new Zend_Db_Expr(
        sprintf('count(%s)', $model->getIdFieldName())
    )
);
echo $select->query()->fetchColumn(0);
Kevin Schroeder
  • 1,296
  • 11
  • 23
0

Use this code to count like on per product place like button on product page , place this code in view.phtml

<?php 
if (!is_dir('clickcounter')) {
                        @mkdir('clickcounter', 0777,true);
                    }       
                    $filename=$_product->getSku().'.txt';               
                    $dir='clickcounter' ;               

                    if(!file_exists($dir.'/'.$filename)){
                        file_put_contents($dir.'/'.$filename, '0');
                    }
                    if(isset($_GET['click']) == 'yes'){
                        file_put_contents($dir.'/'.$filename, ((int) file_get_contents($dir.'/'.$filename)) + 1);
header('Location: ' . $_SERVER['SCRIPT_NAME']);

?>

///// Ajax Update ///

            function myAjax() {                 
                 jQuery.ajax({
                 type: "POST",
                 url: '?click=yes',
                 data:{action:'call_this'},
                 cache: false,
                 success: function (html) {
                   //location.reload(true);
                   jQuery(".favourite-img").replaceWith(jQuery('.favourite-img', jQuery(html)));
                   jQuery('#likeme').addClass('disabled');

                 }

             });
         }

    </script>

//// HTML Code ///

<a id="likeme" class="disabled" href="javascript:void(0)" >
                     <div class="favourite-product">
                    <div class="favourite-img"><?php echo file_get_contents($dir.'/'.$filename); ?></div>
                     </div>
                     </a>
Ramesh
  • 192
  • 1
0

The main table that you can use log_visitor So, here is the code:

$totalUser = Mage::getSingleton('core/resource')->getConnection('core_write');
$queryTotal=$totalUser->query("SELECT * FROM log_visitor ORDER BY visitor_id DESC LIMIT 1 ");
// the result will give you maximum visitor_id
Oğuz Çelikdemir
  • 4,990
  • 4
  • 30
  • 56