You are here

function googleanalytics_admin_settings_form in Clicky - Web Analytics in Real Time 6

Implementation of hook_admin_settings() for configuring the module

File

./getclicky.admin.inc, line 491
Administrative page callbacks for the getclicky module.

Code

function googleanalytics_admin_settings_form(&$form_state) {

  // Backward compatibility only.
  // TODO: If currently not in use, hide the UI and later remove the code.
  $segmentation = variable_get('googleanalytics_segmentation', array());
  if (!empty($segmentation) || variable_get('googleanalytics_segmentation_DEPRECATED', FALSE)) {
    $profile_enabled = module_exists('profile');
    $form['segmentation'] = array(
      '#type' => 'fieldset',
      '#title' => t('User segmentation settings'),
      '#collapsible' => TRUE,
    );

    // Compile a list of fields to show.
    $fields = variable_get('googleanalytics_segmentation_default_fields', array(
      'roles' => t('User roles'),
    ));
    if ($profile_enabled) {
      $result = db_query('SELECT name, title FROM {profile_fields} ORDER BY weight');
      while ($record = db_fetch_object($result)) {
        $fields[$record->name] = $record->title;
      }
    }
    $form['segmentation']['googleanalytics_segmentation'] = array(
      '#type' => 'select',
      '#title' => t('Add segmentation information to tracking code') . ' <span class="admin-disabled">***DEPRECATED***</span>',
      '#description' => t('Segment users based on different properties, additionally to the basic IP address based tracking provided by Google Analytics.') . ' <span class="admin-disabled">' . t('For most situations, Google recommend that you use Custom Variables to segment your visitors.') . '</span>' . (!$profile_enabled ? ' ' . t('<a href="@module_list">Enable the profile module</a> to be able to use profile fields for more granular tracking.', array(
        '@module_list' => url('admin/build/modules'),
      )) : '') . ' ' . t('Make sure you will not associate (or permit any third party to associate) any data gathered from Your Website(s) (or such third parties\' website(s)) with any personally identifying information from any source as part of Your use (or such third parties\' use) of the Google Analytics service. For more information see section 8.1 in the <a href="@ga_tos">Google Analytics terms of use</a>.', array(
        '@ga_tos' => 'http://www.google.com/analytics/en-GB/tos.html',
      )) . ' ' . t('You can select multiple values.'),
      '#default_value' => $segmentation,
      '#options' => $fields,
      '#size' => count($fields) > 3 ? 10 : 3,
      '#multiple' => TRUE,
    );
  }
  $form['googleanalytics_custom_var'] = array(
    '#collapsed' => TRUE,
    '#collapsible' => TRUE,
    '#description' => t('You can add Google Analytics <a href="!custom_var_documentation">Custom Variables</a> here. These will be added to every page that Google Analytics tracking code appears on. Google Analytics will only accept custom variables if the <em>name</em> and <em>value</em> combined are less than 64 bytes after URL encoding. Keep the names as short as possible and expect long values to get trimmed. You may use tokens in custom variable values. Global and user tokens are always available; on node pages, node tokens are also available.', array(
      '!custom_var_documentation' => 'http://code.google.com/intl/en/apis/analytics/docs/tracking/gaTrackingCustomVariables.html',
    )),
    '#theme' => 'googleanalytics_admin_custom_var_table',
    '#title' => t('Custom variables'),
    '#tree' => TRUE,
    '#type' => 'fieldset',
  );
  $token_enabled = module_exists('token');
  $googleanalytics_custom_vars = variable_get('googleanalytics_custom_var', array());

  // Google Analytics supports up to 5 custom variables.
  for ($i = 1; $i < 6; $i++) {

    // TODO: '#default_value' is currently broken, see http://drupal.org/node/410926.
    $form['googleanalytics_custom_var']['slots'][$i]['slot'] = array(
      //'#attributes' => array('readonly' => 'readonly'),

      //'#default_value' => $i,
      '#description' => t('Slot number'),
      '#disabled' => TRUE,
      '#size' => 1,
      '#type' => 'textfield',
      '#value' => $i,
    );
    $form['googleanalytics_custom_var']['slots'][$i]['name'] = array(
      '#default_value' => !empty($googleanalytics_custom_vars['slots'][$i]['name']) ? $googleanalytics_custom_vars['slots'][$i]['name'] : '',
      '#description' => t('The custom variable name.'),
      '#size' => 20,
      '#type' => 'textfield',
    );
    $form['googleanalytics_custom_var']['slots'][$i]['value'] = array(
      '#default_value' => !empty($googleanalytics_custom_vars['slots'][$i]['value']) ? $googleanalytics_custom_vars['slots'][$i]['value'] : '',
      '#description' => $token_enabled ? t('The custom variable value. You may use tokens in this field.') : t('The custom variable value.'),
      '#maxlength' => 255,
      '#type' => 'textfield',
    );
    if ($token_enabled) {
      $form['googleanalytics_custom_var']['slots'][$i]['value']['#element_validate'][] = 'googleanalytics_token_element_validate';
      $form['googleanalytics_custom_var']['slots'][$i]['value']['#element_validate'][] = 'token_element_validate';
      $form['googleanalytics_custom_var']['slots'][$i]['value']['#token_types'][] = 'node';
      $form['googleanalytics_custom_var']['slots'][$i]['value']['#token_types'][] = 'user';
    }
    $form['googleanalytics_custom_var']['slots'][$i]['scope'] = array(
      '#default_value' => !empty($googleanalytics_custom_vars['slots'][$i]['scope']) ? $googleanalytics_custom_vars['slots'][$i]['scope'] : 3,
      '#description' => t('The scope for the custom variable.'),
      '#type' => 'select',
      '#options' => array(
        1 => t('Visitor'),
        2 => t('Session'),
        3 => t('Page'),
      ),
    );
  }
  $form['googleanalytics_custom_var']['googleanalytics_custom_var_description'] = array(
    '#type' => 'item',
    '#description' => t('You can supplement Google Analytics\' basic IP address tracking of visitors by segmenting users based on custom variables.') . (!$token_enabled ? ' ' . t('<a href="@module_list">Enable the token module</a> to be able to use tokens for more granular tracking.', array(
      '@module_list' => url('admin/build/modules'),
    )) : '') . ' ' . t('Section 8.1 of the <a href="@ga_tos">Google Analytics terms of use</a> requires you to make sure you will not associate (or permit any third party to associate) any data gathered from your websites (or such third parties\' websites) with any personally identifying information from any source as part of your use (or such third parties\' use) of the Google Analytics\' service.', array(
      '@ga_tos' => 'http://www.google.com/analytics/en-GB/tos.html',
    )),
  );
  $form['googleanalytics_custom_var']['googleanalytics_custom_var_token_tree'] = array(
    '#theme' => 'token_tree',
    '#token_types' => array(
      'node',
      'user',
    ),
  );
  return system_settings_form($form);
}