--- googleanalytics.module 2009-04-02 03:10:36.000000000 +0900 +++ googleanalyticsNew.module 2010-01-07 08:40:21.000000000 +0900 @@ -46,19 +46,32 @@ 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); + if ($scope == 'header') { + // Build tracker code for header. + $script = "var _gaq = _gaq || [];\n"; + $script .= '_gaq.push([\'_setAccount\', '. drupal_to_js($id) ."]);\n"; } 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); + // Build tracker code for 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.');\n"; + $script .= 'document.write(unescape("%3Cscript src=\'" + gaJsHost + "google-analytics.com/ga.js\' type=\'text/javascript\'%3E%3C/script%3E"));'; + drupal_add_js($script, 'inline', $scope); + $script = "try{\n"; + $script .= 'var pageTracker = _gat._getTracker('. drupal_to_js($id) .");\n"; + } } // Add link tracking. @@ -82,18 +95,9 @@ function googleanalytics_init() { 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 +116,13 @@ function googleanalytics_footer($main = } // Only show segmentation variable if there are specified fields. - $segmentation = ''; if (count($fields) > 0) { - $segmentation = 'pageTracker._setVar('. drupal_to_js(implode(':', $fields)) .');'; + if ($scope == 'header') { + $script .= '_gaq.push([\'_setVar\', '. drupal_to_js(implode(':', $fields)) ."]);\n"; + } + else { + $script .= 'pageTracker._setVar('. drupal_to_js(implode(':', $fields)) .');'; + } } } @@ -149,26 +157,21 @@ function googleanalytics_footer($main = } } - // Add any custom code snippets if specified. - $codesnippet_before = variable_get('googleanalytics_codesnippet_before', ''); - $codesnippet_after = variable_get('googleanalytics_codesnippet_after', ''); - - // 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; + if ($scope == 'header') { + $script .= '_gaq.push([\'_trackPageview\','. $url_custom ."]);\n"; + $script .= variable_get('googleanalytics_codesnippet_after', ''); + $script .= "\n(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 .= "(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(ga);\n"; + $script .= '})();'; } - $script .= 'pageTracker._trackPageview('. $url_custom .');'; - if (!empty($codesnippet_after)) { - $script .= $codesnippet_after; + else { + $script .= 'pageTracker._trackPageview('. $url_custom .");\n"; + $script .= variable_get('googleanalytics_codesnippet_after', ''); + $script .= "\n} catch(err) {}"; } - $script .= '} catch(err) {}'; - - drupal_add_js($script, 'inline', 'footer'); + drupal_add_js($script, 'inline', $scope); } }