Page 1 of 1

Google Analystics Ecomm Tracking

Posted: Mon Aug 23, 2010 6:21 pm
by rockfreak
I'm just in the process of adding my own Google Analytics to my carts. I am using the async code which needs to go in the head tag (thats no problem). What I am trying to do know is on the Order success page I want to be able to pull all the order details and products for that order from the database then pop them into the google tracking code.

Can't quite figure out the best way to go about this! Any suggestions?

Re: Google Analystics Ecomm Tracking

Posted: Mon Aug 23, 2010 7:07 pm
by dbstr
In catalog/controller/checkout/success.php fetch the data before the sessions are being unset

Replace

Code: Select all

if (isset($this->session->data['order_id'])) {
            $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']);
        } 
with something like:

Code: Select all

        if (isset($this->session->data['order_id'])) {
            $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']);
            
            $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']);
        } 
and then on the success.tpl located in the common folder add a check if $orderDetails and $orderProducts is set.

Code: Select all

<?php if(isset($orderDetails) && isset($orderProducts)) { ?> 
<script type="text/javascript">
  var gaJsHost = (("https:" == document.location.protocol ) ? "https://ssl." : "http://www.");
  document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try{
  var pageTracker = _gat._getTracker("UA-xxxxx-x");
  pageTracker._trackPageview();
  pageTracker._addTrans(
      "<?php echo $orderDetails['order_id']; ?>",
      "Shop Name",
      "<?php echo $orderDetails['total']; ?>",
      "",
      "",
      "<?php echo $orderDetails['name']; ?>",
      "",
      "country"
    );
 
   <?php foreach($orderProducts as $product) { ?> 
       pageTracker._addItem(
          "<?php echo $product['order_id']; ?>",
          "<?php echo $product['product_id']; ?>",
          "<?php echo $product['name']; ?>",
          "<?php echo $product['model']; ?>", 
          "<?php echo $product['price']+($product['price']*$product['tax']/100); ?>",
          "<?php echo $product['quantity']; ?>"
       );
   <? } ?>
   
   pageTracker._trackTrans();
} catch(err) {}
</script>
<?php } ?>
Note: It's not tested, just something I made awhile back, but it will eventually get you started.. Use on your own risk, and check with google for the correct syntaxes and stuff.. I can see it got updated, so you might want to create some new stuff for the .tpl file ;-)

http://code.google.com/intl/da/apis/ana ... merce.html

Re: Google Analystics Ecomm Tracking

Posted: Mon Aug 23, 2010 7:18 pm
by rockfreak

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']);
That bit right there is exactly what I needed :-) I had got everything else in place was just that last little bit :-)

Thankyou shall let you know if it works!

Re: Google Analystics Ecomm Tracking

Posted: Sat Oct 23, 2010 7:00 am
by ckanoab
rockfreak wrote:

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']);
That bit right there is exactly what I needed :-) I had got everything else in place was just that last little bit :-)

Thankyou shall let you know if it works!
model_checkout_order doesn't seem to have a getOrderProducts method anymore, and the shipping total was missing as well, so i changed it to the following:

Code: Select all

                        $this->load->model('account/order');

                        $this->data['orderDetails'] = $this->model_account_order->getOrder($this->session->data['order_id']);
                        $this->data['orderDetails']['shipping_total'] = $this->session->data['shipping_method']['cost'];
                        $this->data['orderProducts'] = $this->model_account_order->getOrderProducts($this->session->data['order_id']);

Re: Google Analystics Ecomm Tracking

Posted: Wed Jul 25, 2012 4:03 pm
by exXxe
1.5+ version updated code with a bit of OP's code modifications:

1 step:
In the "controller/checkout/success.php" file add this code:

Code: Select all

/*Data for Analytics: START*/
$this->load->model('checkout/order');
$this->data['orderDetails'] = $this->model_checkout_order->getOrder($this->session->data['order_id']);
     foreach ($this->cart->getProducts() as $product) {
          $this->data['orderProducts'][] = $product;
     }
/*Data for Analytics: END*/
Put it between the line:

Code: Select all

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

Code: Select all

$this->cart->clear();
2 step:
In your theme's directory "template/common/success.tpl" file add this code:

Code: Select all

<?php if(isset($orderDetails) && isset($orderProducts)) { ?>
<script type="text/javascript">
  var gaJsHost = (("https:" == document.location.protocol ) ? "https://ssl." : "http://www.");
  document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try{
  var pageTracker = _gat._getTracker("UA-XXXXXXX-XX");
  pageTracker._trackPageview();
  pageTracker._addTrans(
      "<?php echo $orderDetails['order_id']; ?>",
      "ErotinesPrekes.lt",
      "<?php echo $orderDetails['total']; ?>",
      "0",
      "0",
      "<?php echo $orderDetails['shipping_city']; ?>",
      "<?php echo $orderDetails['shipping_zone']; ?>",
      "<?php echo $orderDetails['shipping_country']; ?>"
    );
  <?php foreach($orderProducts as $product) { ?>
       pageTracker._addItem(
          "<?php echo $orderDetails['order_id']; ?>",
          "<?php echo $product['product_id']; ?>",
          "<?php echo $product['name']; ?>",
          "<?php echo $product['model']; ?>",
          "<?php echo $product['price']+($product['price']*(isset($product['tax'])?$product['tax']:0)/100); ?>",
          "<?php echo $product['quantity']; ?>"
       );
   <? } ?>
   pageTracker._trackTrans();
} catch(err) {}
</script>
<?php } ?>
Put it after the line (might differ, pretty much u need "after the header" part):

Code: Select all

<?php echo $header; ?><?php echo $column_left; ?><?php echo $column_right; ?>
Hope this stuff helps someone.

Re: Google Analystics Ecomm Tracking

Posted: Thu Jul 26, 2012 2:52 am
by adzeds
Hey exXxe,

I have tried adding the code you provided to my site and put through some orders but it still does not appear to be tracking...

Any ideas what might be wrong? It is working fine for you?

Re: Google Analystics Ecomm Tracking

Posted: Fri Jul 27, 2012 1:40 am
by moneycarlo
there;s an updated vqmod version of this in the Free Extensions - vqmod section. Not sure how they differ, but you might want to check it out.

Re: Google Analystics Ecomm Tracking

Posted: Fri Jul 27, 2012 2:41 am
by Tcalp
There is a great Analytics extension ($30) that adds all sorts of tracking / add to cart clicks / funnel visualization of checkout / products orders / coversion rates / etc . http://www.opencart.com/index.php?route ... on_id=4496

Re: Google Analystics Ecomm Tracking

Posted: Thu Nov 08, 2012 3:56 am
by v-v
Hi, do someone knows how to replace model with product category?

Many thanks!

Re: Google Analystics Ecomm Tracking

Posted: Sat Nov 17, 2012 2:31 am
by Ed_Rolla
Seems the code isn't fit for OC v.1.5.4.1.
Does anyone have the solution for the last OC version?

Here is what I'm doing:
Step 1: on "catalog/controller/checkout/success.php", I tried all three listed approaches (by dbstr, ckanoab and exXXe) before the line:

Code: Select all

$this->cart->clear();
Step 2: on "template/common/success.tpl", when trying to print the variables values just for testing, something like:

Code: Select all

<?php echo $orderDetails['order_id']; ?>
I get the following error:
Undefined variable: orderDetails in <b>/home/.../template/common/success.tpl</b> on line <b>24</b>
Can anyone give us a hand?

Cheers!

Re: Google Analystics Ecomm Tracking

Posted: Mon Jan 21, 2013 7:00 pm
by ertygi
Ed_Rolla wrote:

Code: Select all

<?php echo $orderDetails['order_id']; ?>
I get the following error:
Undefined variable: orderDetails in <b>/home/.../template/common/success.tpl</b> on line <b>24</b>
Can anyone give us a hand?

Cheers!
I had the same error !! :(
Did you resolve it?

Re: Google Analystics Ecomm Tracking

Posted: Tue Feb 12, 2013 8:07 pm
by MikeSCC
ertygi wrote:
Ed_Rolla wrote:

Code: Select all

<?php echo $orderDetails['order_id']; ?>
I get the following error:
Undefined variable: orderDetails in <b>/home/.../template/common/success.tpl</b> on line <b>24</b>
Can anyone give us a hand?

Cheers!
I had the same error !! :(
Did you resolve it?
Hi,

Was there any solution to the above error?

Thanks,

Mike