Last active
August 29, 2015 14:13
-
-
Save simbus82/38c19d77fb425caa9e0c to your computer and use it in GitHub Desktop.
Ecommerce trakicng universal analytics
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<! ----- 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