0
votes

My company uses layout.phtml which is being shared by all the pages in the website,where I have inserted the basic Google Analytics code . Now I want to insert the Ecommerce tracking code into the partial head(any code specific to that page goes here)for only the confirmation page which is also sharing the layout.phtml page. Do I have to include this again for ecommerce tracking code apart from the one in the Basic GA?

var _gaq = _gaq || []; 
_gaq.push(['_setAccount', 'UA-XXXXX-X']);**

<---Ecommerece Tracking Code will go here---->

(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();

Else My Ecommerce tracking code, which is mainly :

_gaq.push(['_addTrans','','','','']);

_gaq.push(['_addItem','','','','','',]);

_gaq.push(['_trackTrans']);

which remains outside the Basic Google Analytics code. Would it still work?

This is how my code looks now when I view source my confirmation page:

var _gaq = _gaq || [];

_gaq.push(['_setAccount', 'UA-XXXXX-X']);

_gaq.push(['_trackPageview']);

(function() {

 var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;

  ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';

  var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();

<!------Ecommerce tracking code for confirmation page--->

<?php if($this->tx_id == true && $this->rd['total'] == true){?>

_gaq.push(['_addTrans',<?=$this->tx_id ?>,'',<?=$this->rd['total']?>,'','','','','']);

<!------Items purchased------>

<?php foreach($this->dd as $sku=>$val) {

$i++;

$product_title= $this->pp[$sku]['title'];

$qty = $val['pt']['qty']; ?>

_gaq.push(['_addItem',

<?= $this->tx_id ?>,

<?= $sku ?>,

<?= $this->pp[$sku]['title'] ?>,

'',

<?= $this->pp[$sku]['price']?>,

<?= $qty ?>

 ]);

<?php }?>

 _gaq.push(['_trackTrans']); 

<?php  }?>

<!-------Ecommerce tracking code ends here---->

Could anyone please review it? Also Is there any way to test it before sending to prduction?

1
Did you check the HTML of the target page? if it already contains the function to import the GA scripts then you will not need to call it again. Nobody could possibly give you a better answer without a link to the page or more code samples. - shanabus

1 Answers

2
votes

You don't need to duplicate the tracking code.
addTrans, addItem and trackTrans can be called separately.

(you're code seems to be ok.)