You are here

public function AdminSettingsForm::buildForm in Googalytics - Google Analytics 8

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides ConfigFormBase::buildForm

File

src/Form/AdminSettingsForm.php, line 32

Class

AdminSettingsForm
Form for editing Google Analytics settings.

Namespace

Drupal\ga\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $config = $this
    ->config('ga.settings');
  $form['add_default_commands'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Add default analytics commands'),
    '#description' => $this
      ->t('Disable if another module will be providing these commands.'),
    '#default_value' => $config
      ->get('add_default_commands'),
  ];
  $form['premium'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Premium Account'),
    '#description' => $this
      ->t('Premium Google Analytics accounts have additional configuration options available'),
    '#default_value' => $config
      ->get('premium'),
  ];
  $form['settings'] = [
    '#type' => 'vertical_tabs',
    '#default_tab' => 'basics',
    '#states' => [
      'visible' => [
        ':input[data-drupal-selector="edit-add-default-commands"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];
  $form['basics'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Basics'),
    '#group' => 'settings',
  ];
  $form['basics']['tracking_id'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Web Property Tracking ID'),
    '#description' => $this
      ->t('Tracking ID in the format "UA-xxxxxxx-y"'),
    '#placeholder' => 'UA-xxxxxxxx-y',
    '#maxlength' => 20,
    '#size' => '20',
    '#default_value' => $config
      ->get('tracking_id'),
  ];
  $form['basics']['sample_rate'] = [
    '#type' => 'number',
    '#min' => 1,
    '#max' => 100,
    '#title' => $this
      ->t('Sample Rate'),
    '#description' => $this
      ->t('Specify what percentage of users should be tracked. This defaults to 100 (no users are sampled out) but large sites may need to use a lower sample rate to stay within Google Analytics processing limits.'),
    '#default_value' => $config
      ->get('sample_rate'),
  ];
  $form['basics']['site_speed_sample_rate'] = [
    '#type' => 'number',
    '#min' => 1,
    '#max' => 10,
    '#title' => $this
      ->t('Site Speed Sample Rate'),
    '#description' => $this
      ->t('This setting determines how often site speed tracking beacons will be sent. By default, 1% of users will automatically be tracked.'),
    '#default_value' => $config
      ->get('site_speed_sample_rate'),
  ];
  $form['basics']['force_ssl'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Force SSL'),
    '#description' => $this
      ->t('By default, tracking beacons sent from https pages will be sent using https while beacons sent from http pages will be sent using http. Enabling this option will force http pages to also send all beacons using https.'),
    '#default_value' => $config
      ->get('force_ssl'),
  ];
  $form['basics']['send_pageview'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Send Pageview Event'),
    '#default_value' => $config
      ->get('send_pageview'),
  ];
  $form['plugins'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Plugins'),
    '#group' => 'settings',
    '#tree' => TRUE,
  ];
  $form['plugins']['linkid'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Enhanced Link Attribution'),
    '#description' => $this
      ->t('Improve the accuracy of your In-Page Analytics report by automatically differentiating between multiple links to the same URL on a single page by using link element IDs (<a href=":url">Documentation</a>).', [
      ':url' => 'https://developers.google.com/analytics/devguides/collection/analyticsjs/enhanced-link-attribution',
    ]),
    '#default_value' => $config
      ->get('plugins.linkid'),
  ];
  $form['plugins']['displayfeatures'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Display Features'),
    '#description' => $this
      ->t('Enable Advertising Features in Google Analytics, such as Remarketing, Demographics and Interest Reporting, and more (<a href=":url">Documentation</a>).<br/> This option can also be enabled through your property settings.', [
      ':url' => 'https://developers.google.com/analytics/devguides/collection/analyticsjs/enhanced-link-attribution',
    ]),
    '#default_value' => $config
      ->get('plugins.displayfeatures'),
  ];
  $form['plugins']['linker'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Linker'),
    '#description' => $this
      ->t('Enable cross-domain tracking (<a href=":url">Documentation</a>).', [
      ':url' => 'https://developers.google.com/analytics/devguides/collection/analyticsjs/linker',
    ]),
    '#default_value' => $config
      ->get('plugins.linker.enable'),
  ];
  $form['plugins']['linker_domains'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Linker Domains'),
    '#description' => $this
      ->t('A comma separated list of domains.'),
    '#default_value' => implode(', ', $config
      ->get('plugins.linker.domains')),
    '#states' => [
      'visible' => [
        ':input[data-drupal-selector="edit-plugins-linker"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];
  $form['users'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Users'),
    '#group' => 'settings',
  ];
  $form['users']['track_user_id'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Track User ID'),
    '#description' => $this
      ->t('Enable the analysis of groups of sessions, across devices (<a href=":url">Documentation</a>).', [
      ':url' => 'https://developers.google.com/analytics/devguides/collection/analyticsjs/cookies-user-id#user_id',
    ]),
    '#default_value' => $config
      ->get('track_user_id'),
  ];
  $form['users']['user_roles'] = [
    '#type' => 'fieldset',
    '#title' => 'Restrict analytics by user role',
    '#tree' => TRUE,
  ];
  $form['users']['user_roles']['mode'] = [
    '#type' => 'radios',
    '#title' => $this
      ->t('Filter mode'),
    '#options' => [
      'disabled' => $this
        ->t('Disabled'),
      'include' => $this
        ->t('Include'),
      'exclude' => $this
        ->t('Exclude'),
    ],
    '#default_value' => $config
      ->get('user_roles.mode'),
  ];
  $rolesConfigKeys = $this
    ->configFactory()
    ->listAll('user.role.');
  $rolesConfig = $this
    ->configFactory()
    ->loadMultiple($rolesConfigKeys);
  usort($rolesConfig, function ($a, $b) {
    return $a
      ->get('weight') - $b
      ->get('weight');
  });
  $roleOptions = array_reduce($rolesConfig, function ($result, $item) {
    $result[$item
      ->get('id')] = $item
      ->get('label');
    return $result;
  }, []);
  $form['users']['user_roles']['roles'] = [
    '#type' => 'checkboxes',
    '#title' => $this
      ->t('Roles'),
    '#options' => $roleOptions,
    '#default_value' => $config
      ->get('user_roles.roles'),
  ];
  $form['privacy'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Privacy'),
    '#group' => 'settings',
  ];
  $form['privacy']['anonymize_ip'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('IP Anonymization'),
    '#description' => $this
      ->t('Request IP address anonymization (<a href=":url">Documentation</a>).', [
      ':url' => 'https://developers.google.com/analytics/devguides/collection/analyticsjs/ip-anonymization',
    ]),
    '#default_value' => $config
      ->get('anonymize_ip'),
  ];
  return parent::buildForm($form, $form_state);
}