0

I have two issues with Universal Analytics tracking product prices:

  1. When a Configurable Product without any custom options is sold, Google Universal Analytics tracks the sale of the associated Simple Product, but the price of the Simple Product is missing. Therefore I cannot report on the total sales of these items.

  2. When a Configurable Product with a custom option is sold, Google Universal Analytics tracks the sale of the both the Configurable product with correct price AND the associated Simple Product with price missing. So I can report on the correct total sales amount of these items, but not the quantities.

Here is the current code in our checkout that Google support provided for Universal Analytics:

<?php
$order = Mage::getModel('sales/order')->load(Mage::getSingleton('checkout/session')->getLastOrderId()); 
$subtotal = $order->getSubtotal();
$order_id = $order->getIncrementId(); //the id of the order
//$order->getIncrementId();//the increment id of the order
$gtotal = $order->getBaseGrandTotal(); //grand total of the order
$tax = $order->getBaseTaxAmount(); //tax amount of the order
$shipping = $order->getBaseShippingAmount(); //shipping amount of the order
$address = $order->getBillingAddress()->getData();
$city = $address['city'];
$state = $address['region'];
$country = $address['country_id'];
?>
<script type="text/javascript">
ga('require', 'ecommerce', 'ecommerce.js');

ga('ecommerce:addTransaction', {
  'id': '<?php echo $order_id; ?>',                     // Transaction ID. Required.
  'affiliation': '',   // Affiliation or store name.
  'revenue': '<?php echo $gtotal;?>',               // Grand Total.
  'shipping': '<?php echo $shipping;?>',  // Shipping.
  'tax': '<?php echo $tax;?>' // Tax.
});

<?php 
$items = $order->getAllItems();
$itemcount=count($items);
$name=array();
$unitPrice=array();
$sku=array();
$ids=array();
$qty=array();
foreach ($items as $itemId => $item)
{?>

ga('ecommerce:addItem', {
  'id': '<?php echo $order_id; ?>',                     // Transaction ID. Required.
  'name': '<?php echo $item->getName(); ?>',    // Product name. Required.
  'sku': '<?php echo $item->getSku(); ?>',                 // SKU code.
  'category': '',         // Category or variation.
  'price': '<?php echo $item->getBasePrice(); ?>',                 // Unit price.
  'quantity': '<?php echo $item->getQtyOrdered(); ?>'                   // Quantity.
});
<?php 
}
?> 

ga('ecommerce:send');

</script>

I wish to only get the Simple Product details and correct price, and not Configurable details or price. So I will need a check to see if it's a configurable product to then only get the associated Simple Product details.

I don't know whether it should be parent and child, of test for configurable first. It will be a change to the code below but I don't have the knowledge to figure this out!

Many thanks in advance :)

<?php 
$items = $order->getAllItems();
$itemcount=count($items);
$name=array();
$unitPrice=array();
$sku=array();
$ids=array();
$qty=array();
foreach ($items as $itemId => $item)
{?>

1 Answer 1

0

Try this code, Skip if the item itself not parent :

foreach ($items as $itemId => $item)
    {
        if($item->getParentItemId()){
            continue;           
        }       
    ?>

    ga('ecommerce:addItem', {
      'id': '<?php echo $order_id; ?>',                     // Transaction ID. Required.
      'name': '<?php echo $item->getName(); ?>',    // Product name. Required.
      'sku': '<?php echo $item->getSku(); ?>',                 // SKU code.
      'category': '',         // Category or variation.
      'price': '<?php echo $item->getBasePrice(); ?>',                 // Unit price.
      'quantity': '<?php echo $item->getQtyOrdered(); ?>'                   // Quantity.
    });
    <?php 
}
?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.