Online Visitors Magneto

You might have seen in Magento we have a section for seeing the online visitors at a given time.

I am describing here the method for getting the number of online visitors, for reference see the my webstore Decrypt Web Store, I am showing the online visitors in the left column.

<?php
//this code can be written directly in the template file
            $visitor_count = Mage::getModel('log/visitor_online')
            ->prepare()
            ->getCollection()->count();
		
        if(!empty($visitor_count) && $visitor_count > 0)
        {
        	$cnt =  $visitor_count;       		
                echo 'Visitors online :'.$cnt;
        }  
?>

Here is another way for doing this

<?php
//this code can be written directly in the template file
            $visitor_collection = Mage::getResourceModel('log/visitor_collection')->useOnlineFilter();
	    $visitor_count = count($visitor_collection);	
        if(!empty($visitor_count) && $visitor_count > 0)
        {
        	$cnt =  $visitor_count;       		
                echo 'Visitors online :'.$cnt;
        }  

?>

Hope this is helpful.

8 thoughts on “Online Visitors Magneto

  1. nice one…! It will be better to use this method rather than using the third party extension extension…! 🙂

  2. thanks a bout this article. And can you write an article about total of access website, amount of access in current day and current month?
    Thanks a lots.

    1. @taly – In Magento site, when you login in adminend, you can see number of times the product viewed, I have used this in my store DecryptWeb Store, you need to modify the code for filtering as per the date. Also you can use Google Analytics for tracking your site’s user access, it will be much useful.

  3. Hi, can u please give more instruction which file at what folder need to be edited?

    And which part should be manipulated to make: if there’s no visitor, the visitor count show 5 instead of 0

    Thanks

    1. @Budi – There is no need to modify any of the default file, you can make a new block file and simply write above mentioned code or can write in any of the existing block file. The visitor counts depends on the customer logs generated in Magento, hope this helps you

    1. It depends on the visitors logs generated by Magento in a given time.

      I derived at this code from the default Magento code for Customers->Online Customers section found in the default Magento admin end.

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top