Google Analytics - Transaction Tracking in Virtuemart

  • PDF

Tracking Virtuemart Transactions in Google Analytics

An important part of every ecommerce website is being able to track the purchases made on the website.  Using Google Analytics to do this provides the ability to view the reports on purchases tied to the other reports in Google Analytics, including sources of visits and specific advertising campaigns.  The benefits of understanding what is driving your sales is incredibly valuable.

Steps to adding Google Analytics:

  1. Add Google Analytics to the Site
  2. Include Transaction and Item Tracking
  3. Confirm on GA

Add Google Analytics to the Site

Add tracking to the main website - you can include the standard tracking code before the </head> tag:

<script type="text/javascript">
 var _gaq = _gaq || [];
 _gaq.push(['_setAccount', 'UA-xxxxxxxx-x']);
 _gaq.push(['_trackPageview']);
 
 (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>
 

An alternative is to use the Web Guru Analytics plugin available at this link:

webguru-co.com/downloads/webguru-google-analytics-v.0.9.zip

Once this is done we are ready for the next step.

Include eCommerce Tracking

Tracking Transactions in Virtuemart

There are two parts to adding transation and item tracking to our website.  One step is learning how Google Analytics tracks eCommerce - using transactions and items.  We also have to find where the order has been processed in Virtuemart so we can send confirmed order information to Google Analytics.

This link provides the information about adding transactions and items to your code:

http://code.google.com/apis/analytics/docs/gaJS/gaJSApiEcommerce.html#_gat.GA_Tracker_._addTrans

Example code looks like this:

    _gaq.push(['_setAccount', 'UA-XXXXX-X']);
    _gaq.push(['_trackPageview']);
    _gaq.push(['_addTrans',
       '1234',          // order ID - required
       'Womens Apparel',           // affiliation or store name
       '28.28',          // total - required
       '1.29',           // tax
       '15.00',         // shipping
       'San Jose',       // city
       'California',     // state or province
       'USA'             // country
    ]);
    _gaq.push(['_addItem',
       '1234',           // order ID - necessary to associate item with transaction
       'DD44',           // SKU/code - required
       'T-Shirt',        // product name
       'Olive Medium',   // category or variation
       '11.99',          // unit price - required
       '1'               // quantity - required
    ]);
    _gaq.push(['_trackTrans']);

Where will we put this in our Virtuemart code?  The code in the administrator/components/com_virtuemart/html/checkout.result.php is where the return value for the payment methods is received. 

Our updated code will now look like this:

$q = "SELECT order_status, order_number, order_total, order_tax, order_shipping FROM #__{vm}_orders WHERE ";
 $q .= "#__{vm}_orders.user_id= " . $auth["user_id"] . " ";
 $q .= "AND #__{vm}_orders.order_id= $order_id ";
 

First we create a query with all the parameters we need for creating a transaction.  This will be retrieved from the database and converted to a JavaScript call to the Google Analytics _addTrans function.

 $db->query($q);
 if ($db->next_record()) {
 $order_status = $db->f("order_status");
 $order_number = $db->f("order_number");
 $order_total = $db->f("order_total");
 $order_tax = $db->f("order_tax");
 $order_shipping = $db->f("order_shipping");
 
 if($order_status == PAYPAL_VERIFIED_STATUS
 || $order_status == PAYPAL_PENDING_STATUS) {  
 
// Create a JavaScript string to add a transaction - use order number as the unique field.
 $myTagString = "
 <script type='text/javascript'>
 var _gaq = _gaq || []; 
 _gaq.push(['_addTrans',
 '$order_number',           // order ID - required
 'My Fake Store',        // affiliation or store name
 '$order_total',          // total - required
 '$order_tax',           // tax
 '$order_shipping',        // shipping
'',                   // city
 '',                  // state or province
''                  // country
<pre> ]);";
 

Now we need to select the order information - this can be one or more item lines from the _vm_order_item table.  The query and while loop below creates the JavaScript to add the items to the Google Analytics transaction.


// get multiple items here
 $q2 = "SELECT * FROM #__{vm}_order_item WHERE #__{vm}_order_item.order_id= $order_id";
 $db->query($q2);
 while ($db->next_record()) {
 $item_sku = $db->f("order_item_sku");
 $item_name = $db->f("order_item_name");
 $item_price = $db->f("product_final_price");
 $item_quantity = $db->f("product_quantity");
 $myTagString .= "
 _gaq.push(['_addItem',
 '$order_number',           // order ID - necessary to associate item with transaction
 '$item_sku',           // SKU/code - required
 '$item_name',        // product name
 '',   // category or variation
 '$item_price',          // unit price - required
 '$item_quantity'               // quantity - required
 ]);";
 
 }
$myTagString .= "
 _gaq.push(['_trackTrans']);
 
 </script>
 ";
 
 $document = &JFactory::getDocument();
 $document->addCustomTag($myTagString);
  ?> 
 <img src="<?php echo VM_THEMEURL ?>images/button_ok.png" align="middle" 
alt="<?php echo $VM_LANG->_('VM_CHECKOUT_SUCCESS'); ?>" border="0" />
 <h2><?php echo $VM_LANG->_('PHPSHOP_PAYPAL_THANKYOU') ?></h2>
  <?php
 }
 else { ?>
 <img src="<?php echo VM_THEMEURL ?>images/button_cancel.png" align="middle" 
alt="<?php echo $VM_LANG->_('VM_CHECKOUT_FAILURE'); ?>" border="0" />
 
 <?php echo $VM_LANG->_('PHPSHOP_PAYPAL_ERROR') ?>
  <?php
 } ?>
 


Once this code is in place you can track all your Virtuemart transactions in Google Analytics.  Happy shopping!