Magento customer and order tracking -
i new in magento. when hit site tracking parameters: www.yoursite.com?utm_source=abc&utm_medium=def&utm_content=ghi&utm_campaign=jkl&utm_term=mno
, parameters saved session , cookie. when customer signes up, tracking string saved in customer_entity_varchar
table attribute customer_campaign_params
. when customer places order, tracking string saved in sales_flat_order
table in order_campaign_params
column. tracking string looks that
utm_source=abc|utm_medium=|utm_content=|utm_campaign=|utm_term=
but saves data string in 1 field want save data separately in 5 different fields separately. tried unable that. please see code
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 $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 $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 $key => $value) { $campaignstring .= $key . '=' . $value . '|'; } // set data 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-install-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-upgrade-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();
i made 5 fields in sales_flat_order table unable save data individually each in different fields. please see above code , solve problem possible. thanks
try saving each variable own session key
class robm_tracking_model_observer { public function setcampaignparamstosessionandcookie() { ...... $campaignparams = mage::helper("robm_tracking")->getcampaignparamsarray(); // set data customer session $session = mage::getsingleton('customer/session'); $session->setdata($campaignparams);
to retrieve
$session->getutmsource(); $session->getutmmedium(); ...
or
$session->getdata('utm_source');
Comments
Post a Comment