You are here

function google_analytics_reports_api_admin in Google Analytics Reports 7.3

Menu callback - admin form for OAuth and other settings.

1 call to google_analytics_reports_api_admin()
google_analytics_reports_api_domain_conf in google_analytics_reports_api/google_analytics_reports_api.module
Implements hook_domain_conf().
1 string reference to 'google_analytics_reports_api_admin'
google_analytics_reports_api_menu in google_analytics_reports_api/google_analytics_reports_api.module
Implements hook_menu().

File

google_analytics_reports_api/google_analytics_reports_api.admin.inc, line 11
Admin and OAuth callbacks for Google Analytics Reports API module.

Code

function google_analytics_reports_api_admin() {
  $form = array();
  $account = google_analytics_reports_api_gafeed();

  // There are no profiles, and we should just leave it at setup.
  if (!$account) {
    $setup_help = t('To access data from Google Analytics you have to create a new project in Google Developers Console.');
    $setup_help .= '<ol>';
    $setup_help .= '<li>' . t('Open !google_developers_console.', array(
      '!google_developers_console' => l(t('Google Developers Console'), 'https://console.developers.google.com', array(
        'attributes' => array(
          'target' => '_blank',
        ),
      )),
    )) . '</li>';
    $setup_help .= '<li>' . t('Along the toolbar click the pull down arrow and the press <strong>Create a Project</strong> button, enter project name and press <strong>Create</strong>.') . '</li>';
    $setup_help .= '<li>' . t('Click <strong>Enable and manage APIs</strong>.') . '</li>';
    $setup_help .= '<li>' . t('In the search box type <strong>Analytics</strong> and then press <strong>Analytics API</strong>, this opens the API page, press <strong>Enable</strong>.') . '</li>';
    $setup_help .= '<li>' . t('Click on <strong>Go to Credentials</strong>') . '</li>';
    $setup_help .= '<li>' . t('Under <strong>Where will you be calling the API from?</strong> select <strong>Web Browser Javascript</strong> and then select <strong>User Data</strong>') . '</li>';
    $setup_help .= '<li>' . t('Hit <strong>What credentials do I need</strong>, edit the name if necessary.') . '</li>';
    $setup_help .= '<li>' . t('Leave empty <strong>Authorized JavaScript origins</strong>, fill in <strong>Authorized redirect URIs</strong> with <code>@url</code> and press <strong>Create Client ID</strong> button.', array(
      '@url' => url(request_path(), array(
        'absolute' => TRUE,
      )),
    )) . '</li>';
    $setup_help .= '<li>' . t('Type a Product name to show to users and hit <strong>Continue</strong> and then <strong>Done</strong>') . '</li>';
    $setup_help .= '<li>' . t('Click on the name of your new client ID to be shown both the <strong>Client ID</strong> and <strong>Client Secret</strong>.') . '</li>';
    $setup_help .= '<li>' . t('Copy <strong>Client ID</strong> and <strong>Client secret</strong> from opened page to the form below.') . '</li>';
    $setup_help .= '<li>' . t('Press <strong>Start setup and authorize account</strong> in the form below and allow the project access to Google Analytics data.') . '</li>';
    $setup_help .= '</ol>';
    $form['setup'] = array(
      '#type' => 'fieldset',
      '#title' => t('Initial setup'),
      '#description' => $setup_help,
      '#collapsible' => FALSE,
      '#collapsed' => FALSE,
    );
    $form['setup']['client_id'] = array(
      '#type' => 'textfield',
      '#title' => t('Client ID'),
      '#default_value' => variable_get('google_analytics_reports_client_id', ''),
      '#size' => 75,
      '#description' => t('Client ID from your project in Google Developers Console.'),
      '#required' => TRUE,
    );
    $form['setup']['client_secret'] = array(
      '#type' => 'textfield',
      '#title' => t('Client Secret'),
      '#default_value' => variable_get('google_analytics_reports_client_secret', ''),
      '#size' => 30,
      '#description' => t('Client Secret from your project in Google Developers Console'),
      '#required' => TRUE,
    );
    $form['setup']['setup_submit'] = array(
      '#type' => 'submit',
      '#value' => t('Start setup and authorize account'),
      '#submit' => array(
        'google_analytics_reports_api_admin_submit_setup',
      ),
    );
  }
  elseif ($account
    ->isAuthenticated()) {

    // Load profiles list.
    $profile_list = google_analytics_reports_api_profiles_list();
    $form['settings'] = array(
      '#type' => 'fieldset',
      '#title' => t('Settings'),
      '#collapsible' => FALSE,
      '#collapsed' => FALSE,
    );
    $form['settings']['google_analytics_reports_api_profile_id'] = array(
      '#type' => 'select',
      '#title' => t('Reports profile'),
      '#options' => $profile_list['options'],
      '#default_value' => $profile_list['profile_id'],
      '#description' => t('Choose your Google Analytics profile.  The currently active profile is: %profile.', array(
        '%profile' => theme('google_analytics_reports_api_profile_label', array(
          'profile' => $profile_list['current_profile'],
          'active' => TRUE,
        )),
      )),
    );

    // Default cache periods.
    $times = array();

    // 1-6 days.
    for ($days = 1; $days <= 6; $days++) {
      $times[] = $days * 60 * 60 * 24;
    }

    // 1-4 weeks.
    for ($weeks = 1; $weeks <= 4; $weeks++) {
      $times[] = $weeks * 60 * 60 * 24 * 7;
    }
    $form['settings']['google_analytics_reports_api_cache_length'] = array(
      '#type' => 'select',
      '#title' => t('Query cache'),
      '#description' => t('The <a href="!link">Google Analytics Quota Policy</a> restricts the number of queries made per day. This limits the creation of new reports on your site.  We recommend setting this cache option to at least three days.', array(
        '!link' => url('https://developers.google.com/analytics/devguides/reporting/core/v3/limits-quotas', array(
          'fragment' => 'core_reporting',
        )),
      )),
      '#options' => drupal_map_assoc($times, 'format_interval'),
      '#default_value' => variable_get('google_analytics_reports_api_cache_length', 259200),
    );
    $form['settings']['settings_submit'] = array(
      '#type' => 'submit',
      '#value' => t('Save settings'),
      '#submit' => array(
        'google_analytics_reports_api_admin_submit_settings',
      ),
    );
    $form['revoke'] = array(
      '#type' => 'fieldset',
      '#title' => t('Revoke access and logout'),
      '#description' => t('Revoke your access token from Google Analytics. This action will log you out of your Google Analytics account and stop all reports from displaying on your site.'),
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
    );
    $form['revoke']['revoke_submit'] = array(
      '#type' => 'submit',
      '#value' => t('Revoke access token'),
      '#submit' => array(
        'google_analytics_reports_api_admin_submit_revoke',
      ),
    );
  }
  return $form;
}