Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Ecommerce Tracking Analytics Magento
...
<?php
// Transaction Data
$orderID = $this->getOrderId();
$order = Mage::getModel('sales/order')->loadByIncrementId($orderID);
$orderTotal = $order->getGrandTotal();
$orderDetails = $order->getData();
$couponCode = $orderDetails['coupon_code'];
?>
<!-- script per ecommerce analitycs -->
<script type="text/javascript">
dataLayer.push({
"ecommerce": {
"currencyCode": "EUR",
"purchase": {
"actionField": {
"id": "<?php echo $orderID; ?>",
"affiliation": "SITENAME",
"revenue": "<?php echo $order->getGrandTotal(); ?>",
"tax":"<?php echo $order->getTaxAmount(); ?>",
"shipping": "<?php echo $order->getShippingAmount(); ?>",
"coupon": "<?php echo $couponCode; ?>"
},
"products": [
<?php $numitem = 0; $numitem = count($order->getAllItems());
foreach ($order->getAllVisibleItems() as $item){
$numitem = $numitem - 1; ?>
{
"name": "<?php echo str_replace('"', "", $item->getName()); ?>",
"id": "<?php echo $item->getSku(); ?>",
"price": "<?php echo $item->getPrice(); ?>",
"quantity": <?php echo $item->getQtyOrdered(); ?>
}
<?php if ($numitem != 0) {echo ",";}
} ?>
]
}
}
});
</script>
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment