Index: /var/www/html/drupal/sites/all/modules/ubercart/uc_googleanalytics/uc_googleanalytics.module
===================================================================
--- /var/www/html/drupal/sites/all/modules/ubercart/uc_googleanalytics/uc_googleanalytics.module (revision 32)
+++ /var/www/html/drupal/sites/all/modules/ubercart/uc_googleanalytics/uc_googleanalytics.module (working copy)
@@ -32,10 +32,14 @@
if ((arg(0) == 'cart' && arg(1) == 'checkout' && arg(2) == 'complete') ||
!empty($checkout_page) && $checkout_page == $_GET['q']) {
if ($order = uc_order_load($_SESSION['uc_googleanalytics_order_id'])) {
- $script .= _uc_googleanalytics_ecommerce_form($order);
+ if (variable_get('googleanalytics_legacy_version', 0)) {
+ $script .= _uc_googleanalytics_ecommerce_form($order);
+ uc_add_js('$(document).ready(function() { __utmSetTrans(); });', 'inline');
+ } else {
+ $script .= _uc_googleanalytics_ecommerce_script($order);
+ }
$_SESSION['uc_googleanalytics_order_id'] = NULL;
unset($_SESSION['uc_googleanalytics_order_id']);
- uc_add_js('$(document).ready(function() { __utmSetTrans(); });', 'inline');
}
}
return $script;
@@ -62,6 +66,9 @@
* Helper Functions
******************************************************************************/
+/**
+ * Track sales using Google Analytics old tracking code.
+ */
function _uc_googleanalytics_ecommerce_form(&$order) {
$script = '';
$UTM_T = '';
@@ -123,3 +130,69 @@
return $script;
}
+
+/**
+ * Track sales using Google Analytics new tracking code.
+ */
+function _uc_googleanalytics_ecommerce_script(&$order) {
+ $script = '';
+ $transaction = '';
+ $items = '';
+ $tax = 0;
+ $shipping_cost = 0;
+
+ // Finding name of country, if not use the country code from ubercart
+ if ($country_data = uc_get_country_data(array("country_id" => $order->billing_country))) {
+ $country = $country_data[0]['country_name'];
+ } else {
+ $country = $order->billing_country;
+ }
+
+ foreach ($order->products as $product) {
+ // Try to find a category (term) for the product
+ // Since products most often only have one category, the first one returned (based on tid) is chosen
+ $terms = taxonomy_node_get_terms($product->nid);
+ if (count($terms)) {
+ $term = array_shift($terms);
+ $category = $term->name;
+ }
+ else {
+ $category = t('No category');
+ }
+ // using the model field as SKU
+ $items .= 'pageTracker._addItem('
+ .'"'. $product->order_id .'", '
+ .'"'. $product->model .'", '
+ .'"'. $product->title .'", '
+ .'"'. $category .'", '
+ .'"'. $product->price .'", '
+ .'"'. $product->qty .'"'
+ .');';
+ }
+
+ foreach ($order->line_items as $line_item) {
+ if ($line_item['type'] == 'tax') {
+ $tax += $line_item['amount'];
+ }
+ if ($line_item['type'] == 'shipping') {
+ $shipping_cost += $line_item['amount'];
+ }
+ }
+
+ $transaction = 'pageTracker._addTrans('
+ .'"'. $order->order_id .'", '
+ .'"'. variable_get('uc_store_name', 'Ubercart') .'", '
+ .'"'. $order->order_total .'", '
+ .'"'. $tax .'", '
+ .'"'. $shipping_cost .'", '
+ .'"'. check_plain($order->billing_city) .'", '
+ .'"'. check_plain($order->billing_postal_code) .'", '
+ .'"'. $country .'"'
+ .');';
+
+ $script .= $transaction;
+ $script .= $items;
+ $script .= 'pageTracker._trackTrans();';
+
+ drupal_add_js($script, 'inline', 'footer');
+}