You are here

public function GoogleAnalyticsAdminSettingsForm::buildForm in Google Analytics 8.2

Same name and namespace in other branches
  1. 8.3 src/Form/GoogleAnalyticsAdminSettingsForm.php \Drupal\google_analytics\Form\GoogleAnalyticsAdminSettingsForm::buildForm()
  2. 4.x src/Form/GoogleAnalyticsAdminSettingsForm.php \Drupal\google_analytics\Form\GoogleAnalyticsAdminSettingsForm::buildForm()

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/GoogleAnalyticsAdminSettingsForm.php, line 66

Class

GoogleAnalyticsAdminSettingsForm
Configure Google_Analytics settings for this site.

Namespace

Drupal\google_analytics\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $config = $this
    ->config('google_analytics.settings');
  $form['general'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('General settings'),
    '#open' => TRUE,
  ];
  $form['general']['google_analytics_account'] = [
    '#default_value' => $config
      ->get('account'),
    '#description' => $this
      ->t('This ID is unique to each site you want to track separately, and is in the form of UA-xxxxxxx-yy. To get a Web Property ID, <a href=":analytics">register your site with Google Analytics</a>, or if you already have registered your site, go to your Google Analytics Settings page to see the ID next to every site profile. <a href=":webpropertyid">Find more information in the documentation</a>.', [
      ':analytics' => 'https://marketingplatform.google.com/about/analytics/',
      ':webpropertyid' => Url::fromUri('https://developers.google.com/analytics/resources/concepts/gaConceptsAccounts', [
        'fragment' => 'webProperty',
      ])
        ->toString(),
    ]),
    '#maxlength' => 20,
    '#placeholder' => 'UA-',
    '#required' => TRUE,
    '#size' => 20,
    '#title' => $this
      ->t('Web Property ID'),
    '#type' => 'textfield',
  ];
  $form['general']['google_analytics_premium'] = [
    '#default_value' => $config
      ->get('premium'),
    '#description' => $this
      ->t('If you are a Google Analytics Premium customer, you can use up to 200 instead of 20 custom dimensions and metrics.'),
    '#title' => $this
      ->t('Premium account'),
    '#type' => 'checkbox',
  ];

  // Visibility settings.
  $form['tracking_scope'] = [
    '#type' => 'vertical_tabs',
    '#title' => $this
      ->t('Tracking scope'),
    '#attached' => [
      'library' => [
        'google_analytics/google_analytics.admin',
      ],
    ],
  ];
  $form['tracking']['domain_tracking'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Domains'),
    '#group' => 'tracking_scope',
  ];
  global $cookie_domain;
  $multiple_sub_domains = [];
  foreach ([
    'www',
    'app',
    'shop',
  ] as $subdomain) {
    if (count(explode('.', $cookie_domain)) > 2 && !is_numeric(str_replace('.', '', $cookie_domain))) {
      $multiple_sub_domains[] = $subdomain . $cookie_domain;
    }
    else {
      $multiple_sub_domains[] = $subdomain . '.example.com';
    }
  }
  $multiple_toplevel_domains = [];
  foreach ([
    '.com',
    '.net',
    '.org',
  ] as $tldomain) {
    $host = $_SERVER['HTTP_HOST'];
    $domain = substr($host, 0, strrpos($host, '.'));
    if (count(explode('.', $host)) > 2 && !is_numeric(str_replace('.', '', $host))) {
      $multiple_toplevel_domains[] = $domain . $tldomain;
    }
    else {
      $multiple_toplevel_domains[] = 'www.example' . $tldomain;
    }
  }
  $form['tracking']['domain_tracking']['google_analytics_domain_mode'] = [
    '#type' => 'radios',
    '#title' => $this
      ->t('What are you tracking?'),
    '#options' => [
      0 => $this
        ->t('A single domain (default)'),
      1 => $this
        ->t('One domain with multiple subdomains'),
      2 => $this
        ->t('Multiple top-level domains'),
    ],
    0 => [
      '#description' => $this
        ->t('Domain: @domain', [
        '@domain' => $_SERVER['HTTP_HOST'],
      ]),
    ],
    1 => [
      '#description' => $this
        ->t('Examples: @domains', [
        '@domains' => implode(', ', $multiple_sub_domains),
      ]),
    ],
    2 => [
      '#description' => $this
        ->t('Examples: @domains', [
        '@domains' => implode(', ', $multiple_toplevel_domains),
      ]),
    ],
    '#default_value' => $config
      ->get('domain_mode'),
  ];
  $form['tracking']['domain_tracking']['google_analytics_cross_domains'] = [
    '#title' => $this
      ->t('List of top-level domains'),
    '#type' => 'textarea',
    '#default_value' => $config
      ->get('cross_domains'),
    '#description' => $this
      ->t('If you selected "Multiple top-level domains" above, enter all related top-level domains. Add one domain per line. By default, the data in your reports only includes the path and name of the page, and not the domain name. For more information see section <em>Show separate domain names</em> in <a href=":url">Tracking Multiple Domains</a>.', [
      ':url' => 'https://support.google.com/analytics/answer/1034342',
    ]),
    '#states' => [
      'enabled' => [
        ':input[name="google_analytics_domain_mode"]' => [
          'value' => '2',
        ],
      ],
      'required' => [
        ':input[name="google_analytics_domain_mode"]' => [
          'value' => '2',
        ],
      ],
    ],
  ];

  // Page specific visibility configurations.
  $account = $this->currentUser;
  $php_access = $account
    ->hasPermission('use PHP for google analytics tracking visibility');
  $visibility_request_path_pages = $config
    ->get('visibility.request_path_pages');
  $form['tracking']['page_visibility_settings'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Pages'),
    '#group' => 'tracking_scope',
  ];
  if ($config
    ->get('visibility.request_path_mode') == 2 && !$php_access) {
    $form['tracking']['page_visibility_settings'] = [];
    $form['tracking']['page_visibility_settings']['google_analytics_visibility_request_path_mode'] = [
      '#type' => 'value',
      '#value' => 2,
    ];
    $form['tracking']['page_visibility_settings']['google_analytics_visibility_request_path_pages'] = [
      '#type' => 'value',
      '#value' => $visibility_request_path_pages,
    ];
  }
  else {
    $options = [
      $this
        ->t('Every page except the listed pages'),
      $this
        ->t('The listed pages only'),
    ];
    $description = $this
      ->t("Specify pages by using their paths. Enter one path per line. The '*' character is a wildcard. Example paths are %blog for the blog page and %blog-wildcard for every personal blog. %front is the front page.", [
      '%blog' => '/blog',
      '%blog-wildcard' => '/blog/*',
      '%front' => '<front>',
    ]);
    if ($this->moduleHandler
      ->moduleExists('php') && $php_access) {
      $options[] = $this
        ->t('Pages on which this PHP code returns <code>TRUE</code> (not supported in Drupal 9, experts only)');
      $title = $this
        ->t('Pages or PHP code');
      $description .= ' ' . $this
        ->t('If the PHP option is chosen, enter PHP code between %php. Note that executing incorrect PHP code can break your Drupal site.', [
        '%php' => '<?php ?>',
      ]);
    }
    else {
      $title = $this
        ->t('Pages');
    }
    $form['tracking']['page_visibility_settings']['google_analytics_visibility_request_path_mode'] = [
      '#type' => 'radios',
      '#title' => $this
        ->t('Add tracking to specific pages'),
      '#options' => $options,
      '#default_value' => $config
        ->get('visibility.request_path_mode'),
    ];
    $form['tracking']['page_visibility_settings']['google_analytics_visibility_request_path_pages'] = [
      '#type' => 'textarea',
      '#title' => $title,
      '#title_display' => 'invisible',
      '#default_value' => !empty($visibility_request_path_pages) ? $visibility_request_path_pages : '',
      '#description' => $description,
      '#rows' => 10,
    ];
  }

  // Render the role overview.
  $visibility_user_role_roles = $config
    ->get('visibility.user_role_roles');
  $form['tracking']['role_visibility_settings'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Roles'),
    '#group' => 'tracking_scope',
  ];
  $form['tracking']['role_visibility_settings']['google_analytics_visibility_user_role_mode'] = [
    '#type' => 'radios',
    '#title' => $this
      ->t('Add tracking for specific roles'),
    '#options' => [
      $this
        ->t('Add to the selected roles only'),
      $this
        ->t('Add to every role except the selected ones'),
    ],
    '#default_value' => $config
      ->get('visibility.user_role_mode'),
  ];
  $form['tracking']['role_visibility_settings']['google_analytics_visibility_user_role_roles'] = [
    '#type' => 'checkboxes',
    '#title' => $this
      ->t('Roles'),
    '#default_value' => !empty($visibility_user_role_roles) ? $visibility_user_role_roles : [],
    '#options' => array_map('\\Drupal\\Component\\Utility\\Html::escape', user_role_names()),
    '#description' => $this
      ->t('If none of the roles are selected, all users will be tracked. If a user has any of the roles checked, that user will be tracked (or excluded, depending on the setting above).'),
  ];

  // Standard tracking configurations.
  $visibility_user_account_mode = $config
    ->get('visibility.user_account_mode');
  $form['tracking']['user_visibility_settings'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Users'),
    '#group' => 'tracking_scope',
  ];
  $t_permission = [
    '%permission' => $this
      ->t('Opt-in or out of tracking'),
  ];
  $form['tracking']['user_visibility_settings']['google_analytics_visibility_user_account_mode'] = [
    '#type' => 'radios',
    '#title' => $this
      ->t('Allow users to customize tracking on their account page'),
    '#options' => [
      0 => $this
        ->t('No customization allowed'),
      1 => $this
        ->t('Tracking on by default, users with %permission permission can opt out', $t_permission),
      2 => $this
        ->t('Tracking off by default, users with %permission permission can opt in', $t_permission),
    ],
    '#default_value' => !empty($visibility_user_account_mode) ? $visibility_user_account_mode : 0,
  ];
  $form['tracking']['user_visibility_settings']['google_analytics_trackuserid'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Track User ID'),
    '#default_value' => $config
      ->get('track.userid'),
    '#description' => $this
      ->t('User ID enables the analysis of groups of sessions, across devices, using a unique, persistent, and non-personally identifiable ID string representing a user. <a href=":url">Learn more about the benefits of using User ID</a>.', [
      ':url' => 'https://support.google.com/analytics/answer/3123663',
    ]),
  ];

  // Link specific configurations.
  $form['tracking']['linktracking'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Links and downloads'),
    '#group' => 'tracking_scope',
  ];
  $form['tracking']['linktracking']['google_analytics_trackoutbound'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Track clicks on outbound links'),
    '#default_value' => $config
      ->get('track.outbound'),
  ];
  $form['tracking']['linktracking']['google_analytics_trackmailto'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Track clicks on mailto links'),
    '#default_value' => $config
      ->get('track.mailto'),
  ];
  $form['tracking']['linktracking']['google_analytics_trackfiles'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Track downloads (clicks on file links) for the following extensions'),
    '#default_value' => $config
      ->get('track.files'),
  ];
  $form['tracking']['linktracking']['google_analytics_trackfiles_extensions'] = [
    '#title' => $this
      ->t('List of download file extensions'),
    '#title_display' => 'invisible',
    '#type' => 'textfield',
    '#default_value' => $config
      ->get('track.files_extensions'),
    '#description' => $this
      ->t('A file extension list separated by the | character that will be tracked as download when clicked. Regular expressions are supported. For example: @extensions', [
      '@extensions' => GoogleAnalitycsInterface::GOOGLE_ANALYTICS_TRACKFILES_EXTENSIONS,
    ]),
    '#maxlength' => 500,
    '#states' => [
      'enabled' => [
        ':input[name="google_analytics_trackfiles"]' => [
          'checked' => TRUE,
        ],
      ],
      // Note: Form required marker is not visible as title is invisible.
      'required' => [
        ':input[name="google_analytics_trackfiles"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];
  $colorbox_dependencies = '<div class="admin-requirements">';
  $colorbox_dependencies .= $this
    ->t('Requires: @module-list', [
    '@module-list' => $this->moduleHandler
      ->moduleExists('colorbox') ? $this
      ->t('@module (<span class="admin-enabled">enabled</span>)', [
      '@module' => 'Colorbox',
    ]) : $this
      ->t('@module (<span class="admin-missing">disabled</span>)', [
      '@module' => 'Colorbox',
    ]),
  ]);
  $colorbox_dependencies .= '</div>';
  $form['tracking']['linktracking']['google_analytics_trackcolorbox'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Track content in colorbox modal dialogs'),
    '#description' => $this
      ->t('Enable to track the content shown in colorbox modal windows.') . $colorbox_dependencies,
    '#default_value' => $config
      ->get('track.colorbox'),
    '#disabled' => $this->moduleHandler
      ->moduleExists('colorbox') ? FALSE : TRUE,
  ];
  $form['tracking']['linktracking']['google_analytics_tracklinkid'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Track enhanced link attribution'),
    '#default_value' => $config
      ->get('track.linkid'),
    '#description' => $this
      ->t('Enhanced Link Attribution improves 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">Enable enhanced link attribution</a> in the Admin UI of your Google Analytics account.', [
      ':url' => 'https://support.google.com/analytics/answer/2558867',
    ]),
  ];
  $form['tracking']['linktracking']['google_analytics_trackurlfragments'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Track changing URL fragments as pageviews'),
    '#default_value' => $config
      ->get('track.urlfragments'),
    '#description' => $this
      ->t('By default, the URL reported to Google Analytics will not include the "fragment identifier" (i.e. the portion of the URL beginning with a hash sign), and hash changes by themselves will not cause new pageviews to be reported. Checking this box will cause hash changes to be reported as pageviews (in modern browsers) and all pageview URLs to include the fragment where applicable.'),
  ];

  // Message specific configurations.
  $form['tracking']['messagetracking'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Messages'),
    '#group' => 'tracking_scope',
  ];
  $track_messages = $config
    ->get('track.messages');
  $form['tracking']['messagetracking']['google_analytics_trackmessages'] = [
    '#type' => 'checkboxes',
    '#title' => $this
      ->t('Track messages of type'),
    '#default_value' => !empty($track_messages) ? $track_messages : [],
    '#description' => $this
      ->t('This will track the selected message types shown to users. Tracking of form validation errors may help you identifying usability issues in your site. For each visit (user session), a maximum of approximately 500 combined GATC requests (both events and page views) can be tracked. Every message is tracked as one individual event. Note that - as the number of events in a session approaches the limit - additional events might not be tracked. Messages from excluded pages cannot be tracked.'),
    '#options' => [
      'status' => $this
        ->t('Status message'),
      'warning' => $this
        ->t('Warning message'),
      'error' => $this
        ->t('Error message'),
    ],
  ];
  $form['tracking']['search_and_advertising'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Search and Advertising'),
    '#group' => 'tracking_scope',
  ];
  $site_search_dependencies = '<div class="admin-requirements">';
  $site_search_dependencies .= $this
    ->t('Requires: @module-list', [
    '@module-list' => $this->moduleHandler
      ->moduleExists('search') ? $this
      ->t('@module (<span class="admin-enabled">enabled</span>)', [
      '@module' => 'Search',
    ]) : $this
      ->t('@module (<span class="admin-missing">disabled</span>)', [
      '@module' => 'Search',
    ]),
  ]);
  $site_search_dependencies .= '</div>';
  $form['tracking']['search_and_advertising']['google_analytics_site_search'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Track internal search'),
    '#description' => $this
      ->t('If checked, internal search keywords are tracked. You must configure your Google account to use the internal query parameter <strong>search</strong>. For more information see <a href=":url">Setting Up Site Search for a Profile</a>.', [
      ':url' => 'https://support.google.com/analytics/answer/1012264',
    ]) . $site_search_dependencies,
    '#default_value' => $config
      ->get('track.site_search'),
    '#disabled' => $this->moduleHandler
      ->moduleExists('search') ? FALSE : TRUE,
  ];
  $form['tracking']['search_and_advertising']['google_analytics_trackadsense'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Track AdSense ads'),
    '#description' => $this
      ->t('If checked, your AdSense ads will be tracked in your Google Analytics account.'),
    '#default_value' => $config
      ->get('track.adsense'),
  ];
  $form['tracking']['search_and_advertising']['google_analytics_trackdisplayfeatures'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Track display features'),
    '#description' => $this
      ->t('The display features plugin can be used to enable Display Advertising Features in Google Analytics, such as Remarketing, Demographics and Interest Reporting, and more. <a href=":displayfeatures">Learn more about Display Advertising Features in Google Analytics</a>. If you choose this option you will need to <a href=":privacy">update your privacy policy</a>.', [
      ':displayfeatures' => 'https://support.google.com/analytics/answer/3450482',
      ':privacy' => 'https://support.google.com/analytics/answer/2700409',
    ]),
    '#default_value' => $config
      ->get('track.displayfeatures'),
  ];

  // Privacy specific configurations.
  $form['tracking']['privacy'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Privacy'),
    '#group' => 'tracking_scope',
  ];
  $form['tracking']['privacy']['google_analytics_tracker_anonymizeip'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Anonymize visitors IP address'),
    '#description' => $this
      ->t('Tell Google Analytics to anonymize the information sent by the tracker objects by removing the last octet of the IP address prior to its storage. Note that this will slightly reduce the accuracy of geographic reporting. In some countries it is not allowed to collect personally identifying information for privacy reasons and this setting may help you to comply with the local laws.'),
    '#default_value' => $config
      ->get('privacy.anonymizeip'),
  ];

  // Custom Dimensions.
  $form['google_analytics_custom_dimension'] = [
    '#description' => $this
      ->t('You can set values for Google Analytics <a href=":custom_var_documentation">Custom Dimensions</a> here. You must have already configured your custom dimensions in the <a href=":setup_documentation">Google Analytics Management Interface</a>. You may use tokens. Global and user tokens are always available; on node pages, node tokens are also available. A dimension <em>value</em> is allowed to have a maximum length of 150 bytes. Expect longer values to get trimmed.', [
      ':custom_var_documentation' => 'https://developers.google.com/analytics/devguides/collection/analyticsjs/custom-dims-mets',
      ':setup_documentation' => 'https://support.google.com/analytics/answer/2709829',
    ]),
    '#title' => $this
      ->t('Custom dimensions'),
    '#tree' => TRUE,
    '#type' => 'details',
  ];
  $form['google_analytics_custom_dimension']['indexes'] = [
    '#type' => 'table',
    '#header' => [
      [
        'data' => $this
          ->t('Index'),
      ],
      [
        'data' => $this
          ->t('Value'),
      ],
    ],
  ];
  $google_analytics_custom_dimension = $config
    ->get('custom.dimension');

  // Standard Google Analytics accounts support up to 20 custom dimensions,
  // premium accounts support up to 200 custom dimensions.
  $limit = $config
    ->get('premium') ? 200 : 20;
  for ($i = 1; $i <= $limit; $i++) {
    $form['google_analytics_custom_dimension']['indexes'][$i]['index'] = [
      '#default_value' => $i,
      '#description' => $this
        ->t('Index number'),
      '#disabled' => TRUE,
      '#size' => $limit == 200 ? 3 : 2,
      '#title' => $this
        ->t('Custom dimension index #@index', [
        '@index' => $i,
      ]),
      '#title_display' => 'invisible',
      '#type' => 'textfield',
    ];
    $form['google_analytics_custom_dimension']['indexes'][$i]['value'] = [
      '#default_value' => isset($google_analytics_custom_dimension[$i]['value']) ? $google_analytics_custom_dimension[$i]['value'] : '',
      '#description' => $this
        ->t('The custom dimension value.'),
      '#maxlength' => 255,
      '#title' => $this
        ->t('Custom dimension value #@index', [
        '@index' => $i,
      ]),
      '#title_display' => 'invisible',
      '#type' => 'textfield',
      '#element_validate' => [
        [
          get_class($this),
          'tokenElementValidate',
        ],
      ],
      '#token_types' => [
        'node',
      ],
    ];
    if ($this->moduleHandler
      ->moduleExists('token')) {
      $form['google_analytics_custom_dimension']['indexes'][$i]['value']['#element_validate'][] = 'token_element_validate';
    }
  }
  $form['google_analytics_custom_dimension']['google_analytics_description'] = [
    '#type' => 'item',
    '#description' => $this
      ->t('You can supplement Google Analytics\' basic IP address tracking of visitors by segmenting users based on custom dimensions. Section 7 of the <a href=":ga_tos">Google Analytics terms of service</a> requires that You will not (and will not allow any third party to) use the Service to track, collect or upload any data that personally identifies an individual (such as a name, userid, email address or billing information), or other data which can be reasonably linked to such information by Google. You will have and abide by an appropriate Privacy Policy and will comply with all applicable laws and regulations relating to the collection of information from Visitors. You must post a Privacy Policy and that Privacy Policy must provide notice of Your use of cookies that are used to collect traffic data, and You must not circumvent any privacy features (e.g., an opt-out) that are part of the Service.', [
      ':ga_tos' => 'https://www.google.com/analytics/terms/gb.html',
    ]),
  ];
  if ($this->moduleHandler
    ->moduleExists('token')) {
    $form['google_analytics_custom_dimension']['google_analytics_token_tree'] = [
      '#theme' => 'token_tree_link',
      '#token_types' => [
        'node',
      ],
    ];
  }

  // Custom Metrics.
  $form['google_analytics_custom_metric'] = [
    '#description' => $this
      ->t('You can add Google Analytics <a href=":custom_var_documentation">Custom Metrics</a> here. You must have already configured your custom metrics in the <a href=":setup_documentation">Google Analytics Management Interface</a>. You may use tokens. Global and user tokens are always available; on node pages, node tokens are also available.', [
      ':custom_var_documentation' => 'https://developers.google.com/analytics/devguides/collection/analyticsjs/custom-dims-mets',
      ':setup_documentation' => 'https://support.google.com/analytics/answer/2709829',
    ]),
    '#title' => $this
      ->t('Custom metrics'),
    '#tree' => TRUE,
    '#type' => 'details',
  ];
  $form['google_analytics_custom_metric']['indexes'] = [
    '#type' => 'table',
    '#header' => [
      [
        'data' => $this
          ->t('Index'),
      ],
      [
        'data' => $this
          ->t('Value'),
      ],
    ],
  ];
  $google_analytics_custom_metric = $config
    ->get('custom.metric');

  // Standard Google Analytics accounts support up to 20 custom metrics,
  // premium accounts support up to 200 custom metrics.
  for ($i = 1; $i <= $limit; $i++) {
    $form['google_analytics_custom_metric']['indexes'][$i]['index'] = [
      '#default_value' => $i,
      '#description' => $this
        ->t('Index number'),
      '#disabled' => TRUE,
      '#size' => $limit == 200 ? 3 : 2,
      '#title' => $this
        ->t('Custom metric index #@index', [
        '@index' => $i,
      ]),
      '#title_display' => 'invisible',
      '#type' => 'textfield',
    ];
    $form['google_analytics_custom_metric']['indexes'][$i]['value'] = [
      '#default_value' => isset($google_analytics_custom_metric[$i]['value']) ? $google_analytics_custom_metric[$i]['value'] : '',
      '#description' => $this
        ->t('The custom metric value.'),
      '#maxlength' => 255,
      '#title' => $this
        ->t('Custom metric value #@index', [
        '@index' => $i,
      ]),
      '#title_display' => 'invisible',
      '#type' => 'textfield',
      '#element_validate' => [
        [
          get_class($this),
          'tokenElementValidate',
        ],
      ],
      '#token_types' => [
        'node',
      ],
    ];
    if ($this->moduleHandler
      ->moduleExists('token')) {
      $form['google_analytics_custom_metric']['indexes'][$i]['value']['#element_validate'][] = 'token_element_validate';
    }
  }
  $form['google_analytics_custom_metric']['google_analytics_description'] = [
    '#type' => 'item',
    '#description' => $this
      ->t('You can supplement Google Analytics\' basic IP address tracking of visitors by segmenting users based on custom metrics. Section 7 of the <a href=":ga_tos">Google Analytics terms of service</a> requires that You will not (and will not allow any third party to) use the Service to track, collect or upload any data that personally identifies an individual (such as a name, userid, email address or billing information), or other data which can be reasonably linked to such information by Google. You will have and abide by an appropriate Privacy Policy and will comply with all applicable laws and regulations relating to the collection of information from Visitors. You must post a Privacy Policy and that Privacy Policy must provide notice of Your use of cookies that are used to collect traffic data, and You must not circumvent any privacy features (e.g., an opt-out) that are part of the Service.', [
      ':ga_tos' => 'https://www.google.com/analytics/terms/gb.html',
    ]),
  ];
  if ($this->moduleHandler
    ->moduleExists('token')) {
    $form['google_analytics_custom_metric']['google_analytics_token_tree'] = [
      '#theme' => 'token_tree_link',
      '#token_types' => [
        'node',
      ],
    ];
  }

  // Advanced feature configurations.
  $form['advanced'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Advanced settings'),
    '#open' => FALSE,
  ];
  $form['advanced']['google_analytics_cache'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Locally cache tracking code file'),
    '#description' => $this
      ->t("If checked, the tracking code file is retrieved from Google Analytics and cached locally. It is updated daily from Google's servers to ensure updates to tracking code are reflected in the local copy. Do not activate this until after Google Analytics has confirmed that site tracking is working!"),
    '#default_value' => $config
      ->get('cache'),
  ];

  // Allow for tracking of the originating node when viewing translation sets.
  if ($this->moduleHandler
    ->moduleExists('content_translation')) {
    $form['advanced']['google_analytics_translation_set'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Track translation sets as one unit'),
      '#description' => $this
        ->t('When a node is part of a translation set, record statistics for the originating node instead. This allows for a translation set to be treated as a single unit.'),
      '#default_value' => $config
        ->get('translation_set'),
    ];
  }
  $user_access_add_js_snippets = !$this
    ->currentUser()
    ->hasPermission('add JS snippets for google analytics');
  $user_access_add_js_snippets_permission_warning = $user_access_add_js_snippets ? ' <em>' . $this
    ->t('This field has been disabled because you do not have sufficient permissions to edit it.') . '</em>' : '';
  $form['advanced']['codesnippet'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Custom JavaScript code'),
    '#open' => TRUE,
    '#description' => $this
      ->t('You can add custom Google Analytics <a href=":snippets">code snippets</a> here. These will be added every time tracking is in effect. Before you add your custom code, you should read the <a href=":ga_concepts_overview">Google Analytics Tracking Code - Functional Overview</a> and the <a href=":ga_js_api">Google Analytics Tracking API</a> documentation. <strong>Do not include the &lt;script&gt; tags</strong>, and always end your code with a semicolon (;).', [
      ':snippets' => 'https://drupal.org/node/248699',
      ':ga_concepts_overview' => 'https://developers.google.com/analytics/resources/concepts/gaConceptsTrackingOverview',
      ':ga_js_api' => 'https://developers.google.com/analytics/devguides/collection/analyticsjs/method-reference',
    ]),
  ];
  $form['advanced']['codesnippet']['google_analytics_codesnippet_create'] = [
    '#type' => 'textarea',
    '#title' => $this
      ->t('Create only fields'),
    '#default_value' => $this
      ->getNameValueString($config
      ->get('codesnippet.create')),
    '#rows' => 5,
    '#description' => $this
      ->t('Enter one value per line, in the format name|value. Settings in this textarea will be added to <code>ga("create", "UA-XXXX-Y", {"name":"value"});</code>. For more information, read <a href=":url">create only fields</a> documentation in the Analytics.js field reference.', [
      ':url' => 'https://developers.google.com/analytics/devguides/collection/analyticsjs/field-reference#create',
    ]),
    '#element_validate' => [
      [
        get_class($this),
        'validateCreateFieldValues',
      ],
    ],
  ];
  $form['advanced']['codesnippet']['google_analytics_codesnippet_before'] = [
    '#type' => 'textarea',
    '#title' => $this
      ->t('Code snippet (before)'),
    '#default_value' => $config
      ->get('codesnippet.before'),
    '#disabled' => $user_access_add_js_snippets,
    '#rows' => 5,
    '#description' => $this
      ->t('Code in this textarea will be added <strong>before</strong> <code>ga("send", "pageview");</code>.') . $user_access_add_js_snippets_permission_warning,
  ];
  $form['advanced']['codesnippet']['google_analytics_codesnippet_after'] = [
    '#type' => 'textarea',
    '#title' => $this
      ->t('Code snippet (after)'),
    '#default_value' => $config
      ->get('codesnippet.after'),
    '#disabled' => $user_access_add_js_snippets,
    '#rows' => 5,
    '#description' => $this
      ->t('Code in this textarea will be added <strong>after</strong> <code>ga("send", "pageview");</code>. This is useful if you\'d like to track a site in two accounts.') . $user_access_add_js_snippets_permission_warning,
  ];
  $form['advanced']['google_analytics_debug'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Enable debugging'),
    '#description' => $this
      ->t('If checked, the Google Universal Analytics debugging script will be loaded. You should not enable your production site to use this version of the JavaScript. The analytics_debug.js script is larger than the analytics.js tracking code and it is not typically cached. Using it in your production site will slow down your site for all of your users. Again, this is only for your own testing purposes. Debug messages are printed to the <code>window.console</code> object.'),
    '#default_value' => $config
      ->get('debug'),
  ];
  return parent::buildForm($form, $form_state);
}