title: Check UTM tags in magento url
body: <p>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:</p>
  
  <pre><code>        &lt;controller_action_predispatch&gt;
              &lt;observers&gt;
                  &lt;refersource&gt;
                      &lt;type&gt;singleton&lt;/type&gt;
                      &lt;class&gt;refersource/observer&lt;/class&gt;
                      &lt;method&gt;controllerActionPredispatch&lt;/method&gt;
                  &lt;/refersource&gt;
              &lt;/observers&gt;
          &lt;/controller_action_predispatch&gt;
  </code></pre>
  
  <p>In this observer i'll check if utm tag is present in url , if so i save tags and referr url to customer session.</p>
  
  <pre><code>protected $_allowed_tags = array('utm_source','utm_medium','utm_term','utm_content','utm_campaign','refer_url');
  
  public function controllerActionPredispatch($observer)
  {
  
      $params = Mage::app()-&gt;getRequest()-&gt;getParams();
      $reffer_url = Mage::helper('core/http')-&gt;getHttpReferer();
  
      if($reffer_url &amp;&amp; strstr($reffer_url,'mysite') === false){  
          $utm_exist = false;
          if ((isset($params['utm_source']) &amp;&amp; $params['utm_source']) ||
              (isset($params['utm_medium']) &amp;&amp; $params['utm_medium']) ||
              (isset($params['utm_content']) &amp;&amp; $params['utm_content']) ||
              (isset($params['utm_campaign']) &amp;&amp; $params['utm_campaign'])
          ) {
              $utm_exist = true;
          }
  
          if($utm_exist){
              $params['refer_url'] = $reffer_url;
              Mage::getSingleton('customer/session')-&gt;setReferrerData(serialize($params));
          }           
      }
  }
  </code></pre>
  
  <p>The next observer check if order was placed and save data to order.</p>
  
  <pre><code>       &lt;sales_order_place_after&gt;
              &lt;observers&gt;
                  &lt;refersource&gt;
                      &lt;type&gt;singleton&lt;/type&gt;
                      &lt;class&gt;refersource/observer&lt;/class&gt;
                      &lt;method&gt;salesOrderPlaceAfter&lt;/method&gt;
                  &lt;/refersource&gt;
              &lt;/observers&gt;
          &lt;/sales_order_place_after&gt;
  
  public function salesOrderPlaceAfter($observer)
  {
      $referrer_data = unserialize(Mage::getSingleton('customer/session')-&gt;getReferrerData());
  
      if($referrer_data){
          $order = $observer-&gt;getEvent()-&gt;getOrder();
          if($referrer_data &amp;&amp; is_array($referrer_data)){
              foreach($referrer_data as $tag=&gt;$value){
                  if(in_array($tag,$this-&gt;_allowed_tags)){
                      $order-&gt;setData($tag,$value);
                  }
              }
              $order-&gt;save();
          }
          Mage::getSingleton('customer/session')-&gt;unsReferrerData();
      }
  }
  </code></pre>
  
  <p>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. </p>
  
  <p>can anybody explain why this is happening? thank you!</p>