You are here

function matomo_form_user_profile_form_alter in Matomo Analytics 7.2

Implement hook_form_FORM_ID_alter().

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

File

./matomo.module, line 405
Drupal Module: Matomo

Code

function matomo_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 matomo tracking') && ($custom = variable_get('matomo_custom', 1)) != 0 && _matomo_visibility_roles($account)) {
    $form['matomo'] = array(
      '#type' => 'fieldset',
      '#title' => t('Matomo 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;
    }

    // Migrate old piwik settings to matomo.
    if (isset($account->data['piwik']['custom'])) {
      $account->data['matomo']['custom'] = $account->data['piwik']['custom'];
    }
    $form['matomo']['custom'] = array(
      '#type' => 'checkbox',
      '#title' => t('Enable user tracking'),
      '#description' => $description,
      '#default_value' => isset($account->data['matomo']['custom']) ? $account->data['matomo']['custom'] : $custom == 1,
    );
    return $form;
  }
}