diff --git googleanalytics.module googleanalytics.module index c690ff2..a0c355d 100644 --- googleanalytics.module +++ googleanalytics.module @@ -46,20 +46,12 @@ 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); - } - 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(); @@ -74,6 +66,7 @@ function googleanalytics_init() { $link_settings['trackDownloadExtensions'] = $trackfiles_extensions; } if (!empty($link_settings)) { + drupal_set_message('Not sure link settings actually work yet. They probably do not.'); drupal_add_js(array('googleanalytics' => $link_settings), 'setting', 'header'); drupal_add_js(drupal_get_path('module', 'googleanalytics') .'/googleanalytics.js', 'module', $scope); } @@ -82,18 +75,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 +96,8 @@ function googleanalytics_footer($main = 0) { } // 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)) . "]);"; } } @@ -149,26 +132,35 @@ function googleanalytics_footer($main = 0) { } } - // 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']);"; + } + else { + $script .= "_gaq.push(['_trackPageview', '$url_custom']);"; + } + + $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() { + var ga = document.createElement('script'); + "; + 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) {}'; + $script .= " + ga.setAttribute('async', 'true'); + document.documentElement.firstChild.appendChild(ga); + })();"; - drupal_add_js($script, 'inline', 'footer'); + drupal_add_js($script, 'inline', $scope); } }