Index: googleanalytics.js =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/google_analytics/googleanalytics.js,v retrieving revision 1.9.2.2 diff -u -r1.9.2.2 googleanalytics.js --- googleanalytics.js 22 May 2010 23:41:32 -0000 1.9.2.2 +++ googleanalytics.js 24 May 2010 00:00:14 -0000 @@ -15,32 +15,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.module =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/google_analytics/googleanalytics.module,v retrieving revision 1.36.2.2 diff -u -r1.36.2.2 googleanalytics.module --- googleanalytics.module 22 May 2010 23:41:32 -0000 1.36.2.2 +++ googleanalytics.module 24 May 2010 00:00:15 -0000 @@ -46,20 +46,12 @@ // 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)) { + + // We allow different scopes. Default to footer but allow user to overide if they really need to. $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; - - 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 @@ } 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 @@ } // 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 @@ } } - // 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); } }