Gopesh
Gopesh

Reputation: 3950

Hit counter in magento

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

Upvotes: 5

Views: 3694

Answers (3)

Ramesh
Ramesh

Reputation: 192

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>

Upvotes: 0

Kevin Schroeder
Kevin Schroeder

Reputation: 1295

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

Upvotes: 0

Oğuz &#199;elikdemir
Oğuz &#199;elikdemir

Reputation: 4980

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

Upvotes: 0

Related Questions