Post by justinv » Thu Oct 21, 2010 4:21 pm

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 11:12 am, edited 1 time in total.

Documentation: OpenCart User Guide
Mods: Total Import PRO | CSV Import PRO | Ecom Tracking | Any Feed | Autosuggest | OpenCart CDN
Image


Active Member

Posts

Joined
Tue Oct 12, 2010 1:24 pm

Post by JAY6390 » Thu Oct 21, 2010 5:26 pm

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

Image


User avatar
Guru Member

Posts

Joined
Wed May 26, 2010 11:47 pm
Location - United Kingdom

Post by Xsecrets » Thu Oct 21, 2010 7:37 pm

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.

OpenCart commercial mods and development http://spotonsolutions.net
Layered Navigation
Shipment Tracking
Vehicle Year/Make/Model Filter


Guru Member

Posts

Joined
Sun Oct 25, 2009 3:51 am
Location - FL US

Post by JAY6390 » Thu Oct 21, 2010 7:41 pm

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

Image


User avatar
Guru Member

Posts

Joined
Wed May 26, 2010 11:47 pm
Location - United Kingdom

Post by JAY6390 » Thu Oct 21, 2010 7:44 pm

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

Image


User avatar
Guru Member

Posts

Joined
Wed May 26, 2010 11:47 pm
Location - United Kingdom

Post by justinv » Fri Oct 22, 2010 5:23 am

Probably using server side javascript with Rhino or something. That's still javascript though.

Documentation: OpenCart User Guide
Mods: Total Import PRO | CSV Import PRO | Ecom Tracking | Any Feed | Autosuggest | OpenCart CDN
Image


Active Member

Posts

Joined
Tue Oct 12, 2010 1:24 pm

Post by justinv » Mon Nov 22, 2010 11: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!

Documentation: OpenCart User Guide
Mods: Total Import PRO | CSV Import PRO | Ecom Tracking | Any Feed | Autosuggest | OpenCart CDN
Image


Active Member

Posts

Joined
Tue Oct 12, 2010 1:24 pm

Post by doodlemoonch » Fri Jan 07, 2011 4:48 am

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

How do you set $orderDetails or $orderProducts?

New member

Posts

Joined
Wed May 12, 2010 9:27 pm

Post by jcgadgets » Fri Jan 14, 2011 6:37 am

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

Active Member

Posts

Joined
Sun Oct 31, 2010 4:49 pm

Post by justinv » Fri Jan 14, 2011 8: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.

Documentation: OpenCart User Guide
Mods: Total Import PRO | CSV Import PRO | Ecom Tracking | Any Feed | Autosuggest | OpenCart CDN
Image


Active Member

Posts

Joined
Tue Oct 12, 2010 1:24 pm

Post by jcgadgets » Fri Jan 14, 2011 9: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

Active Member

Posts

Joined
Sun Oct 31, 2010 4:49 pm

Post by jcgadgets » Sat Jan 29, 2011 5:06 pm

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

Active Member

Posts

Joined
Sun Oct 31, 2010 4:49 pm

Post by justinv » Sun Jan 30, 2011 11: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.

Documentation: OpenCart User Guide
Mods: Total Import PRO | CSV Import PRO | Ecom Tracking | Any Feed | Autosuggest | OpenCart CDN
Image


Active Member

Posts

Joined
Tue Oct 12, 2010 1:24 pm

Post by doodlemoonch » Sun Jan 30, 2011 4:35 pm

I followed the instructions but OrderDetails was undefined, do any changes need to be made in the success controller?

New member

Posts

Joined
Wed May 12, 2010 9:27 pm

Post by justinv » Sun Jan 30, 2011 5:32 pm

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!

Documentation: OpenCart User Guide
Mods: Total Import PRO | CSV Import PRO | Ecom Tracking | Any Feed | Autosuggest | OpenCart CDN
Image


Active Member

Posts

Joined
Tue Oct 12, 2010 1:24 pm

Post by doodlemoonch » Sun Jan 30, 2011 5:46 pm

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']);

New member

Posts

Joined
Wed May 12, 2010 9:27 pm

Post by Brocberry » Sun Jan 30, 2011 8: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?

New member

Posts

Joined
Tue Jun 01, 2010 3:31 am
Location - England

Post by Xsecrets » Sun Jan 30, 2011 9: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.

OpenCart commercial mods and development http://spotonsolutions.net
Layered Navigation
Shipment Tracking
Vehicle Year/Make/Model Filter


Guru Member

Posts

Joined
Sun Oct 25, 2009 3:51 am
Location - FL US

Post by jcgadgets » Tue Feb 01, 2011 4:53 am

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

Active Member

Posts

Joined
Sun Oct 31, 2010 4:49 pm

Post by jcgadgets » Tue Feb 01, 2011 4:56 am

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

Active Member

Posts

Joined
Sun Oct 31, 2010 4:49 pm
Who is online

Users browsing this forum: No registered users and 40 guests