You are here

function googleanalytics_init in Google Analytics 6.2

Same name and namespace in other branches
  1. 6 googleanalytics.module \googleanalytics_init()
  2. 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)) {
    $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);
    }

    // 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)) {
      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');
    }
  }
}