function googleanalytics_init in Google Analytics 6
Same name and namespace in other branches
- 6.2 googleanalytics.module \googleanalytics_init()
- 6.3 googleanalytics.module \googleanalytics_init()
File
- ./
googleanalytics.module, line 39
Code
function googleanalytics_init() {
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)) {
// Use the old version of Google Analytics?
$legacy_version = variable_get('googleanalytics_legacy_version', FALSE);
$scope = variable_get('googleanalytics_js_scope', 'footer');
// Should a local cached copy of urchin.js or ga.js be used?
$js_file = $legacy_version ? 'urchin.js' : '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);
}
// Add link tracking.
$link_settings = array();
if ($track_outgoing = variable_get('googleanalytics_trackoutgoing', 1)) {
$link_settings['trackOutgoing'] = $track_outgoing;
}
if ($track_mailto = variable_get('googleanalytics_trackmailto', 1)) {
$link_settings['trackMailto'] = $track_mailto;
}
if (($track_download = variable_get('googleanalytics_trackfiles', 1)) && ($trackfiles_extensions = variable_get('googleanalytics_trackfiles_extensions', GA_TRACKFILES_EXTENSIONS))) {
$link_settings['trackDownload'] = $track_download;
$link_settings['trackDownloadExtensions'] = $trackfiles_extensions;
}
if (!empty($link_settings)) {
// TODO: Why is this legacy_version added as data type string and not integer to settings?
$link_settings['LegacyVersion'] = $legacy_version ? 1 : 0;
drupal_add_js(array(
'googleanalytics' => $link_settings,
), 'setting', 'header');
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');
}
}
}