Reputation: 219
I've used the previous version of analytics many times to setup ecommerce tracking, but not getting the new universal analytics version to work.
I have this so far on the receipt page (all the variables populate correctly when I run a test transaction, I just put in placeholder variables for below example), but no pixel is fired it seems for the transaction:
<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-########-#', 'secure.example.com');
ga('require', 'ecommerce', 'ecommerce.js');
ga('send', 'pageview');
ga('ecommerce:addTransaction', {
'id': '[id]',
'affiliation': '[storename]',
'revenue':'[total]',
'shipping':'[shipping]',
'tax': '[tax]'
});
#foreach ($item in $order.getItems())
ga('ecommerce:addItem', {
'id': '[id]',
'name': '[product]',
'sku': '[sku]',
'category': '',
'price': '[unitprice]',
'quantity': '[quantity]'
});
#end
ga('ecommerce:send');
</script>
Firing a pageview first worked for me in the previous version of analytics; is this the wrong way to structure this for the Universal analytics snippet?
Upvotes: 0
Views: 1046
Reputation: 11
The following piece of code is from our test domain which you can compare and it does send the correct data to GA for sure.
<script type="text/javascript">
//<![CDATA[
(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-XXXXXXXX-X', 'auto');
ga('send', 'pageview');
ga('require', 'ecommerce', 'ecommerce.js');
ga('set', 'currencyCode', 'GBP');
ga('ecommerce:addTransaction', {
'id': '100000001',
'affiliation': '',
'revenue': '143.3000',
'shipping': '88.3000',
'tax': '0.0000'});
ga('ecommerce:addItem', {
'id': '100000001',
'name': 'Blue Horizons Bracelets',
'sku': 'acj0006s',
'category': 'Bracelets',
'price': '55.0000',
'quantity': '1.0000'});
ga('ecommerce:send');
//]]>
</script>
If you don't mind spending some bucks then the following module could do the job for you.
For Magento 1
https://www.scommerce-mage.com/magento-google-enhanced-ecommerce-tracking.html
For Magento 2
https://www.scommerce-mage.com/magento2-google-enhanced-ecommerce-tracking.html
Upvotes: 1
Reputation: 219
So, with a regular Universal Analytics snippet in the header doing the pageview, this works in the receipt page body:
<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('require', 'ecommerce', 'ecommerce.js');
ga('ecommerce:addTransaction', {
'id': '[id]',
'affiliation': '[storename]',
'revenue':'[total]',
'shipping':'[shipping]',
'tax': '[tax]'
});
#foreach ($item in $order.getItems())
ga('ecommerce:addItem', {
'id': '[id]',
'name': '[product]',
'sku': '[sku]',
'category': '',
'price': '[unitprice]',
'quantity': '[quantity]'
});
#end
ga('ecommerce:send');
</script>
Upvotes: 0