简体   繁体   中英

Magento: Google Analytics Ecommerce tracking

I am using Magento 1.7.0.2 and in the past I have added the following code to my success.phtml page and Google was tracking my conversions:

<?php 
//-------------------------------------------
// START ADWORDS CONVERSION VALUE TRACKING CODE
//-------------------------------------------
$order_details = Mage::getModel('sales/order')-    
>loadByIncrementId(Mage::getSingleton('checkout/session')->getLastRealOrderId());
 $adwords_saleamt = $order_details->subtotal; 
 ?>

<!-- Google Code for Thank you page Conversion Page -->
<script type="text/javascript">
/* <![CDATA[ */
var google_conversion_id = 00000000;
var google_conversion_language = "en";
var google_conversion_format = "2";
var google_conversion_color = "ffffff";
var google_conversion_label = "xxxxxxxxxxx";
var google_conversion_value = 1.00;
var google_conversion_currency = "GBP";
var google_remarketing_only = false;
/* ]]> */
</script>
<script type="text/javascript" src="//www.googleadservices.com/pagead/conversion.js">
</script>
<noscript>
<div style="display:inline;">
<img height="1" width="1" style="border-style:none;" alt=""    
src="//www.googleadservices.com/pagead/conversion/00000000000/?    
value=1.00&amp;currency_code=GBP&amp;label=xxxxxxxxxxx&amp;guid=ON&amp;script=0"/>
</div>
</noscript>

Now after I have switched to Universal Analytics, Google is no longer tracking anything. I have contacted Google and they could not explain clearly what I need to do.

My understanding is that now I need to add the following to my success.phtml page.

Before:

  <?php if ($this->getOrderId()):?>
  <?php if ($this->getCanViewOrder()) :?>
   ...
   <?php endif;?>
   <?php endif;?>

After:

 <?php if ($this->getOrderId()):?>
 <?php if ($this->getCanViewOrder()) :?>
  ...
 <?php endif;?>
<?php
// Transaction Data
$orderID = $this->getOrderId();
$order = Mage::getModel('sales/order')->loadByIncrementId($orderID);
$orderTotal = $order->getGrandTotal();
// Affiliation, shipping and tax
$trans = array('id' => $orderID,
         'revenue' => $orderTotal);

 // List of Items Purchased
 $items = array(); 
 foreach ($order->getAllItems() as $item){
 // SKU and category
 $items[] = array('name' => $item->getName(),
            'price' => $item->getPrice(),
            'quantity' => $item->getQtyOrdered());
 }


 // Function to return the JavaScript representation of a TransactionData object.
 function getTransactionJs(&$trans) {
return <<<HTML
ga('ecommerce:addTransaction', {
'id': '{$trans['id']}',
 // if affiliation, shipping or tax is added
 'revenue': '{$trans['revenue']}'
 });
 HTML;
  }

  // Function to return the JavaScript representation of an ItemData object.
  function getItemJs(&$transId, &$item) {
  return <<<HTML
 ga('ecommerce:addItem', {
  'id': '$transId',
  // if SKU or category is added
  'name': '{$item['name']}',
   'price': '{$item['price']}',
   'quantity': '{$item['quantity']}'
  });
 HTML;
  }
 ?>

  <script>
 ga('require', 'ecommerce', 'ecommerce.js');

<?php
echo getTransactionJs($trans);

foreach ($items as &$item) {
echo getItemJs($trans['id'], $item);
}
?>

ga('ecommerce:send');
</script>
<?php endif;?>

Can someone please confirm that this is correct?

Use this:

    <?php // GET SOME VALUES FOR TRACKING PIXELS
    $_customerId = Mage::getSingleton('customer/session')->getCustomerId(); 
    $lastOrderId = Mage::getSingleton('checkout/session')->getLastOrderId(); 
    $order = Mage::getSingleton('sales/order'); 
    $order->load($lastOrderId); 
    $_totalData =$order->getData(); 
    $_sub = $_totalData['subtotal'];
    $_sub = round($_sub,2); 
?>
<!-- Google Code for Website XY Conversion Page -->
<script type="text/javascript">
/* <![CDATA[ */
var google_conversion_id = XXXXXXXXXX;
var google_conversion_language = "de";
var google_conversion_format = "2";
var google_conversion_color = "ffffff";
var google_conversion_label = "YYYYYYYYYYYYYYYY";
var google_conversion_value = <?php echo $_sub ?>;
var google_conversion_currency = "EUR";
var google_remarketing_only = false;
/* ]]> */
</script>
<script type="text/javascript" src="//www.googleadservices.com/pagead/conversion.js"></script>
<noscript>
<div style="display:inline;">
<img height="1" width="1" style="border-style:none;" alt="" src="//www.googleadservices.com/pagead/conversion/XXXXXXXXXX/?value=<?php echo $_sub ?>&amp;currency_code=EUR&amp;label=YYYYYYYYYYYYYYYY&amp;guid=ON&amp;script=0"/>
</div>
</noscript>

I must admit that I am bit confusion with your above post, the first piece of code you mentioned is google adwords conversation tracking code which you don't need to change at all even if you are switching from traditional to universal analytic tracking.

And second piece of code you mentioned is universal analytic conversation tracking code which will be taken care by universal tracking module and you don't need to add it manually to your success page. If you do need to add then you might not be using good module and may want to try magento connect module like http://www.magentocommerce.com/magento-connect/google-analytics-enhanced-ecommerce-tracking.html

Hope the above makes sense, feel free to ask in case of any further confusion. Thanks

Cheers S

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM