Created
February 8, 2016 16:05
-
-
Save mikaelz/55a7c85b5036b10afd43 to your computer and use it in GitHub Desktop.
Joomla 1.5 + VirtueMart 1.1.2 Google Analytics Ecommerce tracking integration
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
<?php | |
// components/com_virtuemart/themes/default/templates/pages/checkout.thankyou.tpl.php | |
// after line 24 | |
$order_id = $db->record[0]->order_id; | |
$order_items = new ps_DB; | |
$q = "SELECT * FROM `#__{vm}_order_item` WHERE `order_id`='$order_id'"; | |
$order_items->query($q); | |
$GLOBALS['GA_ECOMM_ITEMS'] = array(); | |
foreach ($order_items->record as $item) { | |
$GLOBALS['GA_ECOMM_ITEMS'][] = array( | |
'sku' => $item->order_item_sku, | |
'name' => htmlspecialchars($item->order_item_name), | |
'category' => '', | |
'price' => $item->product_final_price, | |
'quantity' => $item->product_quantity, | |
); | |
} | |
$GLOBALS['GA_ECOMM_TRANS'] = array( | |
'id' => $order_id, | |
'affiliation' => '', | |
'revenue' => $db->record[0]->order_total, | |
'shipping' => $db->record[0]->order_shipping, | |
'tax' => $db->record[0]->order_tax | |
); |
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
<!-- Put the following before </head> --> | |
<script> | |
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ | |
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), | |
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) | |
})(window,document,'script','//www.google-analytics.com/analytics.js','ga'); | |
ga('create', 'UA-YOUR_ACCOUNT_ID', 'auto'); | |
ga('require', 'ecommerce'); | |
</script> | |
<!-- The following should go right before </body> --> | |
<script> | |
<?php | |
function getItemJs(&$transId, &$item) { | |
return " | |
ga('ecommerce:addItem', { | |
'id': '$transId', | |
'name': '{$item['name']}', | |
'sku': '{$item['sku']}', | |
'category': '{$item['category']}', | |
'price': '{$item['price']}', | |
'quantity': '{$item['quantity']}' | |
}); | |
"; | |
} | |
function getTransactionJs(&$trans) { | |
return " | |
ga('ecommerce:addTransaction', { | |
'id': '{$trans['id']}', | |
'affiliation': '{$trans['affiliation']}', | |
'revenue': '{$trans['revenue']}', | |
'shipping': '{$trans['shipping']}', | |
'tax': '{$trans['tax']}' | |
}); | |
"; | |
} | |
if (!empty($GLOBALS['GA_ECOMM_TRANS'])) { | |
echo getTransactionJs($GLOBALS['GA_ECOMM_TRANS']); | |
foreach ($GLOBALS['GA_ECOMM_ITEMS'] as &$item) { | |
echo getItemJs($trans['id'], $item); | |
} | |
echo "ga('ecommerce:send');"; | |
} | |
?> | |
ga('send', 'pageview'); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment