Home » Magento18 April 2011

Magento: Track Visitor’s (User / Customer) Information

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.

Magento

Get New Post by Email

Find me on

FacebookTwitterGoogle+LinkedInRSS Feed
  • This is really great information and now it is very easy to track visitors information.

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

  • Vaibhav Shahu

    Can we track logged admin ip address in magento community version

  • Muhammad Saeed Khan

    Hello
    Using this function on category page with url-key of category returns NULL, but when hit the category with catalog/category/view/id, the result array returned.
    Mage::getSingleton(‘core/session’)->getVisitorData()

    What could be the issue? Please help me to figure out this issue.