1

I'm trying to work out how to detect when a user has left a store view and landed on another.

Basically I have website A with a domain of magento.com.localhost and website B with a domain of magento.co.uk.localhost.

On each website I have a store switcher. So if I'm on website A I have a link that looks like magento.co.uk.localhost/category?___store=gb&___from_store=us and when I click it I go to a different store view, but want to be able to track when this happens.

I figured the easiest way to do this is to use Mage::app()->getRequest() to get $_GET['from_store'] but this variable doesn't seem to exist due to the nature of the url rewrites.

Is there a Magento specific way to detect when a user has landed on the site from a different store code and how can I access that store code.

Many thanks in advance.

2

You can listen on the controller_action_predispatch event.

in config.xml:

<events>
    <controller_action_predispatch>
        <observers>
            <controllerActionPredispatch>
                <class>Vendor_Module_Model_Observer</class>
                    <method>controllerActionPredispatch</method>
                </controllerActionPredispatch>
            </observers>
        </controller_action_predispatch>
    </events>

in Observer.php:

<?php
class Vendor_Module_Model_Observer
{
    public function controllerActionPredispatch($observer)
    {
        if($switch = Mage::app()->getRequest()->getParam('___from_store')) {
            $storeCode = Mage::app()->getStore()->getCode();
            /* do your tracking here */
        }
    }
}

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.