Index: googleanalytics.admin.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/google_analytics/googleanalytics.admin.inc,v
retrieving revision 1.13.2.7
diff -u -r1.13.2.7 googleanalytics.admin.inc
--- googleanalytics.admin.inc	15 Aug 2010 16:54:48 -0000	1.13.2.7
+++ googleanalytics.admin.inc	30 Sep 2010 00:21:46 -0000
@@ -270,6 +270,11 @@
     form_set_error('googleanalytics_account', t('A valid Google Analytics account number is case sensitive and formatted like UA-xxxxxx-x.'));
   }
 
+  // Delete obsolete local cache file.
+  if (empty($form_state['values']['googleanalytics_cache'])) {
+    googleanalytics_clear_js_cache();
+  }
+
   // This is for the Newbie's who cannot read a text area description.
   if (stristr($form_state['values']['googleanalytics_codesnippet_before'], 'google-analytics.com/ga.js')) {
     form_set_error('googleanalytics_codesnippet_before', t('Do not add the tracker code provided by Google into the javascript code snippets! This module already builds the tracker code based on your Google Analytics account number and settings.'));
Index: googleanalytics.install
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/google_analytics/googleanalytics.install,v
retrieving revision 1.13.2.5
diff -u -r1.13.2.5 googleanalytics.install
--- googleanalytics.install	28 Aug 2010 18:17:09 -0000	1.13.2.5
+++ googleanalytics.install	30 Sep 2010 00:21:46 -0000
@@ -50,11 +50,7 @@
  * Remove cache directory if module is disabled (or uninstalled).
  */
 function googleanalytics_disable() {
-  $path = file_directory_path() .'/googleanalytics';
-  if (file_exists($path)) {
-    file_delete($path .'/ga.js');
-    rmdir($path);
-  }
+  googleanalytics_clear_js_cache();
 }
 
 /**
Index: googleanalytics.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/google_analytics/googleanalytics.module,v
retrieving revision 1.36.2.12
diff -u -r1.36.2.12 googleanalytics.module
--- googleanalytics.module	14 Sep 2010 21:52:35 -0000	1.36.2.12
+++ googleanalytics.module	30 Sep 2010 00:21:46 -0000
@@ -226,12 +226,31 @@
 function googleanalytics_cron() {
   // Regenerate the google analytics ga.js every day.
   if (time() - variable_get('googleanalytics_last_cache', 0) >= 86400) {
-    // 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();
+    $directory = file_create_path('googleanalytics');
+    $file_destination = $directory .'/'. basename($location);
+
+    if (variable_get('googleanalytics_cache', 0)) {
+      // Download the latest ga.js code and replace local file if outdated.
+      $result = drupal_http_request('http://www.google-analytics.com/ga.js');
+
+      if ($result->code == 200 && file_exists($file_destination)) {
+        $data_hash_local = md5(file_get_contents($file_destination));
+        $data_hash_remote = md5($result->data);
+        // Check that the files directory is writable
+        if ($data_hash_local != $data_hash_remote && file_check_directory($directory, FILE_CREATE_DIRECTORY)) {
+          // Save updated ga.js file to disk.
+          file_save_data($result->data, $file_destination, FILE_EXISTS_REPLACE);
+
+          // Change query-strings on css/js files to enforce reload for all users.
+          _drupal_flush_css_js();
+
+          // Clear aggregated JS files.
+          if (variable_get('preprocess_js', 0)) {
+            drupal_clear_js_cache();
+          }
+        }
+      }
     }
 
     variable_set('googleanalytics_last_cache', time());
@@ -263,6 +282,21 @@
 }
 
 /**
+ * Delete cached ga.js file and directory.
+ */
+function googleanalytics_clear_js_cache() {
+  $directory = file_create_path('googleanalytics');
+  file_scan_directory($directory, '.*', array('.', '..', 'CVS'), 'file_delete', TRUE);
+  rmdir($directory);
+  _drupal_flush_css_js();
+
+  // Clear aggregated JS files.
+  if (variable_get('preprocess_js', 0)) {
+    drupal_clear_js_cache();
+  }
+}
+
+/**
  * Tracking visibility check for an user object.
  *
  * @param $account
