douju7503 2014-10-24 19:54 采纳率: 0%
浏览 58

检查magento网址中的UTM标记

i write custom code for magento to treck utm tags in url, and if order was placed i need to save it to database with referrer url. That is my code:

        <controller_action_predispatch>
            <observers>
                <refersource>
                    <type>singleton</type>
                    <class>refersource/observer</class>
                    <method>controllerActionPredispatch</method>
                </refersource>
            </observers>
        </controller_action_predispatch>

In this observer i'll check if utm tag is present in url , if so i save tags and referr url to customer session.

protected $_allowed_tags = array('utm_source','utm_medium','utm_term','utm_content','utm_campaign','refer_url');

public function controllerActionPredispatch($observer)
{

    $params = Mage::app()->getRequest()->getParams();
    $reffer_url = Mage::helper('core/http')->getHttpReferer();

    if($reffer_url && strstr($reffer_url,'mysite') === false){  
        $utm_exist = false;
        if ((isset($params['utm_source']) && $params['utm_source']) ||
            (isset($params['utm_medium']) && $params['utm_medium']) ||
            (isset($params['utm_content']) && $params['utm_content']) ||
            (isset($params['utm_campaign']) && $params['utm_campaign'])
        ) {
            $utm_exist = true;
        }

        if($utm_exist){
            $params['refer_url'] = $reffer_url;
            Mage::getSingleton('customer/session')->setReferrerData(serialize($params));
        }           
    }
}

The next observer check if order was placed and save data to order.

       <sales_order_place_after>
            <observers>
                <refersource>
                    <type>singleton</type>
                    <class>refersource/observer</class>
                    <method>salesOrderPlaceAfter</method>
                </refersource>
            </observers>
        </sales_order_place_after>

public function salesOrderPlaceAfter($observer)
{
    $referrer_data = unserialize(Mage::getSingleton('customer/session')->getReferrerData());

    if($referrer_data){
        $order = $observer->getEvent()->getOrder();
        if($referrer_data && is_array($referrer_data)){
            foreach($referrer_data as $tag=>$value){
                if(in_array($tag,$this->_allowed_tags)){
                    $order->setData($tag,$value);
                }
            }
            $order->save();
        }
        Mage::getSingleton('customer/session')->unsReferrerData();
    }
}

Needed filed already created in sales_flat_order. All works fine ,but have some problems. When we see in google analytics we see example about 50 orders which did with this utm tags and see referrer urls. But in database we see only 2-4 orders with utm tags information.

can anybody explain why this is happening? thank you!

  • 写回答

1条回答 默认 最新

  • dongtan8122 2014-10-24 20:09
    关注

    HI we are using this extension to store information :

    http://www.magentocommerce.com/magento-connect/acquisition-source-grabber.html

    if you want you can edit extension. we did some modifications to store Operating System , Browser its easy.. this extension work for you.

    评论

报告相同问题?

悬赏问题

  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私