Page 1 of 7

Google Analytics Ecommerce Tracking

Posted: Thu Oct 21, 2010 4:21 pm
by justinv
For ecommerce tracking using the async GA code, I did the following:

1) Move the following code from the catalog/controller/common/footer.php into the catalog/controller/common/header.php

Code: Select all

if ($this->config->get('google_analytics_status')) {
  $this->data['google_analytics'] = html_entity_decode($this->config->get('google_analytics_code'), ENT_QUOTES, 'UTF-8');
} 
else {
  $this->data['google_analytics'] = '';
}
2) Add $google_analytics to your header.tpl file, right before the </head> tag.

3) Remove $google_analytics from your footer.tpl file. It is right before the </body> tag at the bottom.

4) Add the following code to catalog/view/theme/default/template/common/success.tpl

Code: Select all

<?php if(isset($orderDetails) && isset($orderProducts)) { ?>

<script type="text/javascript">
  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-XXXXXXXX-X']);
  _gaq.push(['_addTrans',
    '<?php echo $orderDetails['order_id']; ?>',
    'Store Name',
    '<?php echo $orderDetails['total']; ?>',
    '',
    '',
    '',
    '',
    ''
   ]);

   <?php foreach($orderProducts as $product) { ?>
      _gaq.push(['_addItem',
          "<?php echo $product['order_id']; ?>",
          "<?php echo $product['product_id']; ?>",
          <?php echo json_encode($product['name']); ?>,
          "<?php echo $product['model']; ?>",
          "<?php echo $product['price']+($product['price']*$product['tax']/100); ?>",
          "<?php echo $product['quantity']; ?>"
       ]);
   <? } ?>
   
   _gaq.push(['_trackTrans']); //submits transaction to the Analytics servers
(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);
  })();
</script>
<?php } ?>
EDIT (Steps 5 and 6 were missing from original post):
5) Add these lines to catalog/controller/checkout/success.php:

Code: Select all

$this->load->model('checkout/order');
$this->data['orderDetails'] = $this->model_checkout_order->getOrder($this->session->data['order_id']);
$this->data['orderProducts'] = $this->model_checkout_order->getOrderProducts($this->session->data['order_id']);
They should go immediately before this line:

Code: Select all

$this->cart->clear();
6) Add this function to catalog/model/checkout/order.php, if it doesn't already exist (I think it was in 1.4.7, not in 1.4.9):

Code: Select all

       
public function getOrderProducts($order_id) {
    $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "order_product WHERE order_id = '" . (int)$order_id . "'");

            return $query->rows;
        }	
That should be it. You can check the Google documentation for other parameters you can pass through on the addTrans call.

Hope it works for you. I was noticing some of my transactions were missing before I did the json_encode on the product name. Oops :)

Re: Google Analytics Ecommerce Tracking

Posted: Thu Oct 21, 2010 5:26 pm
by JAY6390
This is good, however it has one major flaw that most e-commerce stores do when using GA with e-com...it tracks orders that don't get comlpeted/reversed/cancelled. There are better ways of doing this (for example you don't actually need to rely on the customer at all, you can send the data without javascript). just my 2 cents

Re: Google Analytics Ecommerce Tracking

Posted: Thu Oct 21, 2010 7:37 pm
by Xsecrets
JAY6390 wrote:This is good, however it has one major flaw that most e-commerce stores do when using GA with e-com...it tracks orders that don't get comlpeted/reversed/cancelled. There are better ways of doing this (for example you don't actually need to rely on the customer at all, you can send the data without javascript). just my 2 cents
how do you send the data to analytics without javascirpt? All I've ever seen in their documentation is the javascript.

Re: Google Analytics Ecommerce Tracking

Posted: Thu Oct 21, 2010 7:41 pm
by JAY6390
Don't seem to be able to find the exact place I first saw the method, which was official, but there are work arounds either way such as http://www.acleon.co.uk/posts/galvanize ... avascript/
I've never actually done it myself, but know it can be done

Re: Google Analytics Ecommerce Tracking

Posted: Thu Oct 21, 2010 7:44 pm
by JAY6390
Also you need to remember that it might not send all of the data you usually send to GA, like screen res which depends on the JS side of things

Re: Google Analytics Ecommerce Tracking

Posted: Fri Oct 22, 2010 5:23 am
by justinv
Probably using server side javascript with Rhino or something. That's still javascript though.

Re: Google Analytics Ecommerce Tracking

Posted: Mon Nov 22, 2010 11:42 am
by justinv
I found what you were talking about the other day, a program called Galvanize that builds the GA img request server side rather than in js. Also saw a doc on google somewhere about doing it for tracking mobile devices without js.

Pretty cool, thanks!

Re: Google Analytics Ecommerce Tracking

Posted: Fri Jan 07, 2011 4:48 am
by doodlemoonch
The success controller unset's it all... http://forum.opencart.com/viewtopic.php?f=20&t=23267

How do you set $orderDetails or $orderProducts?

Re: Google Analytics Ecommerce Tracking

Posted: Fri Jan 14, 2011 6:37 am
by jcgadgets
Hi,

So what is our conclusion here? Of course, I do not want to be counting incomplete / cancelled sales.

I tried to check out the Galvanize link for info, but got a 404.


Thank you,
Jared

Re: Google Analytics Ecommerce Tracking

Posted: Fri Jan 14, 2011 8:28 am
by justinv
My conclusion is that the galvanize php method was designed for browsers that don't have javascript capabilities (mobile phones a while ago, but most now have js). Use the advice above for a standard implementation of google analytics ecommerce tracking.

The google analytics code above will track every completed checkout on your website. If these checkouts are later cancelled they will still show up in google analytics (which they should, as somebody did actually go through your checkout).

That's my take.

Re: Google Analytics Ecommerce Tracking

Posted: Fri Jan 14, 2011 9:18 am
by jcgadgets
Thanks. I wasn't sure if (when above it said if orders "don't get completed/cancelled/reversed") it would count something as a sale even if the customer never completed the order.

I'll try implementing this and see what happens, thanks!


Jared

Re: Google Analytics Ecommerce Tracking

Posted: Sat Jan 29, 2011 5:06 pm
by jcgadgets
Hi again,

Sorry to revisit this again, I am just implementing now and just had a few questions / clarifications from a novice point of view....

1.
Does it matter at all where I put the code from step one in my header.php file?

2.
I assume that I am supposed to insert:

Code: Select all

<?php echo $google_analytics; ?>
in step two, rather than just plain:

Code: Select all

$google_analytics
?

3.
Does it matter at all where I put the code from step four in my success.tpl file?

4.
This line worried me a bit:
Hope it works for you. I was noticing some of my transactions were missing before I did the json_encode on the product name. Oops :)
- Is there anything that I need to do regarding this?


Thank you all again! This forum is truly what makes OpenCart GREAT.
Jared

Re: Google Analytics Ecommerce Tracking

Posted: Sun Jan 30, 2011 11:15 am
by justinv
1. Nope, doesn't matter, but best practices would dictate that it goes immediately before </head>

2. That is correct. You're basically taking it out of the footer and putting it in the header file.

3. No it doesn't matter, as long as you don't put it in the middle of an html tag or some other php and break a tag.

4. Nothing to do here - it is already in the code I listed above.

Once you have it set up make a purchase on your site and you can test whether it's working by checking your analytics account.

Re: Google Analytics Ecommerce Tracking

Posted: Sun Jan 30, 2011 4:35 pm
by doodlemoonch
I followed the instructions but OrderDetails was undefined, do any changes need to be made in the success controller?

Re: Google Analytics Ecommerce Tracking

Posted: Sun Jan 30, 2011 5:32 pm
by justinv
Does your controller have this line in it:

Code: Select all

$this->data['orderDetails'] = $this->model_checkout_order->getOrder($this->session->data['order_id']);
I think this is in the core, but I could have edited it without remembering... let me know, I'll update if necessary!

Re: Google Analytics Ecommerce Tracking

Posted: Sun Jan 30, 2011 5:46 pm
by doodlemoonch
It dosen't seem to, I guess I put here?

Code: Select all

		if (isset($this->session->data['order_id'])) {

		  // set it

		  $this->data['orderDetails'] = $this->model_checkout_order->getOrder($this->session->data['order_id']);


		  // continue clearing
			$this->cart->clear();
			
			unset($this->session->data['shipping_method']);
			unset($this->session->data['shipping_methods']);
			unset($this->session->data['payment_method']);
			unset($this->session->data['payment_methods']);
			unset($this->session->data['guest']);
			unset($this->session->data['comment']);
			unset($this->session->data['order_id']);	
			unset($this->session->data['coupon']);

Re: Google Analytics Ecommerce Tracking

Posted: Sun Jan 30, 2011 8:01 pm
by Brocberry
For those of us who send our customers to off-site to make payment, we need to give the gateway a page to send the customer back to once the transaction is complete... and then we tell GA which that page is I think. What page can we send the customer back to in OC for GA to recognise a complete transaction?

Re: Google Analytics Ecommerce Tracking

Posted: Sun Jan 30, 2011 9:36 pm
by Xsecrets
Brocberry wrote:For those of us who send our customers to off-site to make payment, we need to give the gateway a page to send the customer back to once the transaction is complete... and then we tell GA which that page is I think. What page can we send the customer back to in OC for GA to recognise a complete transaction?
index.php?route=common/success most of the gateways will set this for you automatically.

Re: Google Analytics Ecommerce Tracking

Posted: Tue Feb 01, 2011 4:53 am
by jcgadgets
Xsecrets wrote:
Brocberry wrote:For those of us who send our customers to off-site to make payment, we need to give the gateway a page to send the customer back to once the transaction is complete... and then we tell GA which that page is I think. What page can we send the customer back to in OC for GA to recognise a complete transaction?
index.php?route=common/success most of the gateways will set this for you automatically.
Do you know if the PayPal Standard module has this set automatically?


Thank you,
Jared

Re: Google Analytics Ecommerce Tracking

Posted: Tue Feb 01, 2011 4:56 am
by jcgadgets
I also wanted to see if what we have concluded here coincides with what others have come up in this thread?:

http://forum.opencart.com/viewtopic.php ... ilit=ecomm

Or if perhaps we can get some ideas from there. Which modification method is "better"?


Thank you,
Jared