### Eclipse Workspace Patch 1.0
#P drupal-contrib-6--1
Index: modules/google_analytics/googleanalytics.admin.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/google_analytics/googleanalytics.admin.inc,v
retrieving revision 1.2.2.12
diff -u -r1.2.2.12 googleanalytics.admin.inc
--- modules/google_analytics/googleanalytics.admin.inc	15 Feb 2008 19:20:40 -0000	1.2.2.12
+++ modules/google_analytics/googleanalytics.admin.inc	16 Feb 2008 12:31:37 -0000
@@ -142,5 +142,16 @@
     '#description' => t('Paste <a href="!snippets">custom code snippets here</a>. These will be added to every page that Google Analytics appears on. For help with this feature see the <a href="!blog">cutroni.com blog</a>. <strong>Do not include the &lt;script&gt; tags</strong>, and always end your code with a semicolon (;).', array('!snippets' => 'http://drupal.org/node/39282', '!blog' => 'http://cutroni.com/blog/'))
   );
 
+  $form['advanced']['googleanalytics_js_scope'] = array(
+    '#type' => 'select',
+    '#title' => t('JavaScript scope'),
+    '#description' => t("<strong>Warning:</strong> Google recommends adding the external JavaScript files to footer for performance reasons."),
+    '#options' => array(
+      'footer' => t('Footer'),
+      'header' => t('Header'),
+    ),
+    '#default_value' => variable_get('googleanalytics_js_scope', 'footer'),
+  );
+
   return system_settings_form($form);
 }
Index: modules/google_analytics/googleanalytics.install
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/google_analytics/googleanalytics.install,v
retrieving revision 1.4.2.6
diff -u -r1.4.2.6 googleanalytics.install
--- modules/google_analytics/googleanalytics.install	9 Feb 2008 20:42:39 -0000	1.4.2.6
+++ modules/google_analytics/googleanalytics.install	16 Feb 2008 12:31:37 -0000
@@ -26,6 +26,7 @@
   variable_del('googleanalytics_cache');
   variable_del('googleanalytics_last_cache');
   variable_del('googleanalytics_site_search');
+  variable_del('googleanalytics_js_scope');
 }
 
 
Index: modules/google_analytics/googleanalytics.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/google_analytics/googleanalytics.module,v
retrieving revision 1.16.2.17
diff -u -r1.16.2.17 googleanalytics.module
--- modules/google_analytics/googleanalytics.module	15 Feb 2008 19:44:40 -0000	1.16.2.17
+++ modules/google_analytics/googleanalytics.module	16 Feb 2008 12:31:37 -0000
@@ -37,14 +37,33 @@
 
   // Use the old version of Google Analytics?
   $legacy_version = variable_get('googleanalytics_legacy_version', FALSE);
+  $scope = variable_get('googleanalytics_js_scope', 'footer');
+
+  // Are we on a secure page?
+  $prefix = '://www';
+  if (isset($_SERVER['HTTPS']) && !empty($_SERVER['HTTPS']) && (strtolower($_SERVER['HTTPS']) != 'off')) {
+    $prefix = 's://ssl';
+  }
+
+  // Should a local cached copy of urchin.js or ga.js be used?
+  $js_file = ($legacy_version) ? 'urchin.js' : 'ga.js';
+  $url = 'http'. $prefix .'.google-analytics.com/'. $js_file;
+
+  if (variable_get('googleanalytics_cache', 0) && (variable_get('file_downloads', FILE_DOWNLOADS_PUBLIC) == FILE_DOWNLOADS_PUBLIC) && $source = _googleanalytics_cache($url)) {
+    drupal_add_js($source, 'module', $scope);
+  }
+  else {
+    $script .= 'document.write(unescape("%3Cscript src=\''. $source .'\' type=\'text/javascript\'%3E%3C/script%3E"));';
+    drupal_add_js($script, 'inline', $scope);
+  }
 
   // Add download tracking.
   $path = drupal_get_path('module', 'googleanalytics');
   if ($trackfiles = variable_get('googleanalytics_trackfiles', GA_TRACKFILES)) {
     drupal_add_js(array('googleanalytics' => array('trackDownload' => $trackfiles, 'LegacyVersion' => $legacy_version)), 'setting', 'header');
-    drupal_add_js($path .'/downloadtracker.js', 'module', 'footer');
+    drupal_add_js($path .'/downloadtracker.js', 'module', $scope);
   }
-
+  
 }
 
 /**
@@ -61,13 +80,6 @@
   // Don't track page views in the admin sections
   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')) {
-      $prefix = 's://ssl';
-    }
-
     // Use the old version of Google Analytics?
     $legacy_version = variable_get('googleanalytics_legacy_version', FALSE);
 
@@ -102,27 +114,6 @@
     // Add any custom code snippets if specified.
     $codesnippet = variable_get('googleanalytics_codesnippet', '');
 
-    // Should a local cached copy of urchin.js or ga.js be used?
-    if (variable_get('googleanalytics_cache', 0) && (variable_get('file_downloads', FILE_DOWNLOADS_PUBLIC) == FILE_DOWNLOADS_PUBLIC)) {
-      if ($legacy_version) {
-        $source = _googleanalytics_cache('http://www.google-analytics.com/urchin.js');
-      }
-      else {
-        $source = _googleanalytics_cache('http://www.google-analytics.com/ga.js');
-      }
-      if (!empty($source)) {
-        $source = base_path() . $source;
-      }
-    }
-    if (!isset($source)) {
-      if ($legacy_version) {
-        $source = 'http'. $prefix .'.google-analytics.com/urchin.js';
-      }
-      else {
-        $source = 'http'. $prefix .'.google-analytics.com/ga.js';
-      }
-    }
-
     // Site search tracking support.
     $url_custom = '';
     if (module_exists('search') && variable_get('googleanalytics_site_search', FALSE) && arg(0) == 'search') {
@@ -136,9 +127,8 @@
     }
 
     // Should the legacy code be used?
+    $script = '';
     if ($legacy_version) {
-      $script = '<script type="text/javascript" src="'. $source ."\"></script>\n";
-      $script .= '<script type="text/javascript">';
       $script .= '_uacct = '. drupal_to_js($id) .';';
       if (!empty($segmentation)) {
         $script .= $segmentation;
@@ -147,13 +137,8 @@
         $script .= $codesnippet;
       }
       $script .= 'urchinTracker('. $url_custom .');';
-      $script .= "</script>\n";
     }
     else {
-      $script = '<script type="text/javascript">';
-      $script .= 'document.write(unescape("%3Cscript src=\''. $source .'\' type=\'text/javascript\'%3E%3C/script%3E"));';
-      $script .= "</script>\n";
-      $script .= '<script type="text/javascript">';
       $script .= 'var pageTracker = _gat._getTracker('. drupal_to_js($id) .');';
       $script .= 'pageTracker._initData();';
       if (!empty($segmentation)) {
@@ -163,10 +148,9 @@
         $script .= $codesnippet;
       }
       $script .= 'pageTracker._trackPageview('. $url_custom .');';
-      $script .= "</script>\n";
     }
 
-    return $script;
+    drupal_add_js($script, 'inline', 'footer');
   }
 }
 
@@ -202,6 +186,12 @@
 
     // New google analytics version.
     file_delete(file_directory_path() .'/googleanalytics/ga.js');
+
+    // Clear aggregated JS files.
+    if (variable_get('preprocess_js', 0)) {
+      drupal_clear_js_cache();
+    }
+
     variable_set('googleanalytics_last_cache', time());
   }
 }
