Skip to content

Instantly share code, notes, and snippets.

@simbus82
Last active Aug 29, 2015
Embed
What would you like to do?
Ecommerce trakicng universal analytics
<! ----- tracking code -------- !>
<?php
// Transaction Data
$orderID = $this->getOrderId();
$order = Mage::getModel('sales/order')->loadByIncrementId($orderID);
$orderTotal = $order->getGrandTotal();
// you can also add affiliation, shipping and tax
$trans = array('id' => $orderID,
'revenue' => $orderTotal);
// List of Items Purchased
$items = array();
foreach ($order->getAllItems() as $item){
foreach($item->getChildrenItems() as $child) {
$items[] = array(
'name' => $item->getName(),
'sku' => $child->getSku(),
'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']}',
// if you added affiliation, shipping or tax above, include them here as well
'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',
// if you added SKU or category above, include them here as well
'name': '{$item['name']}',
'sku': '{$item['sku']}',
'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>
<! --------- fine tracking code ------- !>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment