Skip to content

Instantly share code, notes, and snippets.

@matdave
Created September 4, 2015 15:47
Show Gist options
  • Save matdave/ce828357dc44a064d8f2 to your computer and use it in GitHub Desktop.
Save matdave/ce828357dc44a064d8f2 to your computer and use it in GitHub Desktop.
Magento Ecommerce Snippet
<?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){
// you can also add SKU and category
$items[] = array('name' => $item->getName(),
'price' => $item->getPrice(),
'quantity' => $item->getQtyOrdered(),
'sku' => $item->getSku());
}
// 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']}',
'price': '{$item['price']}',
'quantity': '{$item['quantity']}',
'sku' : '{$item['sku']}'
});
HTML;
}
?>
<script>
ga('require', 'ecommerce', 'ecommerce.js');
<?php
echo getTransactionJs($trans);
foreach ($items as &$item) {
echo getItemJs($trans['id'], $item);
}
?>
ga('ecommerce:send');
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment