diff --git a/googleanalytics.js b/googleanalytics.js
index db63aae..5770bac 100644
--- a/googleanalytics.js
+++ b/googleanalytics.js
@@ -15,32 +15,29 @@ $(document).ready(function() {
       // Expression to check for download links.
       var isDownload = new RegExp("\\.(" + ga.trackDownloadExtensions + ")$", "i");
 
-      try {
-        // Is the clicked URL internal?
-        if (isInternal.test(this.href)) {
-          // Is download tracking activated and the file extension configured for download tracking?
-          if (ga.trackDownload && isDownload.test(this.href)) {
-            // Download link clicked.
-            var extension = isDownload.exec(this.href);
-            pageTracker._trackEvent("Downloads", extension[1].toUpperCase(), this.href.replace(isInternal, ''));
-          }
-          else if (isInternalSpecial.test(this.href)) {
-            // Keep the internal URL for Google Analytics website overlay intact.
-            pageTracker._trackPageview(this.href.replace(isInternal, ''));
-          }
+      // Is the clicked URL internal?
+      if (isInternal.test(this.href)) {
+        // Is download tracking activated and the file extension configured for download tracking?
+        if (ga.trackDownload && isDownload.test(this.href)) {
+          // Download link clicked.
+          var extension = isDownload.exec(this.href);
+          _gaq.push(["_trackEvent", "Downloads", extension[1].toUpperCase(), this.href.replace(isInternal, '')]);
         }
-        else {
-          if (ga.trackMailto && $(this).is("a[href^=mailto:],area[href^=mailto:]")) {
-            // Mailto link clicked.
-            pageTracker._trackEvent("Mails", "Click", this.href.substring(7));
-          }
-          else if (ga.trackOutgoing) {
-            // External link clicked.
-            pageTracker._trackEvent("Outgoing links", "Click", this.href);
-          }
+        else if (isInternalSpecial.test(this.href)) {
+          // Keep the internal URL for Google Analytics website overlay intact.
+          _gaq.push(["_trackPageview", this.href.replace(isInternal, '')]);
         }
-      } catch(err) {}
-
+      }
+      else {
+        if (ga.trackMailto && $(this).is("a[href^=mailto:],area[href^=mailto:]")) {
+          // Mailto link clicked.
+          _gaq.push(["_trackEvent", "Mails", "Click", this.href.substring(7)]);
+        }
+        else if (ga.trackOutgoing) {
+          // External link clicked.
+          _gaq.push(["_trackEvent", "Outgoing links", "Click", this.href]);
+        }
+      }
     });
   });
 });
diff --git a/googleanalytics.module b/googleanalytics.module
index c690ff2..b9174b6 100644
--- a/googleanalytics.module
+++ b/googleanalytics.module
@@ -46,20 +46,12 @@ function googleanalytics_init() {
   // 2. Track page views based on visibility value.
   // 3. Check if we should track the currently active user's role.
   if (!empty($id) && _googleanalytics_visibility_pages() && _googleanalytics_visibility_user($user)) {
-    $scope = variable_get('googleanalytics_js_scope', 'footer');
 
-    // Should a local cached copy of ga.js be used?
-    $js_file = 'ga.js';
-    $url = 'http://www.google-analytics.com/'. $js_file;
+    // We allow different scopes. Default to footer but allow user to overide if they really need to.
+    $scope = variable_get('googleanalytics_js_scope', 'footer');
 
-    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 = 'var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");';
-      $script .= 'document.write(unescape("%3Cscript src=\'" + gaJsHost + "google-analytics.com/'. $js_file .'\' type=\'text/javascript\'%3E%3C/script%3E"));';
-      drupal_add_js($script, 'inline', $scope);
-    }
+    $script = "var _gaq = _gaq || [];\n";
+    $script .= "_gaq.push(['_setAccount', " . drupal_to_js($id) . "]);\n";
 
     // Add link tracking.
     $link_settings = array();
@@ -75,25 +67,16 @@ function googleanalytics_init() {
     }
     if (!empty($link_settings)) {
       drupal_add_js(array('googleanalytics' => $link_settings), 'setting', 'header');
-      drupal_add_js(drupal_get_path('module', 'googleanalytics') .'/googleanalytics.js', 'module', $scope);
+      drupal_add_js(drupal_get_path('module', 'googleanalytics') . '/googleanalytics.js', 'module', $scope);
     }
 
     // Custom tracking.
     if (variable_get('googleanalytics_trackadsense', FALSE)) {
       drupal_add_js('window.google_analytics_uacct = ' . drupal_to_js($id) . ';', 'inline', 'header');
     }
-  }
-}
-
-/**
- * Implementation of hook_footer() to insert Javascript at the end of the page.
- */
-function googleanalytics_footer($main = 0) {
-  global $user;
-
-  $id = variable_get('googleanalytics_account', '');
 
-  if (!empty($id) && _googleanalytics_visibility_pages() && _googleanalytics_visibility_user($user)) {
+    // Add any custom code snippets if specified.
+    $script .= variable_get('googleanalytics_codesnippet_before', '');
 
     // Add User profile segmentation values.
     if (is_array($profile_fields = variable_get('googleanalytics_segmentation', '')) && ($user->uid > 0)) {
@@ -112,9 +95,8 @@ function googleanalytics_footer($main = 0) {
       }
 
       // Only show segmentation variable if there are specified fields.
-      $segmentation = '';
       if (count($fields) > 0) {
-        $segmentation = 'pageTracker._setVar('. drupal_to_js(implode(':', $fields)) .');';
+        $script .= "_gaq.push(['_setVar', " . drupal_to_js(implode(':', $fields)) . "]);\n";
       }
     }
 
@@ -149,26 +131,37 @@ function googleanalytics_footer($main = 0) {
       }
     }
 
-    // Add any custom code snippets if specified.
-    $codesnippet_before = variable_get('googleanalytics_codesnippet_before', '');
-    $codesnippet_after = variable_get('googleanalytics_codesnippet_after', '');
+    if (empty($url_custom)) {
+      $script .= "_gaq.push(['_trackPageview']);\n";
+    }
+    else {
+      $script .= "_gaq.push(['_trackPageview', '$url_custom']);\n";
+    }
+
+    $script .= variable_get('googleanalytics_codesnippet_after', '');
+
+    // Should a local cached copy of ga.js be used?
+    $js_file = 'ga.js';
+    $url = 'http://www.google-analytics.com/'. $js_file;
 
     // Build tracker code for footer.
-    $script = 'try{';
-    $script .= 'var pageTracker = _gat._getTracker('. drupal_to_js($id) .');';
-    if (!empty($segmentation)) {
-      $script .= $segmentation;
-    }
-    if (!empty($codesnippet_before)) {
-      $script .= $codesnippet_before;
+    $script .= "(function() {\n" .
+      "  var ga = document.createElement('script');\n";
+
+    if (variable_get('googleanalytics_cache', 0) && (variable_get('file_downloads', FILE_DOWNLOADS_PUBLIC) == FILE_DOWNLOADS_PUBLIC) && $source = _googleanalytics_cache($url)) {
+      $script .= "  ga.src = '$source';";
     }
-    $script .= 'pageTracker._trackPageview('. $url_custom .');';
-    if (!empty($codesnippet_after)) {
-      $script .= $codesnippet_after;
+    else {
+      $script .= "  ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';\n";
     }
-    $script .= '} catch(err) {}';
 
-    drupal_add_js($script, 'inline', 'footer');
+    // HTML5 async forward support. Does nothing on most current browsers but will be cool in future.
+    $script .= "  ga.setAttribute('async', 'true');\n";
+    
+    $script .= "  document.documentElement.firstChild.appendChild(ga);\n";
+    $script .= "})();";
+
+    drupal_add_js($script, 'inline', $scope);
   }
 }
 
