Magento Google Analytics Product Category not set

Magento Google Analytics Product Category not set

October 1, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento Google Analytics Product Category not set

Solving Magento Google Analytics Product Category Tracking Issue

Hello developers! Today we’re going to tackle an issue that many of you might be facing when trying to track product category data from your Magento store to Google Analytics (GA). We’ll specifically focus on solving this problem without modifying the core Magento files.

Identifying the Problem in Magento 1.7 Core Files

The default app/code/core/Mage/GoogleAnalytics/Block/Ga.php file in Magento 1.7 does indeed have a comment-out section where the product category data should be included:

foreach ($order->getAllVisibleItems() as $item) {
    // The category field is commented out here
    // $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()
    // );
}

As you can see, Magento doesn’t include a placeholder for the product category by default. Instead, it’s filled with null.

Understanding the Challenge

The challenge here is that while Magento tracks products and their categories internally, this data isn’t passed directly to Google Analytics. The core file simply omits the necessary code for tracking categories.

Solving the Problem Locally

To solve this issue without modifying the core files (which is a best practice), we need to create a custom extension in your app/code/local/Mage directory:

  1. Create a new directory structure: app/code/local/Mage/YourVendorName/GoogleAnalytics/etc/config.xml

  2. In the config.xml file, add the following XML code:

<config>
    <modules>
        <YourVendorName_GoogleAnalytics>
            <version>0.1.0</version>
        </YourVendorName_GoogleAnalytics>
    </modules>
    <global>
        <blocks>
            <googleanalytics>
                <rewrite>
                    <ga>Mage_YourVendorName_Ga</ga>
                </rewrite>
            </googleanalytics>
        </blocks>
    </global>
</config>

Replace YourVendorName with your actual vendor name.

  1. Next, create a custom Ga.php file in the following directory: app/code/local/Mage/YourVendorName/GoogleAnalytics/Block/Ga.php

  2. In the Ga.php file, replace the contents with the following code:

<?php

class Mage_YourVendorName_Ga extends Mage_Core_Block_Template
{
    protected function _toHtml()
    {
        $output = parent::_toHtml();
        $output .= $this->getGoogleAnalyticsHtml();

        return $output;
    }

    public function getGoogleAnalyticsHtml()
    {
        $order = $this->getOrder();

        if (!$order instanceof Mage_Sales_Model_Order) {
            return '';
        }

        // Get a list of all visible items for this order
        $items = $order->getAllVisibleItems();
        $result = array();

        foreach ($items as $item) {
            $categoryId = $item->getProduct()->getCategoryIds()[0]; // Assuming first category

            if (empty($categoryId)) {
                continue; // If there's no category, skip this item
            }

            $category = Mage::getModel('catalog/category')->load($categoryId);

            if (!$category) {
                continue; // If the category can't be loaded, skip this item
            }

            $result[] = sprintf(
                "_gaq.push(['_addItem', '%s', '%s', '%s', '%s', '%s', '%s']);",
                $order->getIncrementId(),
                $this->jsQuoteEscape($item->getSku()),
                $this->jsQuoteEscape($item->getName()),
                $this->jsQuoteEscape($category->getName()), // Include the category name
                $item->getBasePrice(),
                $item->getQtyOrdered()
            );
        }

        return implode("\n", $result);
    }
}

This custom Ga.php file extends the core file and adds the missing category tracking.

Conclusion

With this approach, we’ve successfully solved the Magento Google Analytics product category tracking issue without modifying any core files. By creating a local extension in your app/code/local/Mage directory, you can easily add or modify functionality as needed.

Remember to replace YourVendorName with your actual vendor name throughout the code and make sure to clear your cache after making these changes.

If you found this solution helpful, please consider sharing it on Laravel Company for others in the development community!

Happy coding!

Magento 1 Catalog Orders Pricing

Our network