Skip to content

Instantly share code, notes, and snippets.

@simbus82
Created April 18, 2016 10:56
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/cdad05f23f09511f8b683bb7e19a204c to your computer and use it in GitHub Desktop.
Save simbus82/cdad05f23f09511f8b683bb7e19a204c to your computer and use it in GitHub Desktop.
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