Vitali
Vitali

Reputation: 549

Magento assign visitor_id in custom module

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)

Upvotes: 0

Views: 362

Answers (1)

Cameron Shaw
Cameron Shaw

Reputation: 142

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);
    }
}

Upvotes: 2

Related Questions