You are here

function googleanalytics_menu in Google Analytics 5

Same name and namespace in other branches
  1. 6.4 googleanalytics.module \googleanalytics_menu()
  2. 6 googleanalytics.module \googleanalytics_menu()
  3. 6.2 googleanalytics.module \googleanalytics_menu()
  4. 6.3 googleanalytics.module \googleanalytics_menu()
  5. 7.2 googleanalytics.module \googleanalytics_menu()
  6. 7 googleanalytics.module \googleanalytics_menu()

File

./googleanalytics.module, line 25

Code

function googleanalytics_menu($maycache) {
  global $user;
  $items = array();
  if ($maycache) {
    $items[] = array(
      'path' => 'admin/settings/googleanalytics',
      'title' => t('Google Analytics'),
      'description' => t('Configure the settings used to generate your Google Analytics tracking code.'),
      'callback' => 'drupal_get_form',
      'callback arguments' => array(
        'googleanalytics_admin_settings_form',
      ),
      'access' => user_access('administer google analytics'),
      'type' => MENU_NORMAL_ITEM,
    );
  }
  else {
    $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', 0);
      $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');
      }
    }
  }
  return $items;
}