I had the exactly same situation The proper way to do this job the to extend the functionality of block class. I had created the module for that have a look for that if you want to add more items in Google Analytics for reporting purpose:
I had created the module MyGoogleAnalytics under the directory Renegade:
Have a look on file:
Renegade_MyGoogleAnalytics.xml (module file)
<?xml version="1.0"?>
<config>
<modules>
<Renegade_MyGoogleAnalytics>
<active>true</active>
<codePool>local</codePool>
<version>0.1.0</version>
</Renegade_MyGoogleAnalytics>
</modules>
</config>
block file Ga.php in (app\code\local\Renegade\MyGoogleAnalytics\Block\GoogleAnalytics)
<?php
class Renegade_MyGoogleAnalytics_Block_GoogleAnalytics_Ga extends Mage_GoogleAnalytics_Block_Ga
{
protected function _getOrdersTrackingCode()
{
$orderIds = $this->getOrderIds();
if (empty($orderIds) || !is_array($orderIds)) {
return;
}
$collection = Mage::getResourceModel('sales/order_collection')
->addFieldToFilter('entity_id', array('in' => $orderIds))
;
$result = array();
foreach ($collection as $order) {
if ($order->getIsVirtual()) {
$address = $order->getBillingAddress();
} else {
$address = $order->getShippingAddress();
}
$result[] = sprintf("_gaq.push(['_addTrans', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s']);",
$order->getIncrementId(),
$this->jsQuoteEscape(Mage::app()->getStore()->getFrontendName()),
$order->getBaseGrandTotal(),
$order->getBaseTaxAmount(),
$order->getBaseShippingAmount(),
$this->jsQuoteEscape(Mage::helper('core')->escapeHtml($address->getCity())),
$this->jsQuoteEscape(Mage::helper('core')->escapeHtml($address->getRegion())),
$this->jsQuoteEscape(Mage::helper('core')->escapeHtml($address->getCountry()))
);
$manufacturerModel = Mage::getModel('manufacturers/manufacturers');
$productModel = Mage::getModel('catalog/product');
$categoryModel = Mage::getModel('catalog/category');
foreach ($order->getAllVisibleItems() as $item) {
// add category name and brand name
$categoryName = null;
$brandName = null;
$_product = $productModel->load($item->getProductId());
$categoryIds = $_product->getCategoryIds();
if(count($categoryIds)>0) {
$firstCategoryId = $categoryIds[0];
$categoryName = $categoryModel->load($firstCategoryId)->getName();
}
// brand
$productManufacturerId = $manufacturerModel->getProductManufacturer($item->getProductId());
$manufacturerName = $manufacturerModel->load($productManufacturerId)->getmName();
if(!empty($manufacturerName))
$brandName = $manufacturerName;
$result[] = sprintf("_gaq.push(['_addItem', '%s', '%s', '%s', '%s', '%s', '%s', '%s']);",
$order->getIncrementId(),
$this->jsQuoteEscape($item->getSku()),
$this->jsQuoteEscape($item->getName()),
$this->jsQuoteEscape($categoryName),
$this->jsQuoteEscape($brandName),
$item->getBasePrice(),
$item->getQtyOrdered()
);
}
$result[] = "_gaq.push(['_trackTrans']);";
}
return implode("\n", $result);
}
}
configuration file in app\code\local\Renegade\MyGoogleAnalytics\etc
config.xml
<?xml version="1.0"?>
<config>
<modules>
<Renegade_MyGoogleAnalytics>
<version>0.1.0</version>
</Renegade_MyGoogleAnalytics>
</modules>
<global>
<helpers>
<mygoogleanalytics>
<class>Renegade_MyGoogleAnalytics_Helper</class>
</mygoogleanalytics>
</helpers>
<blocks>
<mygoogleanalytics>
<class>Renegade_MyGoogleAnalytics_Block</class>
</mygoogleanalytics>
<googleanalytics>
<rewrite>
<ga>Renegade_MyGoogleAnalytics_Block_GoogleAnalytics_Ga</ga>
</rewrite>
</googleanalytics>
</blocks>
</global>
</config>
Compare _getOrdersTrackingCode with original file function you will find the and understand the difference.