1
votes

I am trying to track product category from magento to GA, (under Conversions>Ecommerce>overview), but with magento default core GA.php file dosent including this part: /app/code/core/Mage/GoogleAnalytics/Block/Ga.php

foreach ($order->getAllVisibleItems() as $item) {
                $result[] = sprintf("_gaq.push(['_addItem', '%s', '%s', '%s', '%s', '%s', '%s']);",
                    $order->getIncrementId(),
                    $this->jsQuoteEscape($item->getSku()), $this->jsQuoteEscape($item->getName()),
                    null, // there is no "category" defined for the order item
                    $item->getBasePrice(), $item->getQtyOrdered()
                );
            }

*see, they commend out category field and put null right there.

I am looking for a solution that do not modify the magento in the /core, but solve the problem in /local.

BTW, I am using magento 1.7

hopefully someone could help me out, many thx :D

2

2 Answers

1
votes

I don't know much about Magento but I believe all you have to do is copy that whole file from core over to your /local folder and change it to include the category. Magento will always look for a file in /local before going to core, but the path have to match. So the file should live in:

/app/code/local/Mage/GoogleAnalytics/Block/Ga.php

Another problem you'll have is that Magento products can have several categories. so you probably want to grab just the first one and pass to GA.

So here's what the file should look like:

$_category = null;

$categoryIds = $_product->getCategoryIds();
if(count($categoryIds)) {
    $firstCategoryId = $categoryIds[0];
    $_category = Mage::getModel('catalog/category')->load($firstCategoryId);
}

foreach ($order->getAllVisibleItems() as $item) {
    $result[] = sprintf("_gaq.push(['_addItem', '%s', '%s', '%s', '%s', '%s', '%s']);",
        $order->getIncrementId(),
        $this->jsQuoteEscape($item->getSku()), $this->jsQuoteEscape($item->getName()),
        $_category,
        $item->getBasePrice(), $item->getQtyOrdered()
    );
}

Based in

0
votes

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.