You are here

class AdminSettingsForm in Googalytics - Google Analytics 8

Form for editing Google Analytics settings.

Hierarchy

Expanded class hierarchy of AdminSettingsForm

1 string reference to 'AdminSettingsForm'
ga.routing.yml in ./ga.routing.yml
ga.routing.yml

File

src/Form/AdminSettingsForm.php, line 11

Namespace

Drupal\ga\Form
View source
class AdminSettingsForm extends ConfigFormBase {

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'ga_admin_settings';
  }

  /**
   * {@inheritdoc}
   */
  protected function getEditableConfigNames() {
    return [
      'ga.settings',
    ];
  }

  /**
   * {@inheritdoc}
   */
  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);
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    parent::validateForm($form, $form_state);
    $property = $form_state
      ->getValue('tracking_id');
    if (!empty($property) && !preg_match('/^UA-\\d+-\\d+$/', $property)) {
      $form_state
        ->setErrorByName('tracking_id', $this
        ->t('The provided Tracking ID is not valid.'));
    }
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $this
      ->config('ga.settings')
      ->set('add_default_commands', $form_state
      ->getValue('add_default_commands'))
      ->set('premium', $form_state
      ->getValue('premium'))
      ->set('tracking_id', $form_state
      ->getValue('tracking_id'))
      ->set('sample_rate', $form_state
      ->getValue('sample_rate'))
      ->set('site_speed_sample_rate', $form_state
      ->getValue('site_speed_sample_rate'))
      ->set('force_ssl', $form_state
      ->getValue('force_ssl'))
      ->set('send_pageview', $form_state
      ->getValue('send_pageview'))
      ->set('plugins.linkid', $form_state
      ->getValue([
      'plugins',
      'linkid',
    ]))
      ->set('plugins.displayfeatures', $form_state
      ->getValue([
      'plugins',
      'displayfeatures',
    ]))
      ->set('plugins.linker.enable', $form_state
      ->getValue([
      'plugins',
      'linker',
    ]))
      ->set('plugins.linker.domains', array_filter(preg_split('/, ?/', $form_state
      ->getValue([
      'plugins',
      'linker_domains',
    ]))))
      ->set('track_user_id', $form_state
      ->getValue('track_user_id'))
      ->set('user_roles.mode', $form_state
      ->getValue([
      'user_roles',
      'mode',
    ]))
      ->set('user_roles.roles', array_values(array_filter($form_state
      ->getValue([
      'user_roles',
      'roles',
    ]))))
      ->set('anonymize_ip', $form_state
      ->getValue('anonymize_ip'))
      ->save();
    parent::submitForm($form, $form_state);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AdminSettingsForm::buildForm public function Form constructor. Overrides ConfigFormBase::buildForm
AdminSettingsForm::getEditableConfigNames protected function Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait::getEditableConfigNames
AdminSettingsForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
AdminSettingsForm::submitForm public function Form submission handler. Overrides ConfigFormBase::submitForm
AdminSettingsForm::validateForm public function Form validation handler. Overrides FormBase::validateForm
ConfigFormBase::create public static function Instantiates a new instance of this class. Overrides FormBase::create 13
ConfigFormBase::__construct public function Constructs a \Drupal\system\ConfigFormBase object. 11
ConfigFormBaseTrait::config protected function Retrieves a configuration object.
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
FormBase::$configFactory protected property The config factory. 1
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::configFactory protected function Gets the config factory for this form. 1
FormBase::container private function Returns the service container.
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.