Adam Stevens
Adam Stevens

Reputation: 13

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>

Upvotes: 0

Views: 604

Answers (3)

Dinesh Gopal Chand
Dinesh Gopal Chand

Reputation: 587

This is your code

<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>

Surly Opencart using tpl file for template. and this code suppose to work. since it is not working then try below code.

<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': '<?php echo $order_query['order_id'];?>',
  'revenue': <?php echo $price;?>,
});
ga('ecommerce:addItem', {
  'id': '<?php echo $order_query['order_id'];?>',
  'name': '<?php echo $product['name'];?>',
  'sku': '<?php echo $product['model'] ;?>',
  'price': <?php echo $product['price'];?>,
  'quantity': <?php echo $product['quantity']};?>,
  'currency': 'GBP'
});

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

and if there is problem with your variables then you should go through this link http://www.notesonclick.com/blog/ecommerce-tracking-in-opencart-2-3/ this might be useful .

Upvotes: 1

mackwizard
mackwizard

Reputation: 85

The best way to implement this would be to use an extension already made. It will save you messing about and should work straight away. There's loads on the store. I've tried a few analytics extensions for opencart but the best and easiest to work with in my experience is this one: http://www.opencart.com/index.php?route=extension/extension/info&extension_id=4496 It took me about 10 minutes to set up and I had ecommerce and site data in my analytics account the same day

Upvotes: 0

Linda Lawton - DaImTo
Linda Lawton - DaImTo

Reputation: 117254

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.

Upvotes: 0

Related Questions