diff --git googleanalytics.module googleanalytics.module
index 62c888e..16d6f59 100644
--- googleanalytics.module
+++ googleanalytics.module
@@ -66,7 +66,12 @@ function googleanalytics_menu() {
     'type' => MENU_NORMAL_ITEM,
     'file' => 'googleanalytics.admin.inc',
   );
-
+  $items[variable_get('file_public_path', conf_path() . '/files') . '/googleanalytics/ga.js'] = array(
+    'title' => "Fetches, writes and serves google analytics JavaScript",
+    'page callback' => 'googleanalytics_cache_js',
+    'access callback' => TRUE,
+    'type' => MENU_CALLBACK,
+  );
   return $items;
 }
 
@@ -261,14 +266,21 @@ function googleanalytics_page_alter(&$page) {
     $script .= 'ga.async = true;';
 
     // Should a local cached copy of ga.js be used?
-    if (variable_get('googleanalytics_cache', 0) && $url = _googleanalytics_cache('http://www.google-analytics.com/ga.js')) {
-      // A dummy query-string is added to filenames, to gain control over
-      // browser-caching. The string changes on every update or full cache
-      // flush, forcing browsers to load a new copy of the files, as the
-      // URL changed.
-      $query_string = '?' . variable_get('css_js_query_string', '0');
-
-      $script .= 'ga.src = "' . $url . $query_string . '";';
+    if (variable_get('googleanalytics_cache', 0)) {
+      $location = 'http://www.google-analytics.com/ga.js';
+      if (variable_get('clean_url', 0)) {
+        // If the cached js doesn't exist, it will always be available
+        // due to the menu callback.
+        $path = 'public://googleanalytics';
+        $file_destination = $path . '/' . basename($location);
+        $url = file_create_url($file_destination);
+      }
+      else {
+        // However if clean URLs are not available, the menu callback and
+        // file location will be different, so fall back to generating the
+        // file inline if it doesn't already exist.
+        $url = _googleanalytics_cache($location);
+      }
     }
     else {
       $script .= 'ga.src = ("https:" == document.location.protocol ? "https://ssl" : "http://www") + ".google-analytics.com/ga.js";';
@@ -341,6 +353,36 @@ function googleanalytics_cron() {
 }
 
 /**
+ * Menu callback to fetch and cache google analytics js if it doesn't already exist.
+ */
+function googleanalytics_cache_js() {
+  $location = 'http://www.google-analytics.com/ga.js';
+  $basename = basename($location);
+  $path = 'public://googleanalytics';
+  $file_destination = $path . '/' . $basename;
+  if (!file_exists($file_destination)) {
+    _googleanalytics_cache($location);
+  }
+  $js = file_get_contents($file_destination);
+  $headers = array();
+  $headers['Content-Type'] = 'application/javascript';
+  // Ensure this file can be cached by browsers and reverse proxies.
+  $headers['Cache-Control'] = 'public, max-age=1209600';
+  // Since the file name is based on a hash of file contents, there is no
+  // problem allowing the browser to cache it for as long as possible.
+  // Set this to two weeks since that's what .htaccess does if mod_expires
+  // is enabled.
+  $headers['Expires'] = gmdate(DATE_RFC1123, REQUEST_TIME + 1209600);
+
+  // When possible, serve the gzip version of the file via PHP.
+  foreach ($headers as $key => $value) {
+    drupal_add_http_header($key, $value);
+  }
+  print $js;
+  drupal_exit();
+}
+
+/**
  * Download/Synchronize/Cache tracking code file locally.
  *
  * @param $location
