Home » Magento

Magento: Track Visitor’s Information

18 April 2011 Share/Bookmark

Here, I will show you how to track visitor’s data information in Magento. By visitor’s information, I mean information like remote address, http host, user agent (browser), referer url, first visit date time, last visit date time, etc.

The following code fetches the vistor data:-

$visitorData = Mage::getSingleton('core/session')->getVisitorData();

// printing visitor information data
echo "<pre>"; print_r($visitorData); echo "</pre>";

You will get an array of visitor information data like the following one:-

Array
(
    [] => 
    [server_addr] => 167772437
    [remote_addr] => 167772437
    [http_secure] => 
    [http_host] => 127.0.0.1
    [http_user_agent] => Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/534.10 (KHTML, like Gecko) Chrome/8.0.552.237 Safari/534.10
    [http_accept_language] => en-US,en;q=0.8
    [http_accept_charset] => ISO-8859-1,utf-8;q=0.7,*;q=0.3
    [request_uri] => /magento/index.php/catalog/category/view/id/22
    [session_id] => 13qm5u80238vb15lupqcac97r5
    [http_referer] => http://127.0.0.1/magento/
    [first_visit_at] => 2011-01-17 11:42:23
    [is_new_visitor] => 
    [last_visit_at] => 2011-01-17 11:58:38
    [visitor_id] => 41
    [last_url_id] => 139
)

In the above array, the server_addr and remote_addr are in different form than usual. The (IPv4) Internet Protocol dotted address has been converted into a proper address using ip2long PHP function. You can get the same kind of value from the following code:-

// user's ip address (visitor's ip address)
$remoteAddr = Mage::helper('core/http')->getRemoteAddr(true);

// server's ip address (where the current script is)
$serverAddr = Mage::helper('core/http')->getServerAddr(true);

Hope this helps. Thanks.

From Mukesh Chapagain's Blog, post Magento: Track Visitor’s Information

email

php magento mukesh chapagain

Get New Post by Email
RSS Feed Subscribe RSS Feed
  • http://www.best-hostings.in/ Best web hosting company

    This is really great information and now it is very easy to track visitors information.

  • http://www.mactonweb.com web development bangalore

    Nice information, thanks for sharing on how to track visitors.

  • Luke

    is there a way of getting the user-agent to show up in the admin section of Magento so it is recorded on an order (just like the IP address is now) and on user registration or last visit in Customers in the Magento admin?

    It would be a very useful feature for tech support staff to have these details on hand without having to bother the customer who is usually going to be already frustrated and more often than not quite computer illiterate.

  • lariese

    if you could guide me on how to implement this kind of thing on Magento it would be much appreciated.