Community Forums

Google Analytics Ecommerce Tracking

Free manual modifications can be contributed here. Modifications are manual snippets of code that are pasted into the forums for others to use.

Google Analytics Ecommerce Tracking

Postby justinv » Thu Oct 21, 2010 8:21 am

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 :)
Last edited by justinv on Thu Feb 03, 2011 3:12 am, edited 1 time in total.
justinv
 
Posts: 660
Joined: Tue Oct 12, 2010 5:24 am

Re: Google Analytics Ecommerce Tracking

Postby JAY6390 » Thu Oct 21, 2010 9:26 am

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
ImageImageImage

Better Product SEO URL's - Perfectly structured product links
Better Category SEO URL's - Give subcategories the same SEO keyword
SEO URL's Route Editor - Fix all of your index.php links


Image
User avatar
JAY6390
 
Posts: 4639
Joined: Wed May 26, 2010 3:47 pm
Location: United Kingdom

Re: Google Analytics Ecommerce Tracking

Postby Xsecrets » Thu Oct 21, 2010 11:37 am

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.
Xsecrets
 
Posts: 5042
Joined: Sat Oct 24, 2009 7:51 pm
Location: FL US

Re: Google Analytics Ecommerce Tracking

Postby JAY6390 » Thu Oct 21, 2010 11:41 am

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
ImageImageImage

Better Product SEO URL's - Perfectly structured product links
Better Category SEO URL's - Give subcategories the same SEO keyword
SEO URL's Route Editor - Fix all of your index.php links


Image
User avatar
JAY6390
 
Posts: 4639
Joined: Wed May 26, 2010 3:47 pm
Location: United Kingdom

Re: Google Analytics Ecommerce Tracking

Postby JAY6390 » Thu Oct 21, 2010 11:44 am

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
ImageImageImage

Better Product SEO URL's - Perfectly structured product links
Better Category SEO URL's - Give subcategories the same SEO keyword
SEO URL's Route Editor - Fix all of your index.php links


Image
User avatar
JAY6390
 
Posts: 4639
Joined: Wed May 26, 2010 3:47 pm
Location: United Kingdom

Re: Google Analytics Ecommerce Tracking

Postby justinv » Thu Oct 21, 2010 9:23 pm

Probably using server side javascript with Rhino or something. That's still javascript though.
justinv
 
Posts: 660
Joined: Tue Oct 12, 2010 5:24 am

Re: Google Analytics Ecommerce Tracking

Postby justinv » Mon Nov 22, 2010 3:42 am

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!
justinv
 
Posts: 660
Joined: Tue Oct 12, 2010 5:24 am

Re: Google Analytics Ecommerce Tracking

Postby doodlemoonch » Thu Jan 06, 2011 8:48 pm

The success controller unset's it all... http://forum.opencart.com/viewtopic.php?f=20&t=23267

How do you set $orderDetails or $orderProducts?
doodlemoonch
 
Posts: 26
Joined: Wed May 12, 2010 1:27 pm

Re: Google Analytics Ecommerce Tracking

Postby jcgadgets » Thu Jan 13, 2011 10:37 pm

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
jcgadgets
 
Posts: 115
Joined: Sun Oct 31, 2010 8:49 am

Re: Google Analytics Ecommerce Tracking

Postby justinv » Fri Jan 14, 2011 12:28 am

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.
justinv
 
Posts: 660
Joined: Tue Oct 12, 2010 5:24 am

Re: Google Analytics Ecommerce Tracking

Postby jcgadgets » Fri Jan 14, 2011 1:18 am

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
jcgadgets
 
Posts: 115
Joined: Sun Oct 31, 2010 8:49 am

Re: Google Analytics Ecommerce Tracking

Postby jcgadgets » Sat Jan 29, 2011 9:06 am

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
jcgadgets
 
Posts: 115
Joined: Sun Oct 31, 2010 8:49 am

Re: Google Analytics Ecommerce Tracking

Postby justinv » Sun Jan 30, 2011 3:15 am

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.
justinv
 
Posts: 660
Joined: Tue Oct 12, 2010 5:24 am

Re: Google Analytics Ecommerce Tracking

Postby doodlemoonch » Sun Jan 30, 2011 8:35 am

I followed the instructions but OrderDetails was undefined, do any changes need to be made in the success controller?
doodlemoonch
 
Posts: 26
Joined: Wed May 12, 2010 1:27 pm

Re: Google Analytics Ecommerce Tracking

Postby justinv » Sun Jan 30, 2011 9:32 am

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!
justinv
 
Posts: 660
Joined: Tue Oct 12, 2010 5:24 am

Re: Google Analytics Ecommerce Tracking

Postby doodlemoonch » Sun Jan 30, 2011 9:46 am

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']);
doodlemoonch
 
Posts: 26
Joined: Wed May 12, 2010 1:27 pm

Re: Google Analytics Ecommerce Tracking

Postby Brocberry » Sun Jan 30, 2011 12:01 pm

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?
Brocberry
 
Posts: 47
Joined: Mon May 31, 2010 7:31 pm
Location: England

Re: Google Analytics Ecommerce Tracking

Postby Xsecrets » Sun Jan 30, 2011 1:36 pm

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.
Xsecrets
 
Posts: 5042
Joined: Sat Oct 24, 2009 7:51 pm
Location: FL US

Re: Google Analytics Ecommerce Tracking

Postby jcgadgets » Mon Jan 31, 2011 8:53 pm

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
jcgadgets
 
Posts: 115
Joined: Sun Oct 31, 2010 8:49 am

Re: Google Analytics Ecommerce Tracking

Postby jcgadgets » Mon Jan 31, 2011 8:56 pm

I also wanted to see if what we have concluded here coincides with what others have come up in this thread?:

viewtopic.php?f=20&t=18926&hilit=ecomm

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


Thank you,
Jared
jcgadgets
 
Posts: 115
Joined: Sun Oct 31, 2010 8:49 am

Next

Return to Modifications

Who is online

Users browsing this forum: No registered users and 4 guests

Hosted by Arvixe Web Hosting