Search code examples
javascriptgoogle-analyticse-commercetracking

Google Analytics Universal Ecommerce Tracking implementation


i am in middle of upgrading our google analytics code to universal google universal anaytics. We also have ecomrece tracking on out site that needs to be updated. I was wondering if this implementation is correct:

ga code in our header for all the pages

<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-xxxxxxx-x', 'auto');
                  ga('send', 'pageview');

</script>

Then in our thank you page(header is common to all pages on the site), i've added following code for ecommerce tracking:

<script type="text/javascript"> 

        ga('require', 'ecommerce', 'ecommerce.js');   // Load the ecommerce plug-in.

        ga('ecommerce:addTransaction', {
          'id': 'xxxx-ID',                     
          'affiliation': 'Example',   
          'revenue': '124.15',              
          'shipping': '0.00',                 
          'tax': '5.00'                 
        });

                        ga('ecommerce:addItem', {
                                  'id': 'xxxx-ID',                     
                                  'name': 'testSKU',                
                                  'sku': 'testID',                   
                                  'category': '',       
                                  'price': '114.95',                 
                                  'quantity': '1'                   
                                });



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

Do i need to add following lines before loading ecommerce plugin?

ga('create', 'UA-35844822-1', 'auto'); ga('send', 'pageview');

what will be the correct implementation?

Is there any way i can track this before putting it in production?

thanks


Solution

  • Yes, you will need to add the 'create' and 'send' lines before loading the ecommerce plugin.

    ga('create', 'UA-xxxxxxx-x', 'auto');
    ga('send', 'pageview');
    ga('require', 'ecommerce', 'ecommerce.js');
    

    Some good information here: http://cutroni.com/blog/2013/03/22/tracking-ecommerce-transactions-with-universal-analytics/

    Hope this helps!