Skip to content

Instantly share code, notes, and snippets.

@simbus82
Last active August 29, 2015 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simbus82/38c19d77fb425caa9e0c to your computer and use it in GitHub Desktop.
Save simbus82/38c19d77fb425caa9e0c to your computer and use it in GitHub Desktop.
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