Adam Stevens Adam Stevens - 2 months ago 16
Javascript Question

Google Analytics Ecommerce Tracking Data Issue

The Google Analytics example code for tracking ecommerce in the new analytics.js instead of ga.js gives data in the fields where I have $product['name'] etc.

The code below currently doesn't work and I'm not 100% sure it's pulling the correct information onpage as this is what is shown in the page source instead of 'id': '{1455}', it's showing 'id': '{$order_query['order_id']}',. We're using Opencart 1.5.5.1.

Is this the correct code?
Are the $price, $product['name'] sections correct?

<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-21397982-1', 'auto');
ga('send', 'pageview');
ga('require', 'ecommerce', 'ecommerce.js');

ga('ecommerce:addTransaction', {
'id': '{$order_query['order_id']}',
'revenue': '{$price}',
});
ga('ecommerce:addItem', {
'id': '$order_query['order_id']',
'name': '{$product['name']}',
'sku': '{$product['model']}',
'price': '{$product['price']}',
'quantity': '{$product['quantity']}',
'currency': 'GBP'
});

ga('ecommerce:send');
</script>

Answer

your raw php code should be echoing out the values in to the JavaScript

'id': '{<?php echo $order_query['order_id']; ?> }',

what you are sending to Google Anlayitcs now is the

id of "{$order_query['order_id']}" which is a text string and not going to work.