Index: googleanalytics.admin.inc =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/google_analytics/googleanalytics.admin.inc,v retrieving revision 1.16 diff -u -r1.16 googleanalytics.admin.inc --- googleanalytics.admin.inc 3 Jun 2010 01:37:58 -0000 1.16 +++ googleanalytics.admin.inc 3 Jun 2010 17:30:14 -0000 @@ -246,12 +246,12 @@ $form['advanced']['googleanalytics_js_scope'] = array( '#type' => 'select', '#title' => t('JavaScript scope'), - '#description' => t('Google recommends adding the external JavaScript files to the footer for performance reasons.'), + '#description' => t('Google recommends adding the external JavaScript files to the header for performance reasons.'), '#options' => array( 'footer' => t('Footer'), 'header' => t('Header'), ), - '#default_value' => variable_get('googleanalytics_js_scope', 'footer'), + '#default_value' => variable_get('googleanalytics_js_scope', 'header'), ); return system_settings_form($form); Index: googleanalytics.js =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/google_analytics/googleanalytics.js,v retrieving revision 1.9 diff -u -r1.9 googleanalytics.js --- googleanalytics.js 17 Oct 2009 13:42:43 -0000 1.9 +++ googleanalytics.js 3 Jun 2010 17:30:14 -0000 @@ -16,32 +16,29 @@ // 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]); + } + } }); }); }); Index: googleanalytics.install =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/google_analytics/googleanalytics.install,v retrieving revision 1.14 diff -u -r1.14 googleanalytics.install --- googleanalytics.install 28 May 2010 18:19:31 -0000 1.14 +++ googleanalytics.install 3 Jun 2010 17:30:14 -0000 @@ -279,3 +279,15 @@ return $ret; } + +/** + * Update JavaScript scope to 'header'. + */ +function googleanalytics_update_7000() { + $ret = array(); + + variable_set('googleanalytics_js_scope', 'header'); + $ret[] = array('success' => TRUE, 'query' => 'Google tracking code has been moved to header scope.'); + + return $ret; +} Index: googleanalytics.module =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/google_analytics/googleanalytics.module,v retrieving revision 1.42 diff -u -r1.42 googleanalytics.module --- googleanalytics.module 3 Jun 2010 13:10:19 -0000 1.42 +++ googleanalytics.module 3 Jun 2010 17:30:15 -0000 @@ -51,7 +51,7 @@ } /** - * Implementation of hook_page_alter() to insert Javascript at the bottom of the page. + * Implementation of hook_page_alter() to insert JavaScript to the appropriate scope/region of the page. */ function googleanalytics_page_alter(&$page) { global $user; @@ -64,7 +64,16 @@ if (!empty($id) && _googleanalytics_visibility_pages() && _googleanalytics_visibility_user($user)) { // We allow different scopes. Default to 'footer' but allow user to override if they really need to. - $scope = variable_get('googleanalytics_js_scope', 'footer'); + $scope = variable_get('googleanalytics_js_scope', 'header'); + + // Custom tracking. Prepend before all other JavaScript. + if (variable_get('googleanalytics_trackadsense', FALSE)) { + $page['footer']['googleanalytics']['#attached']['js']['window.google_analytics_uacct = ' . drupal_json_encode($id) . ';'] = array( + 'type' => 'inline', + 'scope' => 'header', + 'weight' => JS_LIBRARY - 21, + ); + } // Add link tracking. $link_settings = array(); @@ -86,15 +95,6 @@ $page['footer']['googleanalytics']['#attached']['js'][drupal_get_path('module', 'googleanalytics') .'/googleanalytics.js'] = array('scope' => $scope); } - // Custom tracking. - if (variable_get('googleanalytics_trackadsense', FALSE)) { - $page['footer']['googleanalytics']['#attached']['js']['window.google_analytics_uacct = ' . drupal_json_encode($id) . ';'] = array( - 'type' => 'inline', - 'scope' => 'header', - 'weight' => JS_LIBRARY - 21, - ); - } - // Add User profile segmentation values. $profile_fields = variable_get('googleanalytics_segmentation', array()); if (!empty($profile_fields) && ($user->uid > 0)) { @@ -111,7 +111,7 @@ // Only show segmentation variable if there are specified fields. $segmentation = ''; if (count($fields) > 0) { - $segmentation = 'pageTracker._setVar('. drupal_json_encode(implode(':', $fields)) .');'; + $segmentation = '_gaq.push(["_setVar", ' . drupal_json_encode(implode(':', $fields)) . ']);'; } } @@ -148,33 +148,40 @@ $codesnippet_before = variable_get('googleanalytics_codesnippet_before', ''); $codesnippet_after = 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; - $script = ''; - - if (variable_get('googleanalytics_cache', 0) && $source = _googleanalytics_cache($url)) { - $page['footer']['googleanalytics']['#attached']['js'][$source] = array('scope' => $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"));'; - } - // Build tracker code. - $script .= 'try{'; - $script .= 'var pageTracker = _gat._getTracker('. drupal_json_encode($id) .');'; + $script = 'var _gaq = _gaq || [];'; + $script .= '_gaq.push(["_setAccount", ' . drupal_json_encode($id) . ']);'; if (!empty($segmentation)) { $script .= $segmentation; } if (!empty($codesnippet_before)) { $script .= $codesnippet_before; } - $script .= 'pageTracker._trackPageview('. $url_custom .');'; + if (empty($url_custom)) { + $script .= '_gaq.push(["_trackPageview"]);'; + } + else { + $script .= '_gaq.push(["_trackPageview", ' . $url_custom . ']);'; + } if (!empty($codesnippet_after)) { $script .= $codesnippet_after; } - $script .= '} catch(err) {}'; + + $script .= '(function() {'; + $script .= 'var ga = document.createElement("script");'; + $script .= 'ga.type = "text/javascript";'; + $script .= 'ga.async = true;'; + + // Should a local cached copy of ga.js be used? + if (variable_get('googleanalytics_cache', 0) && $source = _googleanalytics_cache('http://www.google-analytics.com/ga.js')) { + $script .= 'ga.src = "' . file_create_url($source) . '";'; + } + else { + $script .= 'ga.src = ("https:" == document.location.protocol ? "https://ssl" : "http://www") + ".google-analytics.com/ga.js";'; + } + $script .= 'var s = document.getElementsByTagName("script")[0];'; + $script .= 's.parentNode.insertBefore(ga, s);'; + $script .= '})();'; // Add tracker code to scope. $page['footer']['googleanalytics']['#attached']['js'][$script] = array(