Tuesday, September 17, 2013

Magento add category of products to Google Analytics

Some very useful information I found. Go, under the google analytics website, to Conversions->Google Analytics where you would find the Category option for the products. Under product category, you should see (not set) or something similar if the categories don't show yet. Make sure you go to Admin, Ecommerce Settings and select the yes option for 'Is this a ecommerce store' or something.

In your code, copy app/code/core/Mage/GoogleAnalytics/Block/Ga.php to
app/code/local/Mage/GoogleAnalytics/Block/Ga.php.

Change the appropriate section to this


foreach ($order->getAllVisibleItems() as $item) { // Category for Google Analytics fix $product = Mage::getModel('catalog/product')->loadByAttribute('sku', $item->getSku()); $categoryIds = $product->getCategoryIds(); $categoryName = ''; if (isset($categoryIds[0])){ $category = Mage::getModel('catalog/category')->setStoreId( Mage::app()->getStore()->getId())->load($categoryIds[0]); $categoryName = $category->getName(); } $result[] = sprintf("_gaq.push(['_addItem', '%s', '%s', '%s', '%s', '%s', '%s']);", $order->getIncrementId(), $this->jsQuoteEscape($item->getSku()), $this->jsQuoteEscape($item->getName()), $categoryName, $item->getBasePrice(), $item->getQtyOrdered() ); }  That should be it!

I found a lot of help at http://www.magentocommerce.com/boards/viewthread/291153/ from
tzyganu and a few other comments.

2 comments:

  1. is it ok for magento 1.7.0.2 , i have make it in magento code , but it thogh not catch category in google anlytics code.
    Please suggest me what should to do

    ReplyDelete
  2. Hi, sorry for the late comment, I don't go on my blog too often. You can try it, if it doesn't work for you, you can try something else. Not sure if this fix still works.

    ReplyDelete