Index: googleanalytics.install
===================================================================
--- googleanalytics.install	(revision 4115)
+++ googleanalytics.install	(working copy)
@@ -7,6 +7,7 @@
 
 function googleanalytics_uninstall() {
   variable_del('googleanalytics_account');
+  variable_del('googleanalytics_code_position');
   variable_del('googleanalytics_custom_var');
   variable_del('googleanalytics_codesnippet_before');
   variable_del('googleanalytics_codesnippet_after');
Index: googleanalytics.admin.inc
===================================================================
--- googleanalytics.admin.inc	(revision 4115)
+++ googleanalytics.admin.inc	(working copy)
@@ -53,6 +53,25 @@
     '#default_value' => variable_get('googleanalytics_domain_mode', 0),
   );
 
+  // 
+  $form['tracking_code_position'] = array(
+    '#type' => 'fieldset',
+    '#title' => t('Position of tracking code'),
+    '#collapsible' => TRUE,
+    '#collapsed' => TRUE,
+  );
+  
+  $form['tracking_code_position']['googleanalytics_code_position'] = array(
+    '#type' => 'radios',
+    '#options' => array(
+      'header' => t('Header (recommended)'),
+      'footer' => t('Footer'),
+    ),
+    '#default_value' => variable_get('googleanalytics_code_position', 'header'),
+    '#description' => t('Google officialy recommends to put the tracking code in the header. The advantage is that you also capture partial page loads. If your
+      target browser does not support asynchronous JavaScript callbacks or you do some GA relevant JavaScript calculation during the client-side page load, put it in the footer.'),
+  );
+  
   // Page specific visibility configurations.
   $form['page_vis_settings'] = array(
     '#type' => 'fieldset',
Index: googleanalytics.module
===================================================================
--- googleanalytics.module	(revision 4115)
+++ googleanalytics.module	(working copy)
@@ -36,6 +36,15 @@
 }
 
 /**
+ * Implementation of hook_theme_registry_alter().
+ */
+function googleanalytics_theme_registry_alter(&$registry) {
+  // Add custom preprocess function to the beginning of the stack.
+  // This allows use of the drupal_add_js() function before $vars is populated.
+  array_unshift($registry['page']['preprocess functions'], 'googleanalytics_add_js');
+}
+
+/**
  * Implementation of hook_perm().
  */
 function googleanalytics_perm() {
@@ -60,9 +69,9 @@
 }
 
 /**
- * Implementation of hook_init().
+ * Adds Google Analytics tracking scripts.
  */
-function googleanalytics_init() {
+function googleanalytics_add_js(&$vars, $hook) {
   global $user;
 
   $id = variable_get('googleanalytics_account', '');
@@ -96,21 +105,6 @@
       drupal_add_js(array('googleanalytics' => $link_settings), 'setting', 'header');
       drupal_add_js(drupal_get_path('module', 'googleanalytics') .'/googleanalytics.js', 'module', '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', '');
-
-  // 1. Check if the GA account number has a value.
-  // 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)) {
 
     // Add User profile segmentation values.
     $profile_fields = variable_get('googleanalytics_segmentation', array());
@@ -261,8 +255,8 @@
     $script .= 'var s = document.getElementsByTagName("script")[0];';
     $script .= 's.parentNode.insertBefore(ga, s);';
     $script .= '})();';
-
-    drupal_add_js($script, 'inline', 'footer');
+    
+    drupal_add_js($script, 'inline', variable_get('googleanalytics_code_position', 'header'));
   }
 }
 
@@ -485,7 +479,7 @@
 function _googleanalytics_visibility_pages() {
   static $page_match;
 
-  // Cache visibility setting in hook_init for hook_footer.
+  // Cache visibility setting in googleanalytics_add_js.
   if (!isset($page_match)) {
 
     $visibility = variable_get('googleanalytics_visibility', 0);
