Index: README.txt
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/google_analytics/README.txt,v
retrieving revision 1.2.4.1
diff -U3 -r1.2.4.1 README.txt
--- README.txt	23 Apr 2007 16:18:01 -0000	1.2.4.1
+++ README.txt	19 May 2007 08:14:14 -0000
@@ -1,10 +1,15 @@
 Module: Google Analytics
 Author: Mike Carter <www.ixis.co.uk/contact>
+        Bruno Massa http://drupal.org/user/67164
 
 
 Description
 ===========
 Adds the Google Analytics tracking system to your website.
+You can select which roles will be tracked. All admin pages
+are not tracked, by default.
+It also can track your ecommerce transactions if E-Commerce
+modules is enabled.
 
 Requirements
 ============
Index: googleanalytics.install
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/google_analytics/googleanalytics.install,v
retrieving revision 1.2
diff -U3 -r1.2 googleanalytics.install
--- googleanalytics.install	21 Nov 2006 14:17:54 -0000	1.2
+++ googleanalytics.install	19 May 2007 08:14:14 -0000
@@ -5,13 +5,34 @@
   $result = db_query('SELECT * FROM {role} ORDER BY name');
 
   while ($role = db_fetch_object($result)) {
-    $role_varname = $string = str_replace(" ", "_", $role->name);
-    $ga_role = "googleanalytics_track_{$role_varname}";
+    $ga_role = "googleanalytics_track_".  $role->rid;
 
     //check if variable is not already set from a previous install
     if(strpos(variable_get($ga_role, 'new'), 'new') !== FALSE) {
-      variable_set($ga_role, TRUE);
+      variable_set($ga_role, FALSE);
       drupal_set_message(t('Role %rolename is now being tracked by Google Analytics', array('%rolename' => $role->name)));
     }
   }
-}
\ No newline at end of file
+}
+
+function googleanalytics_uninstall() {
+  $result = db_query("SELECT rid FROM {role}");
+  while ($role = db_fetch_object($result)) {
+    variable_del("googleanalytics_track_".  $role->rid);
+  }
+  variable_del("googleanalytics_account");
+  variable_del("googleanalytics_codesnippet");
+  variable_del("googleanalytics_filetypes");
+  variable_del("googleanalytics_segmentation");
+  variable_del("googleanalytics_track__user1");
+}
+
+function googleanalytics_update_1() {
+  $result = db_query("SELECT * FROM {role}");
+  while ($role = db_fetch_object($result)) {
+     // can't use empty spaces in varname
+    $role_varname = str_replace(" ", "_", $role->name);
+    variable_set("googleanalytics_track_".  $role->rid,   !variable_get("googleanalytics_track_{$role_varname}", FALSE));
+    variable_del("googleanalytics_track_{$role_varname}");
+  }
+  variable_set("googleanalytics_track__user1", TRUE);
Index: googleanalytics.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/google_analytics/googleanalytics.module,v
retrieving revision 1.14.2.4
diff -U3 -r1.14.2.4 googleanalytics.module
--- googleanalytics.module	13 May 2007 17:05:17 -0000	1.14.2.4
+++ googleanalytics.module	19 May 2007 08:14:14 -0000
@@ -1,13 +1,64 @@
 <?php
 // $Id: googleanalytics.module,v 1.14.2.3 2007/05/03 10:58:17 budda Exp $
-/*
+/**
  * Drupal Module: GoogleAnalytics
  * Adds the required Javascript to the bottom of all your Drupal pages
  * to allow tracking by the Google Analytics statistics package.
  *
  * @author: Mike Carter <www.ixis.co.uk/contact>
+ * @author: Bruno Massa http://drupal.org/user/67164
  */
 
+/**
+ * Implementation of of hook_form_alter().
+ *
+ * Google Analytics can track ecommerce transactions. We need to
+ * create a special field with all data about each transaction
+ *
+ * GA needs that each transaction should have a unique id. Drupal
+ * only creates the txnid when the order has been confirmed.
+ * So we use time() . cart_get_id() to generate a unique id.
+ * Therefore, it will not be the same as the Drupal ECommerce Transaction ID
+ *
+ * @param &$form_id
+ *  string, the form id. we need only to change form called "checkout_review_form"
+ * @param &$form
+ *  array, all existing form fields
+ */
+function googleanalytics_form_alter(&$form_id, &$form) {
+  if ($form_id == "checkout_review_form") {
+    $txn = $form["txn"]["#value"];
+    $ga_ecommerce_item = array();
+    array_push($ga_ecommerce_item, preg_replace("/[,\]\[]/","", "UTM:T|".
+      time() . cart_get_id() ."|".          // Transaction ID or any unique code
+      "|".                                  // Optional partner or store affilation
+      $txn->gross ."|".                     // Total amount of the transaction
+      "|".                                  // Tax amount of the transaction
+      "|".                                  // The shipping amount of the transaction
+      $txn->address["billing"]->city ."|".  //
+      $txn->address["billing"]->state ."|". // billing address, if exists
+      $txn->address["billing"]->country));  //
+    foreach ($txn->items as $item) {
+      array_push($ga_ecommerce_item, preg_replace("/[,\]\[]/","", "UTM:I|".
+        time() . cart_get_id() ."|".        // Transaction ID or any unique code
+        $item->sku ."|".                    // Product SKU code
+        $item->title ."|".                  // Product name or description
+        "|".                                // Category of the product or variation
+        $item->price ."|".                  // Unit-price of the product
+        $item->qty));                       // Quantity ordered
+    }
+    drupal_add_js("ga_ecommerce = true;", "inline");
+    $form["utmform"] = array(               // the form used by google analytics to track transactions
+      "#type"           => "form",
+      "#attributes"     => array("name" => "utmform")
+    );
+    $form["utmform"]["utmtrans"] = array(
+      "#type"           => "textarea",
+      "#default_value"  => implode("\n", $ga_ecommerce_item),
+      "#id"             => "utmtrans"
+    );
+  }
+} // googleanalytics_form_alter()
 
 function googleanalytics_help($section) {
   switch ($section) {
@@ -43,19 +94,19 @@
   $id = variable_get('googleanalytics_account', '');
 
   // Check if we should track the currently active user's role
-  $track = 0;
-  foreach($user->roles as $role) {
-    $role = str_replace(' ', '_', $role);
-    $track += variable_get("googleanalytics_track_{$role}", FALSE);
+  $track = TRUE;
+  foreach(array_keys($user->roles) as $role) {
+    if (variable_get("googleanalytics_track_". $role, FALSE)) {$track = FALSE;}
   }
+  if ($user->uid == 1 and variable_get("googleanalytics_track__user1", FALSE)) {$track = FALSE;}
 
   // Don't track page views in the admin sections
-  if($id && (arg(0) != 'admin') && $track > 0) {
+  if($id && (arg(0) != 'admin') && $track == TRUE) {
 
     $prefix = '://www';
 
     // Are we on a secure page?
-    if(isset($_SERVER['HTTPS']) && !empty($_SERVER['HTTPS']) && (strtolower($_SERVER['HTTPS']) != 'off')) {
+    if($_SERVER['HTTPS']) {
       $prefix = 's://ssl';
     }
 
@@ -74,15 +125,23 @@
 
         $fields[$field] = $value;
       }
-      $segmentation = "__utmSetVar('".implode(':', $fields)."');";
 		}
 
 		// Add any custom code snippets if specified
 		$codesnippet = variable_get('googleanalytics_codesnippet', '');
-
-    $script = '<script type="text/javascript" src="http' . $prefix . ".google-analytics.com/urchin.js\"></script>\n";
-    $script .= '<script type="text/javascript">' . "<!--\n_uacct = \"".$id."\";{$segmentation}{$codesnippet}urchinTracker();\n// --></script><noscript></noscript>\n";
-    return $script;
+    // Include the GoogleAnalytics javascript
+    // since drupal_add_js dont allow external files, the old style is used
+    drupal_set_html_head("<script type='text/javascript' src='http". $prefix .".google-analytics.com/urchin.js'></script>");
+    drupal_add_js(drupal_get_path("module", "googleanalytics") ."/googleanalytics.js");
+    // Include variables needed
+    drupal_add_js("_uacct = \"". $id .
+      "\"; ga_basepath = \"". base_path() .
+      "\"; ga_filetypes = \"". variable_get("googleanalytics_filetypes", "pdf|doc|xls|ppt|jpeg|jpg|gif|png|mp3|ogg|avi|mpg|rm|wav") ."\";", "inline");
+    if (!empty($fields)) {drupal_add_js("__utmSetVar=\"". implode(":", $fields) ."\";", "inline");}
+    if (!empty($codesnippet)) { // Custom Snippet
+      return "<script type='text/javascript'><!--\n{$codesnippet}\n// --></script><noscript></noscript>\n";
+    }
+    return;
   }
 }
 
@@ -113,16 +172,19 @@
         '#type' => 'fieldset',
         '#title' => t('User Role Tracking'),
         '#collapsible' => TRUE,
-        '#description' => t('Define what user roles should be tracked by Google Analytics.')
+        '#description' => t('Define what user roles should not be tracked by Google Analytics.')
+  );
+  $form['roles']["googleanalytics_track__user1"] = array(
+    '#type' => 'checkbox',
+    '#title' => t("Admin (user 1)"),
+    '#default_value' => variable_get("googleanalytics_track__user1", TRUE)
   );
 
   while ($role = db_fetch_object($result)) {
-     // can't use empty spaces in varname
-    $role_varname = $string = str_replace(' ', '_', $role->name);
-    $form['roles']["googleanalytics_track_{$role_varname}"] = array(
+    $form['roles']["googleanalytics_track_". $role->rid] = array(
       '#type' => 'checkbox',
       '#title' => t($role->name),
-      '#default_value' => variable_get("googleanalytics_track_{$role_varname}", FALSE),
+      '#default_value' => variable_get("googleanalytics_track_". $role->rid, FALSE),
     );
   }
 
@@ -132,7 +194,12 @@
         '#collapsible' => TRUE,
         '#description' => t('If your users have profile fields completed, you can track your logged in users based on a defined profile field.')
   );
-
+  $form["filetypes"] = array(
+        "#type"           => "fieldset",
+        "#title"          => t("Track Downloads"),
+        "#default_value"  => variable_get("googleanalytics_filetypes", "pdf|doc|xls|ppt|jpeg|jpg|gif|png|mp3|ogg|avi|mpg|rm|wav"),
+        "#description"    => t("GoogleAnalytics has the ability to track downloads from your side. List here all types os files (separated by | ).")
+  );
   if(!module_exists('profile')) {
     $form['segmentation']['profile'] = array(
       '#type' => 'markup',
