Post by rockfreak » Mon Aug 23, 2010 6:21 pm

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?

Newbie

Posts

Joined
Tue Aug 10, 2010 5:51 pm

Post by dbstr » Mon Aug 23, 2010 7:07 pm

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

Request Reviews v1.0 released.


Active Member

Posts

Joined
Sun Aug 30, 2009 12:20 am

Post by rockfreak » Mon Aug 23, 2010 7:18 pm

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!

Newbie

Posts

Joined
Tue Aug 10, 2010 5:51 pm

Post by ckanoab » Sat Oct 23, 2010 7:00 am

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

Newbie

Posts

Joined
Wed Jul 22, 2009 11:20 pm

Post by exXxe » Wed Jul 25, 2012 4:03 pm

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.

Newbie

Posts

Joined
Wed Jul 25, 2012 3:48 pm

Post by adzeds » Thu Jul 26, 2012 2:52 am

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?

Newbie

Posts

Joined
Wed May 16, 2012 4:26 am

Post by moneycarlo » Fri Jul 27, 2012 1:40 am

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.

Active Member

Posts

Joined
Wed Sep 28, 2011 3:40 am

Post by Tcalp » Fri Jul 27, 2012 2:41 am

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

Increase Page Speed (#1 rated commercial extension on OpenCart Marketplace)
15in1 Essential Extensions Value Pack Premium Customer Testimonials Reward Points Extended Admin Security Lockdown Suite

Image
irc.freenode.net #opencart


User avatar
Active Member

Posts

Joined
Wed Jul 06, 2011 1:49 pm

Post by v-v » Thu Nov 08, 2012 3:56 am

Hi, do someone knows how to replace model with product category?

Many thanks!

v-v
Active Member

Posts

Joined
Wed Jun 16, 2010 10:09 pm

Post by Ed_Rolla » Sat Nov 17, 2012 2:31 am

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!

WIP Design Publicidade e Marketing Digital


User avatar
New member

Posts

Joined
Sun Jul 15, 2012 12:16 am
Location - Brazil, Rio de Janeiro

Post by ertygi » Mon Jan 21, 2013 7:00 pm

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?

Web Developer / SEO


Newbie

Posts

Joined
Mon Jan 21, 2013 6:52 pm

Post by MikeSCC » Tue Feb 12, 2013 8:07 pm

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

Active Member

Posts

Joined
Tue Nov 20, 2012 5:55 pm
Who is online

Users browsing this forum: No registered users and 45 guests