Magento客户和订单跟踪

时间:2015-06-20 09:26:03

标签: magento magento-1.9 magento-1.9.1

我是magento的新人。 当我使用跟踪参数点击网站时:www.yoursite.com?utm_source=abc&utm_medium=def&utm_content=ghi&utm_campaign=jkl&utm_term=mno,参数将保存到会话和Cookie中。 当客户签名时,跟踪字符串将作为属性customer_entity_varchar保存在customer_campaign_params表中。当客户下订单时,跟踪字符串将保存在sales_flat_order列的order_campaign_params表中。 然后跟踪字符串看起来像

utm_source=abc|utm_medium=|utm_content=|utm_campaign=|utm_term=

但它将数据保存为一个字段中的字符串,但我想分别将数据保存在5个不同的字段中。 我试图这样做,但无法做到这一点。 请参阅代码

Data.php

class Robm_Tracking_Helper_Data extends Mage_Core_Helper_Abstract
{
    const CUSTOMER_CAMPAIGN_PARAMS = 'customer_campaign_params';
    const TRACKING_COOKIE_LIFETIME = 15552000;

    public function getCampaignParamsArray()
    {
        $request = Mage::app()->getRequest();
        $campaignParams = array(
            'utm_source' => utf8_encode($request->getParam('utm_source')),
            'utm_medium' => utf8_encode($request->getParam('utm_medium')),
            'utm_content' => utf8_encode($request->getParam('utm_content')),
            'utm_campaign' => utf8_encode($request->getParam('utm_campaign')),
            'utm_term' => utf8_encode($request->getParam('utm_term')), 
        );

        return $campaignParams;
    }

    public function getUtmSource()
    {
        $session = Mage::getSingleton('customer/session');
        $campaignString = $session->getData(Robm_Tracking_Helper_Data::CUSTOMER_CAMPAIGN_PARAMS);
        if(is_null($campaignString)) {
            $cookie = Mage::getSingleton('core/cookie');
            $campaignString = $cookie->get(Robm_Tracking_Helper_Data::CUSTOMER_CAMPAIGN_PARAMS);
        }

        $utmSource = '';
        $campaignStringArray = explode("|", $campaignString);
        foreach ($campaignStringArray as $campaignParam) {
            if (!empty($campaignParam) && strpos($campaignParam, "=")) {
                $keyValuePair = explode("=", $campaignParam);
                if ($keyValuePair[0] == 'utm_source') {
                    $utmSource = strtolower($keyValuePair[1]);
                    break;
                }
            }
        }

        return $utmSource;
    }


    public function getUtmSourceSession()
    {
        $session = Mage::getSingleton('customer/session');
        $campaignString = $session->getData(Robm_Tracking_Helper_Data::CUSTOMER_CAMPAIGN_PARAMS);

        $utmSource = '';
        $campaignStringArray = explode("|", $campaignString);
        foreach ($campaignStringArray as $campaignParam) {
            if (!empty($campaignParam) && strpos($campaignParam, "=")) {
                $keyValuePair = explode("=", $campaignParam);
                if ($keyValuePair[0] == 'utm_source') {
                    $utmSource = strtolower($keyValuePair[1]);
                    break;
                }
            }
        }

        return $utmSource;
    }
}

observer.php

class Robm_Tracking_Model_Observer
{
    public function setCampaignParamsToSessionAndCookie()
    {
        $request = Mage::app()->getRequest();

        $utmSource = $request->getParam('utm_source');
        if ($utmSource) {
            $campaignParams = Mage::helper("robm_tracking")->getCampaignParamsArray();


            $campaignString = '';
            foreach ($campaignParams as $key => $value) {
                $campaignString .= $key . '=' . $value . '|';               
            }

            // set data to customer session
            $session = Mage::getSingleton('customer/session');
            $session->setData(Robm_Tracking_Helper_Data::CUSTOMER_CAMPAIGN_PARAMS, $campaignString);

            // set cookie containing data
            $expire = time() + Robm_Tracking_Helper_Data::TRACKING_COOKIE_LIFETIME;
            $cookie = Mage::getSingleton('core/cookie');
            $cookie->set(Robm_Tracking_Helper_Data::CUSTOMER_CAMPAIGN_PARAMS, $campaignString, $expire, '/');
            $__fields = array('utf_source'=> $utmSource);

            $db = Mage::getSingleton('core/resource')->getConnection('core_write');

            try {
                $db->insertOnDuplicate($db->getTableName('robm_tracking'), array('date' => date('Y-m-d'), 'utm_source' => $utmSource, 'clicks' => 1),  array('clicks' => new Zend_Db_Expr('`clicks` +1')));
                $db->setData($__fields)->save()->getId();
            } catch (Exception $e) {
                Mage::logException($e);
            }

            return true;
        }

        return false;
    }


    public function addTrackingToCustomerCreate(Varien_Event_Observer $observer) {
        $cookie = Mage::getSingleton('core/cookie');
        $campaignString = $cookie->get(Robm_Tracking_Helper_Data::CUSTOMER_CAMPAIGN_PARAMS);
        if(is_null($campaignString) || $campaignString == '') {
            $campaignString = 'untracked';
        }

        $customerObject = $observer->getEvent()->getDataObject();
        if($customerObject->isObjectNew()) {
            $customerObject->setCustomerCampaignParams($campaignString);
        }

        return $this;
    }

    public function addTrackingToOrderCreate(Varien_Event_Observer $observer) {
        $cookie = Mage::getSingleton('core/cookie');
        $campaignString = $cookie->get(Robm_Tracking_Helper_Data::CUSTOMER_CAMPAIGN_PARAMS);
        if(is_null($campaignString) || $campaignString == '') {
            $campaignString = 'untracked';
        }

        $orderObject = $observer->getEvent()->getOrder();
        $orderObject->setOrderCampaignParams($campaignString);

        return $this;
    }
}

mysql4安装-0.0.1.php

$installer = $this;
$installer->startSetup();
$installer->run("ALTER TABLE {$this->getTable('sales_flat_order')} ADD `order_campaign_params` VARCHAR(255) NULL;");
$installer->endSetup();

mysql4升级-0.0.1-0.0.2.php

$installer = $this;
$installer->startSetup();
$installer->addAttribute(
    'customer',
    'customer_campaign_params',
    array(
        'type'                      => 'varchar',
        'label'                     => 'Customer Campaign Params',
        'input'                     => 'text',
        'is_used_for_price_rules'   => 0,
        'required'                  => 0,
        'visible'                   => 0,
    )
);
$installer->endSetup();

我已经在sales_flat_order表中创建了5个字段,但无法在不同的字段中单独保存数据。 请参阅上面的代码并尽快解决我的问题。 感谢

1 个答案:

答案 0 :(得分:0)

尝试将每个变量保存为其拥有的会话密钥

class Robm_Tracking_Model_Observer
{
    public function setCampaignParamsToSessionAndCookie()
    {
        ......

            $campaignParams = Mage::helper("robm_tracking")->getCampaignParamsArray();

            // set data to customer session
            $session = Mage::getSingleton('customer/session');
            $session->setData($campaignParams);

要检索

$session->getUtmSource(); 
$session->getUtmMedium();
...

$session->getData('utm_source');

你也可以Create a custom Session Namespace with a Model in Magento