1

I want to get visitor data when any visitor visit magento website. In visitor detail, I want to get IP address, browser name , Operating System Name , First Visit Time, & Last Visit time. How is it possible? This Feature must working on all page of magento website.

Please Help Me.

Thanks in advance. :-)

  • This works. Check customer_visitor table in admin panel. But it doesnt have all the data you want. That you need to code differently. – Kingshuk Deb Apr 5 '17 at 13:44
  • Try this method getVisitorData() to get Data from session object. Check what you can get. If not... try to get data from request header like http_user_agent to get the browser, remote_addr to get the IP address etc etc. – Kingshuk Deb Apr 5 '17 at 13:52
  • Magento\Customer\Model\Visitor is the class where Magento is setting visitors information by using setVisitorData inside initByRequest() function. – Kingshuk Deb Apr 5 '17 at 13:54
  • 1
    Please put code here if you have. – Rohan Hapani Apr 6 '17 at 7:12
0

Ok. So try this. But this wont save your data in customer_visitor table at least not the fields i added.

public function initByRequest($observer) //Magento\Customer\Model\Visitor - PATH
    {
        if ($this->skipRequestLogging || $this->isModuleIgnored($observer)) {
            return $this;
        }
        if ($this->session->getVisitorData()) {
            $this->setData($this->session->getVisitorData());
        }

        $this->setLastVisitAt((new \DateTime())->format(\Magento\Framework\Stdlib\DateTime::DATETIME_PHP_FORMAT));
        $this->setHttpUserAgent($_SERVER['HTTP_USER_AGENT']); // setting user browser to object
        $this->setUserRemoteAddr($_SERVER['REMOTE_ADDR']); //setting user ip to object

        if (!$this->getId()) {
            $this->setSessionId($this->session->getSessionId());
            $this->save();
            $this->_eventManager->dispatch('visitor_init', ['visitor' => $this]);
            $this->session->setVisitorData($this->getData());
        }
        return $this;
    }

Now remove your cookies from browser and try to get the value like this

$this->session->getVisitorData(); // Or where you want to get the data by initializing session object and get it.

Now you will get something like this Array ( [last_visit_at] => 2017-04-06 07:35:44 [http_user_agent] => Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36 [user_remote_addr] => 127.0.0.1 [session_id] => 9c2uc36v01is5mn30q890o1ih2 [visitor_id] => 22 )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.