0

I'm new to Magento (I'm using 1.9 version), in custom module in observer I want to have visitor_id of all the users, however it assigns only after some actions on a page. So if you were redirected from some page to Magento web store then Observer will catch this action, but will not find visitor_id, because it's not assigned yet. I tried to solve the problem with the next code

config.xml:

        <controller_action_predispatch>
            <observers>
                <test_model_observer>
                    <type>singleton</type>
                    <class>Test_Model_Observer</class>
                    <method>setVisitorData</method>
                </test_model_observer>
            </observers>
        </controller_action_predispatch>

Observer.php

public function setVisitorData($observer)
{
    $session = Mage::getSingleton("core/session");
    $visitor_id = $session['visitor_data']['visitor_id'];
    $session_id = $session->getSessionId();
    if (!empty($visitor_id)) {
        Mage::getSingleton('log/visitor')->setId($visitor_id);
        Mage::getSingleton('log/visitor')->setSessionId($session_id);
    } else{
        $log_visitor = Mage::getSingleton('log/visitor')->initServerData()->setFirstVisitAt(now())->setIsNewVisitor(true)->setLastVisitAt(now())->save();
        $visitor_id = $log_visitor->getId();
        $session_id = $log_visitor->getSessionId();
    }
    if (!isset($session['visitor_data'])) {
        $session->setData('visitor_id', $visitor_id);
        $session->setData('session_id', $session_id);
    }
}

(original answer here)

It works when I surfing the page, but when only I go to admin panel it breaks trying to create new visitors duplicating visitor_ids. Any idea how to solve this problem? Won't it break somethig else? (I mean calling the method before it was called by Magento itself is not a best practice in my opinion)

Vitali
  • 549
  • 6
  • 16

1 Answers1

2

You can add a conditional check to see if the active page is an admin page, the best way would be to add this in a helper in your extension, the file should look as below with a filepath of {Namespace}/{Modulename}/Helper/Isadmin.php

class {{Namespace}}_{{Modulename}}_Helper_Isadmin extends Mage_Core_Helper_Abstract
    {
        public function isAdmin()
        {
            return (Mage::app()->getStore()->isAdmin() || Mage::getDesign()->getArea() == 'adminhtml') ? 
               true : 
               false ;
        }
    }

The second check which looks at the design area will cover pages such as your magento connect package downloader

Then in your model, add the following line to the beggining of your function

public function setVisitorData($observer)
{
    if(Mage::helper('modulename/isadmin')->isAdmin(){
        return;
    }
    $session = Mage::getSingleton("core/session");
    $visitor_id = $session['visitor_data']['visitor_id'];
    $session_id = $session->getSessionId();
    if (!empty($visitor_id)) {
        Mage::getSingleton('log/visitor')->setId($visitor_id);
        Mage::getSingleton('log/visitor')->setSessionId($session_id);
    } else{
        $log_visitor = Mage::getSingleton('log/visitor')->initServerData()->setFirstVisitAt(now())->setIsNewVisitor(true)->setLastVisitAt(now())->save();
        $visitor_id = $log_visitor->getId();
        $session_id = $log_visitor->getSessionId();
    }
    if (!isset($session['visitor_data'])) {
        $session->setData('visitor_id', $visitor_id);
        $session->setData('session_id', $session_id);
    }
}
Cameron Shaw
  • 142
  • 11
  • Thanks for the answer! It worked for me! Couldn't it break somewhere else, not only in an admin page? – Vitali Jul 27 '17 at 12:06
  • Im not sure as i have not properly understood your issue as a whole and what issues could arise from this, i only know a fix for the mentioned issue regarding the admin pages. I am sure that the visitor info gets set already when the event 'controller_action_predispatch' is triggered, so is your issue that this event is not triggered when a user comes from the other website directly to this one? – Cameron Shaw Jul 27 '17 at 12:23
  • seems that it is triggered, but only after another Observer functions called, so I don't receive on first page access any information from Observer when try to get visitor_id, it appears only on next clicks. – Vitali Jul 27 '17 at 12:35
  • the next thing to try is to see when this visitor information is set, and check why when you first land on the page the visitor information is not set. can you create a small log in your observer which logs 'test' every time it executes, and check if it is executing when you come on the first page. If its not executing then this event is not being triggered which will be the problem, if this event is still being triggered then there may be something else happening the first time they visit the page compared to the second time – Cameron Shaw Jul 27 '17 at 12:51
  • I tried to do it, when I first time land on the page cookies appear, but I still cant access the visitor id, only on second click (if I do test it without the function I posted) – Vitali Jul 27 '17 at 14:33
  • I'm not sure what to advise unless i had access to your codebase, it may be best to get another developer to take a look at the problem, or see if its a common issue among the community – Cameron Shaw Aug 01 '17 at 16:39
  • With the fixed function it works like a charm (I didn't find any problem still), so everything is fine. Thank you! – Vitali Aug 02 '17 at 07:50