You are here

function googleanalytics_init in Google Analytics 6.3

Same name and namespace in other branches
  1. 6 googleanalytics.module \googleanalytics_init()
  2. 6.2 googleanalytics.module \googleanalytics_init()

Implementation of hook_init().

File

./googleanalytics.module, line 74
Drupal Module: GoogleAnalytics Adds the required Javascript to the bottom of all your Drupal pages to allow tracking by the Google Analytics statistics package.

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)) {

    // Custom tracking.
    if (variable_get('googleanalytics_trackadsense', FALSE)) {

      // Prepare Adsense tracking.
      $googleanalytics_adsense_script = 'window.google_analytics_uacct = ' . drupal_to_js($id) . ';';

      // Domain tracking type.
      global $cookie_domain;
      $domain_mode = variable_get('googleanalytics_domain_mode', 0);

      // Per RFC 2109, cookie domains must contain at least one dot other than the
      // first. For hosts such as 'localhost' or IP Addresses we don't set a cookie domain.
      if ($domain_mode == 1 && count(explode('.', $cookie_domain)) > 2 && !is_numeric(str_replace('.', '', $cookie_domain))) {
        $googleanalytics_adsense_script .= 'window.google_analytics_domain_name = ' . drupal_to_js($cookie_domain) . ';';
      }
      drupal_add_js($googleanalytics_adsense_script, 'inline', 'header');
    }

    // Add link tracking.
    $link_settings = array();
    if ($track_outbound = variable_get('googleanalytics_trackoutgoing', 1)) {
      $link_settings['trackOutbound'] = $track_outbound;
    }
    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', GOOGLEANALYTICS_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', 'header');
    }
  }
}