diff --git a/googleanalytics.info b/googleanalytics.info
index fb3cb26..f8fd66d 100644
--- a/googleanalytics.info
+++ b/googleanalytics.info
@@ -3,7 +3,7 @@ name = "Google Analytics"
 description = Adds Google Analytics javascript tracking code to all your site's pages.
 
 ; Information added by drupal.org packaging script on 2010-09-19
-version = "5.x-1.x-dev"
+version = "5.x-1.x-dev-async"
 project = "google_analytics"
 datestamp = "1284898185"
 
diff --git a/googleanalytics.install b/googleanalytics.install
index c7450c5..8e53910 100644
--- a/googleanalytics.install
+++ b/googleanalytics.install
@@ -39,6 +39,7 @@ function googleanalytics_uninstall() {
   variable_del('googleanalytics_roles');
   variable_del('googleanalytics_visibility');
   variable_del('googleanalytics_pages');
+  variable_del('googleanalytics_version');
 }
 
 /**
@@ -258,3 +259,21 @@ function googleanalytics_update_6008() {
 
   return $ret;
 }
+
+
+/**
+ * Update old variable name
+ */
+function googleanalytics_update_6009() {
+  $ret = array();
+  if(variable_get('googleanalytics_version',-1) == -1) {
+    $legacy_mode = variable_get('googleanalytics_legacy_version',-1);
+    if($legacy_mode != -1) {
+      variable_del('googleanalytics_legacy_version');
+      variable_set('googleanalytics_version', $legacy_mode ? 1 : 0);
+      $ret[] = array('success' => TRUE, 'query' => 'Renamed legacy version variable');
+    }
+  }
+
+  return $ret;
+}
\ No newline at end of file
diff --git a/googleanalytics.js b/googleanalytics.js
index 359130f..851ed2b 100644
--- a/googleanalytics.js
+++ b/googleanalytics.js
@@ -11,7 +11,6 @@ Drupal.gaTrackerAttach = function(context) {
     var isInternalSpecial = new RegExp("(\/go\/.*)$", "i");
     // Expression to check for download links.
     var isDownload = new RegExp("\\.(" + ga.trackDownloadExtensions + ")$", "i");
-
     try {
       // Is the clicked URL internal?
       if (isInternal.test(this.href)) {
@@ -23,7 +22,12 @@ Drupal.gaTrackerAttach = function(context) {
           else {
             // Download link clicked.
             var extension = isDownload.exec(this.href);
-            pageTracker._trackEvent("Downloads", extension[1].toUpperCase(), this.href.replace(isInternal, ''));
+            if(ga.AsyncVersion) {
+              _gaq.push(["_trackEvent", "Downloads", extension[1].toUpperCase(), this.href.replace(isInternal, '')]);
+            }
+            else {
+              pageTracker._trackEvent("Downloads", extension[1].toUpperCase(), this.href.replace(isInternal, ''));
+            }
           }
         }
         else if (isInternalSpecial.test(this.href)) {
@@ -32,7 +36,12 @@ Drupal.gaTrackerAttach = function(context) {
             urchinTracker(this.href.replace(isInternal, ''));
           }
           else {
-            pageTracker._trackPageview(this.href.replace(isInternal, ''));
+            if(ga.AsyncVersion) {
+              _gaq.push(["_trackPageview", this.href.replace(isInternal, '')]);
+            }
+            else {
+              pageTracker._trackPageview(this.href.replace(isInternal, ''));
+            }
           }
         }
       }
@@ -43,7 +52,12 @@ Drupal.gaTrackerAttach = function(context) {
             urchinTracker('/mailto/' + this.href.substring(7));
           }
           else {
-            pageTracker._trackEvent("Mails", "Click", this.href.substring(7));
+            if(ga.AsyncVersion) {
+              _gaq.push(["_trackEvent", "Mails", "Click", this.href.substring(7)]);
+            }
+            else {
+              pageTracker._trackEvent("Mails", "Click", this.href.substring(7));
+            }
           }
         }
         else if (ga.trackOutgoing && this.href) {
@@ -52,11 +66,16 @@ Drupal.gaTrackerAttach = function(context) {
             urchinTracker('/outgoing/' + this.href.replace(/^(https?|ftp|news|nntp|telnet|irc|ssh|sftp|webcal):\/\//i, '').split('/').join('--'));
           }
           else {
-            pageTracker._trackEvent("Outgoing links", "Click", this.href);
+            if(ga.AsyncVersion) {
+              _gaq.push(["_trackEvent", "Outgoing links", "Click", this.href]);
+            }
+            else {
+              pageTracker._trackEvent("Outgoing links", "Click", this.href);
+            }
           }
         }
       }
-    } catch(err) {}
+    } catch(err) { window.console && console.log && console.log(err, "error tracking GA clicks" ); }
   });
 };
 
diff --git a/googleanalytics.module b/googleanalytics.module
index 72dd363..6b6617e 100644
--- a/googleanalytics.module
+++ b/googleanalytics.module
@@ -11,6 +11,11 @@
  */
 
 define('GA_TRACKFILES_EXTENSIONS', '7z|aac|arc|arj|asf|asx|avi|bin|csv|doc|exe|flv|gif|gz|gzip|hqx|jar|jpe?g|js|mp(2|3|4|e?g)|mov(ie)?|msi|msp|pdf|phps|png|ppt|qtm?|ra(m|r)?|sea|sit|tar|tgz|torrent|txt|wav|wma|wmv|wpd|xls|xml|z|zip');
+define('GA_VERSION_TRADITIONAL',0);
+define('GA_VERSION_LEGACY',1);
+define('GA_VERSION_ASYNC',2);
+
+
 
 function googleanalytics_help($section) {
   switch ($section) {
@@ -46,19 +51,35 @@ function googleanalytics_menu($maycache) {
     // 3. Check if we should track the currently active user's role.
     if (!empty($id) && _googleanalytics_visibility_pages() && _googleanalytics_visibility_user($user)) {
       // Use the old version of Google Analytics?
-      $legacy_version = variable_get('googleanalytics_legacy_version', 0);
+      $ga_version = variable_get('googleanalytics_version', 0);
       $scope = variable_get('googleanalytics_js_scope', 'footer');
 
       // Should a local cached copy of urchin.js or ga.js be used?
-      $js_file = ($legacy_version) ? 'urchin.js' : 'ga.js';
+      $js_file = ($ga_version == GA_VERSION_LEGACY) ? 'urchin.js' : 'ga.js';
       $url = 'http://www.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 = '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"));';
+        if($ga_version == GA_VERSION_ASYNC) {
+          // async snippet (http://code.google.com/intl/fr/apis/analytics/docs/tracking/asyncTracking.html)
+          $script = '';
+          
+          // should set up vars before loading script in async mode
+          $script .= _googleanalytics_get_vars_script() . "\n";
+          
+          $script .= "(function() {\n";
+          $script .= "var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;\n";
+          $script .= "ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';\n";
+          $script .= "var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);\n";
+          $script .= "})();\n";
+        }
+        else {
+          // traditional or legacy snippet (http://code.google.com/apis/analytics/docs/tracking/gaTrackingOverview.html)
+          $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);
       }
 
@@ -75,8 +96,8 @@ function googleanalytics_menu($maycache) {
         $link_settings['trackDownloadExtensions'] = $trackfiles_extensions;
       }
       if (!empty($link_settings)) {
-        // TODO: Why is this legacy_version added as data type string and not integer to settings?
-        $link_settings['LegacyVersion'] = $legacy_version ? 1 : 0;
+        $link_settings['LegacyVersion'] = ($ga_version == GA_VERSION_LEGACY) ? 1 : 0;
+        $link_settings['AsyncVersion'] = ($ga_version == GA_VERSION_ASYNC) ? 1 : 0;
         drupal_add_js(array('googleanalytics' => $link_settings), 'setting', 'header');
         drupal_add_js(drupal_get_path('module', 'googleanalytics') .'/googleanalytics.js', 'module', $scope);
       }
@@ -91,12 +112,8 @@ function googleanalytics_menu($maycache) {
   return $items;
 }
 
-/**
- * Implementation of hook_footer() to insert Javascript at the end of the page
- */
-function googleanalytics_footer($main = 0) {
+function _googleanalytics_get_vars_script() {
   global $user;
-
   $id = variable_get('googleanalytics_account', '');
 
   // 1. Check if the GA account number has a value.
@@ -105,7 +122,7 @@ function googleanalytics_footer($main = 0) {
   if (!empty($id) && _googleanalytics_visibility_pages() && _googleanalytics_visibility_user($user)) {
 
     // Use the old version of Google Analytics?
-    $legacy_version = variable_get('googleanalytics_legacy_version', 0);
+    $ga_version = variable_get('googleanalytics_version', 0);
 
     // Add User profile segmentation values
     if (is_array($profile_fields = variable_get('googleanalytics_segmentation', '')) && ($user->uid > 0)) {
@@ -126,10 +143,16 @@ function googleanalytics_footer($main = 0) {
       // Only show segmentation variable if there are specified fields.
       $segmentation = '';
       if (count($fields) > 0) {
-        if ($legacy_version) {
-          $segmentation = '__utmSetVar('. drupal_to_js(implode(':', $fields)) .');';
-        } else {
-          $segmentation = 'pageTracker._setVar('. drupal_to_js(implode(':', $fields)) .');';
+        switch($ga_version) {
+          case GA_VERSION_LEGACY:
+            $segmentation = '__utmSetVar('. drupal_to_js(implode(':', $fields)) .');';
+            break;
+          case GA_VERSION_TRADITIONAL:
+            $segmentation = 'pageTracker._setVar('. drupal_to_js(implode(':', $fields)) .');';
+            break;
+          case GA_VERSION_ASYNC:
+            $segmentation = '_gaq.push(["_setVar", ' . drupal_to_js(implode(':', $fields)) . ']);';
+            break;
         }
       }
     }
@@ -144,7 +167,7 @@ function googleanalytics_footer($main = 0) {
     if (function_exists('drupal_get_headers')) {
       $headers = drupal_get_headers();
       if (strstr($headers, '403 Forbidden')) {
-        if ($legacy_version) {
+        if ($ga_version == GA_VERSION_LEGACY) {
           // See http://www.google.com/support/analytics/bin/answer.py?answer=86928
           $url_custom = '"/403.html?page=" + _udl.pathname + _udl.search';
         }
@@ -154,7 +177,7 @@ function googleanalytics_footer($main = 0) {
         }
       }
       elseif (strstr($headers, '404 Not Found')) {
-        if ($legacy_version) {
+        if ($ga_version == GA_VERSION_LEGACY) {
           $url_custom = '"/404.html?page=" + _udl.pathname + _udl.search';
         }
         else {
@@ -167,38 +190,68 @@ function googleanalytics_footer($main = 0) {
     $codesnippet_before = variable_get('googleanalytics_codesnippet_before', '');
     $codesnippet_after = variable_get('googleanalytics_codesnippet_after', '');
 
-    // Should the legacy code be used?
+    // Which version of code should be used
     $script = 'try{';
-    if ($legacy_version) {
-      $script .= '_uacct = '. drupal_to_js($id) .';';
-      if (!empty($segmentation)) {
-        $script .= $segmentation;
-      }
-      if (!empty($codesnippet_before)) {
-        $script .= $codesnippet_before;
-      }
-      $script .= 'urchinTracker('. $url_custom .');';
-      if (!empty($codesnippet_after)) {
-        $script .= $codesnippet_after;
-      }
-    }
-    else {
-      $script .= 'var pageTracker = _gat._getTracker('. drupal_to_js($id) .');';
-      if (!empty($segmentation)) {
-        $script .= $segmentation;
-      }
-      if (!empty($codesnippet_before)) {
-        $script .= $codesnippet_before;
-      }
-      $script .= 'pageTracker._trackPageview('. $url_custom .');';
-      if (!empty($codesnippet_after)) {
-        $script .= $codesnippet_after;
-      }
+    switch($ga_version) {
+      case GA_VERSION_LEGACY:
+        $script .= '_uacct = '. drupal_to_js($id) .';';
+        if (!empty($segmentation)) {
+          $script .= $segmentation;
+        }
+        if (!empty($codesnippet_before)) {
+          $script .= $codesnippet_before . "\n";
+        }
+        $script .= 'urchinTracker('. $url_custom .');';
+        if (!empty($codesnippet_after)) {
+          $script .= $codesnippet_after . "\n";
+        }
+        break;
+      case GA_VERSION_TRADITIONAL:
+        $script .= 'var pageTracker = _gat._getTracker('. drupal_to_js($id) .');';
+        if (!empty($segmentation)) {
+          $script .= $segmentation;
+        }
+        if (!empty($codesnippet_before)) {
+          $script .= $codesnippet_before . "\n";
+        }
+        $script .= 'pageTracker._trackPageview('. $url_custom .');';
+        if (!empty($codesnippet_after)) {
+          $script .= $codesnippet_after . "\n";
+        }
+        break;
+      case GA_VERSION_ASYNC:
+        $script .= 'var _gaq = _gaq || [];';
+        $script .= '_gaq.push([\'_setAccount\', '. drupal_to_js($id) .']);';
+        if (!empty($segmentation)) {
+          $script .= $segmentation;
+        }
+        if (!empty($codesnippet_before)) {
+          $script .= $codesnippet_before . "\n";
+        }
+        $script .= '_gaq.push([\'_trackPageview\']);';
+        if (!empty($codesnippet_after)) {
+          $script .= $codesnippet_after . "\n";
+        }
+        break;
     }
-    $script .= '} catch(err) {}';
+  }
+    $script .= '} catch(err) { window.console && console.log && console.log( err, "error setting GA vars" ); }';
+  return $script;
+}
 
-    drupal_add_js($script, 'inline', 'footer');
+/**
+ * Implementation of hook_footer() to insert Javascript at the end of the page
+ */
+function googleanalytics_footer($main = 0) {
+  $ga_version = variable_get('googleanalytics_version', 0);
+  
+  // In async mode, all vars were already included immediately before loading ga.js
+  if($ga_version != GA_VERSION_ASYNC) {
+    $scope = variable_get('googleanalytics_js_scope', 'footer');
+    $script = _googleanalytics_get_vars_script();
+    drupal_add_js($script, 'inline', $scope);
   }
+  
 }
 
 /**
@@ -414,12 +467,13 @@ function googleanalytics_admin_settings_form() {
     '#collapsed' => TRUE,
   );
 
-  $form['advanced']['googleanalytics_legacy_version'] = array(
+  $form['advanced']['googleanalytics_version'] = array(
     '#type' => 'radios',
     '#title' => t('Google Analytics version used'),
-    '#default_value' => variable_get('googleanalytics_legacy_version', 0),
+    '#default_value' => variable_get('googleanalytics_version', 0),
     '#options' => array(
-      0 => t('Latest (ga.js) tracking code'),
+      0 => t('Traditional (ga.js) tracking code'),
+      2 => t('Async (ga.js) tracking code'),
       1 => t('Legacy (urchin.js) tracking code')
     ),
     '#description' => t('<a href="@ga_js_url">On December 13, 2007, Google rolled out a new API</a> for its tracking code, and suggests all new sites to use this code. You should only use the older legacy code, if you have custom tracking code tied to that API. Otherwise it is suggested you use the latest API, as the legacy code will not receive feature updates and is not compatible with new features.', array('@ga_js_url' => 'http://analytics.blogspot.com/2007/12/announcing-new-graphing-tools-gajs.html')),
@@ -470,7 +524,7 @@ function googleanalytics_admin_settings_form() {
     '#default_value' => variable_get('googleanalytics_codesnippet_before', ''),
     '#rows' => 5,
     '#wysiwyg' => FALSE,
-    '#description' => t('Code in this textarea will be added <strong>before</strong> urchinTracker() or pageTracker._trackPageview().')
+    '#description' => t('Code in this textarea will be added <strong>before</strong> urchinTracker() or pageTracker._trackPageview() or _gaq.push([\'_trackPageview\']).')
   );
   $form['advanced']['codesnippet']['googleanalytics_codesnippet_after'] = array(
     '#type' => 'textarea',
@@ -478,13 +532,13 @@ function googleanalytics_admin_settings_form() {
     '#default_value' => variable_get('googleanalytics_codesnippet_after', ''),
     '#rows' => 5,
     '#wysiwyg' => FALSE,
-    '#description' => t("Code in this textarea will be added <strong>after</strong> urchinTracker() or pageTracker._trackPageview(). This is useful if you'd like to track a site in two accounts.")
+    '#description' => t("Code in this textarea will be added <strong>after</strong> urchinTracker() or pageTracker._trackPageview() or _gaq.push([\'_trackPageview\']). This is useful if you'd like to track a site in two accounts.")
   );
 
   $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."),
+    '#description' => t("<strong>Warning:</strong> Google recommends adding the async version to header, but traditional version to footer for performance reasons."),
     '#options' => array(
       'footer' => t('Footer'),
       'header' => t('Header'),
