I have installed Google Tag Manager and want to add an additional datalayer for ecommerce tracking on success page which contains the transition id, product specific data like sku, price, name, quantity and so on.

I want something like this for my success page after open body tag (pseudocode):

       if ( $this->getOrderId() ) $transactionId = $this->getOrderId();
       $currentOrder = Mage::getResourceModel( 'sales/order_collection' )->byId( $transactionId ); 
       $transactionAffiliation = Mage::app()->getStore()->getFrontendName();
       $transactionTotal = $currentOrder->getTotal();
       $transactionTax = $currentOrder->getTax();
       $transactionShipping = $currentOrder->getShipping();
       $transactionProducts = $currentOrder->getProducts();
   dataLayer = [{
     'transactionId': <?php echo "'".$transactionId."'" ?>,
     'transactionAffiliation': <?php echo "'".$transactionAffiliation."'" ?>,
     'transactionTotal': <?php echo $transactionTotal ?>,
     'transactionTax': <?php echo $transactionTax ?>,
     'transactionShipping': <?php echo $transactionShipping ?>,
     'transactionProducts': [
      <?php foreach( $transactionProducts as $product ): ?>
         'sku': <?php echo "'".$product->getSku()."'" ?>,
         'name': <?php echo "'".$product->getName()."'" ?>,
         'category': <?php echo "'".$product->getCategory()."'" ?>,
         'price': <?php echo $product->getPrice() ?>,
         'quantity': <?php echo $product->getQuantity() ?>
      <?php endforeach; ?>

So I have to replace this pseudocode with "real" Magento code to get the order details on succes page. But which correct functions should I use to implement this?

Hope you can help me :)


Here is the code which can go on success page -:

$orderId = Mage::getSingleton('checkout/session')->getLastOrderId();
$order = Mage::getModel('sales/order')->load($orderId);
$orderCurrency      = $order->getBaseCurrencyCode();
$orderGrandTotal    = $order->getBaseGrandTotal();
$orderShippingTotal = $order->getBaseShippingAmount();
$orderTax           = $order->getBaseTaxAmount();
$orderItems = $order->getAllVisibleItems();
<script type="text/javascript">
    window.dataLayer = window.dataLayer || [];
    <?php $intCtr=0;?>
      'ecommerce': {
        'purchase': {
          'actionField': {
            'id': '<?php echo $order->getIncrementId()?>',                         // Transaction ID. Required for purchases and refunds.
            'affiliation': '<?php echo $order->getAffiliation() ?>',
            'revenue': '<?php echo $orderGrandTotal?>',                     // Total transaction value (incl. tax and shipping)
            'tax': '<?php echo $orderTax?>',
            'shipping': '<?php echo $orderShippingTotal?>',
            'coupon': '<?php echo $order->getCouponCode() ?>'
          'products': [
          <?php foreach($orderItems as $item): ?>
                <?php $intCtr++;?>
                <?php if($item->getParentItemId()) continue;?>
            'name': '<?php echo $this->jsQuoteEscape($item->getName()) ?>',     // Name or ID is required.
            'id': '<?php echo $this->jsQuoteEscape($item->getSku()) ?>',
            'price': '<?php echo $item->getBasePrice()?>',
            'quantity': <?php echo $item->getQtyOrdered()?>
            <?php if ($intCtr==count($orderItems)):?>
            <?php else:?>
            <?php endif;?>
            <?php endforeach;?>

If you are not a Magento developer then recommendation is to use good module or extension which gives you much more insight and data to see better reporting in Google Analytics. Have a look at the following module


Hope it helps!

  • Why do I feel that theres a missing peice that has to be setup in gtm? Do we need to add a tag with a transaction track type or anything?
    – CD Brian
    May 5 '17 at 15:47
  • @CDBrian - You just need to have Universal Analytics PageView Tag with just Enable Enhanced Ecommerce Features and use data layer on
    – user28171
    Jul 15 '17 at 23:00

We created a free module that pushes DataLayer events for all of these Enhanced eCommerce features.


Creare_SEO will let you put in the GTM container ID in the config, and render that snippet for you. It's also free.


If you don't feel like adding an extra module, you can add the GTM container code to your pages by adding it to the Miscellaneous Scripts config field at System > Configuration > General > Design > HTML Head > Miscellaneous Scripts

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.