You are here

function googleanalytics_form_user_profile_form_alter in Google Analytics 7

Same name and namespace in other branches
  1. 7.2 googleanalytics.module \googleanalytics_form_user_profile_form_alter()

Implements hook_form_FORM_ID_alter().

Allow users to decide if tracking code will be added to pages or not.

File

./googleanalytics.module, line 355
Drupal Module: Google Analytics

Code

function googleanalytics_form_user_profile_form_alter(&$form, &$form_state) {
  $account = $form['#user'];
  $category = $form['#user_category'];
  if ($category == 'account' && user_access('opt-in or out of tracking') && ($custom = variable_get('googleanalytics_custom', 0)) != 0 && _googleanalytics_visibility_roles($account)) {
    $form['googleanalytics'] = array(
      '#type' => 'fieldset',
      '#title' => t('Google Analytics configuration'),
      '#weight' => 3,
      '#collapsible' => TRUE,
      '#tree' => TRUE,
    );
    switch ($custom) {
      case 1:
        $description = t('Users are tracked by default, but you are able to opt out.');
        break;
      case 2:
        $description = t('Users are <em>not</em> tracked by default, but you are able to opt in.');
        break;
    }

    // Disable tracking for visitors who have opted out from tracking via DNT (Do-Not-Track) header.
    $disabled = FALSE;
    if (variable_get('googleanalytics_privacy_donottrack', 1) && !empty($_SERVER['HTTP_DNT'])) {
      $disabled = TRUE;

      // Override settings value.
      $account->data['googleanalytics']['custom'] = FALSE;
      $description .= '<span class="admin-disabled">';
      $description .= ' ' . t('You have opted out from tracking via browser privacy settings.');
      $description .= '</span>';
    }
    $form['googleanalytics']['custom'] = array(
      '#type' => 'checkbox',
      '#title' => t('Enable user tracking'),
      '#description' => $description,
      '#default_value' => isset($account->data['googleanalytics']['custom']) ? $account->data['googleanalytics']['custom'] : $custom == 1,
      '#disabled' => $disabled,
    );
    return $form;
  }
}