Google Tag Manager & Magento Ecommerce Implementation

Before completing the below steps, you will need to make sure that your Google Tag Manager container is installed properly on the site. You will also need to create Google Analytics ecommerce tracking rules and macros within the Google Tag Manager container. This tutorial assumes you have already done that!

Below are some examples of what your code should look like once you implement ecommerce tracking for Google Tag Manager. Obviously, there are multiple ways you can implement the code, however I provided the way I did it. Feel free to use whatever method works best for you.

We will need to use dataLayer instead of _gaq, which is usually used when implementing Google Analytics ecommerce tracking. Once that is implemented, you can submit a few test orders and test it out.

What the Magento PHP code looks like:

File: /app/code/local/Mage/GoogleAnalytics/Block/Ga.php
        if (empty($orderIds) || !is_array($orderIds)) {
        $collection = Mage::getResourceModel('sales/order_collection')
            ->addFieldToFilter('entity_id', array('in' => $orderIds))

        $aOrders = array();
        foreach ($collection as $order) {
            $objOrder = new stdClass();
            $objOrder->transactionId = $order->getIncrementId();
            $objOrder->transactionAffiliation = Mage::app()->getStore()->getFrontendName();
            $objOrder->transactionTotal = $order->getBaseGrandTotal();
            $objOrder->transactionTax = $order->getBaseTaxAmount();
            $objOrder->transactionShipping = $order->getBaseShippingAmount();

            $aItems = array();
            foreach ($order->getAllVisibleItems() as $item) {
                $objItem = array();
                $objItem['sku'] = $item->getSku();
                $objItem['name'] = $item->getName();
                $objItem['category'] = null; //todo
                $objItem['price'] = $item->getBasePrice();
                $objItem['quantity'] = $item->getQtyOrdered();
                $aItems[] = (object) $objItem;

            $objOrder->transactionProducts = $aItems;
            $aOrders[] = $objOrder;

        return (empty($aOrders))? null : sprintf('dataLayer = %s;', json_encode($aOrders));

What the HTML page looks like: