Magento: Google Analytics Ecommerce Tracking with Universal Analytics
Google recently transitioned from Classic Google Analytics to Universal Analytics, which requires updates to your ecommerce tracking code. If you’re using Magento 1.7.0.2, here’s how to correctly implement the new Universal Analytics ecommerce tracking on your success.phtml page.
Understanding the Changes in Ecommerce Tracking
With the move to Universal Analytics, Google no longer supports the asynchronous tracking method used in Classic Google Analytics. Instead, you need to use the ga('ecommerce:addTransaction') and ga('ecommerce:send') functions to send transaction data to Google Analytics.
Updating Your Success.phtml Page
First, let’s address your current code:
<?php if ($this->getOrderId()): ?>
<?php if ($this->getCanViewOrder()) : ?>
...
<?php endif; ?>
<?php endif; ?>
This is the standard conditional to check for an order ID and view permission. We’ll need to add the new Universal Analytics tracking code inside this condition.
Correct Implementation of Universal Analytics Ecommerce Tracking
Here’s the updated success.phtml code with the correct Universal Analytics ecommerce tracking implementation:
<?php if ($this->getOrderId()): ?>
<?php if ($this->getCanViewOrder()) : ?>
...
<?php
// Transaction Data
$orderID = $this->getOrderId();
$order = Mage::getModel('sales/order')->loadByIncrementId($orderID);
$orderTotal = $order->getGrandTotal();
// Affiliation, shipping and tax (not used in the example)
$trans = array(
'id' => $orderID,
'revenue' => $orderTotal
);
// List of Items Purchased
$items = array();
foreach ($order->getAllItems() as $item) {
// SKU and category data for each item (not used in the example)
$items[] = array(
'name' => $item->getName(),
'price' => $item->getPrice(),
'quantity' => $item->getQtyOrdered()
);
}
// Function to return the JavaScript representation of a TransactionData object.
function getTransactionJs($trans) {
return <<<HTML
ga('ecommerce:addTransaction', {
'id': '{$trans['id']}',
'revenue': '{$trans['revenue']}'
});
HTML;
}
// Function to return the JavaScript representation of an ItemData object.
function getItemJs($transId, $item) {
return <<<HTML
ga('ecommerce:addItem', {
'id': '$transId',
'name': '{$item['name']}',
'price': '{$item['price']}',
'quantity': '{$item['quantity']}'
});
HTML;
}
?>
<script>
ga('require', 'ecommerce', 'ecommerce.js');
<?php
echo getTransactionJs($trans);
foreach ($items as $item) {
echo getItemJs($trans['id'], $item);
}
?>
ga('ecommerce:send');
</script>
<?php endif; ?>
<?php endif; ?>
Explanation of the Changes
Transaction Data
The transaction data is loaded from the current order using Magento’s built-in sales/order model. We retrieve the order ID and grand total for use in our JavaScript functions.
Items Purchased
We loop through all items in the order, creating an array for each item with its name, price, and quantity ordered. These details are used to populate the ga('ecommerce:addItem') function.
PHP Functions
Two PHP functions (getTransactionJs() and getItemJs()) are created to generate the JavaScript code needed by Universal Analytics. These functions dynamically create the necessary ga('ecommerce:addTransaction') and ga('ecommerce:addItem') calls with the appropriate data.
Google Analytics Script
The script block inside the success.phtml file loads the ecommerce.js library using ga('require', 'ecommerce', 'ecommerce.js'). It then executes our PHP-generated JavaScript code to add transactions and items, followed by ga('ecommerce:send') which sends the data to Google Analytics.
Conclusion
The updated success.phtml page with Universal Analytics ecommerce tracking is now correctly implemented. This approach ensures that your Magento store’s conversion data continues to be accurately tracked in Google Analytics after the transition from Classic to Universal Analytics.
Remember, proper implementation of ecommerce tracking is crucial for understanding customer behavior and optimizing your online sales strategy. If you have any issues or need further assistance with Magento development, don’t hesitate to contact our team at LaravelCompany.
Happy coding!