You are here

function google_analytics_reports_api_profiles_list in Google Analytics Reports 8.3

Same name and namespace in other branches
  1. 7.3 google_analytics_reports_api/google_analytics_reports_api.admin.inc \google_analytics_reports_api_profiles_list()

Google Analytics reports profiles for current authorized user.

Return value

arraynull An associative array containing:

  • options: list of current available profiles.
  • profile_id: current default profile id.
  • current_profile: current default profile object.
2 calls to google_analytics_reports_api_profiles_list()
GoogleAnalyticsQuery::buildOptionsForm in src/Plugin/views/query/GoogleAnalyticsQuery.php
Provide a form to edit options for this plugin.
GoogleAnalyticsReportsApiAdminSettingsForm::buildForm in google_analytics_reports_api/src/Form/GoogleAnalyticsReportsApiAdminSettingsForm.php
Form constructor.

File

google_analytics_reports_api/google_analytics_reports_api.module, line 174
Implements the API through which Google Analytics data can be accessed.

Code

function google_analytics_reports_api_profiles_list() {
  $config = \Drupal::configFactory()
    ->getEditable('google_analytics_reports_api.settings');
  $account = google_analytics_reports_api_gafeed();
  if ($account && $account
    ->isAuthenticated()) {
    $web_properties = NULL;
    $web_properties_obj = $account
      ->queryWebProperties();
    if (isset($web_properties_obj->results->items)) {
      $web_properties = $web_properties_obj->results->items;
    }
    $profiles_obj = $account
      ->queryProfiles();
    $profiles = [];
    if (isset($profiles_obj->results->items)) {
      $profiles = $profiles_obj->results->items;
    }
    $options = [];
    $profile_id = $config
      ->get('profile_id');
    $config_google_analytics = \Drupal::config('google_analytics.settings');
    $google_analytics_account = $config_google_analytics
      ->get('account') ? $config_google_analytics
      ->get('account') : NULL;
    $set_default = FALSE;

    // Add optgroups for each web property.
    if (!empty($profiles)) {
      foreach ($profiles as $profile) {
        $web_property = NULL;
        foreach ($web_properties as $web_property_value) {
          if ($web_property_value->id == $profile->webPropertyId) {
            $web_property = $web_property_value;
            break;
          }
        }
        $options[$web_property->name][$profile->id] = $profile->name . ' (' . $profile->id . ')';

        // Find current site in the account list.
        if (empty($profile_id)) {

          // If Google Analytics module is enabled check it first.
          if (isset($google_analytics_account) && $google_analytics_account == $profile->webPropertyId) {
            $profile_id = $profile->id;
            $set_default = TRUE;
          }
          elseif (isset($web_property->websiteUrl) ? parse_url($web_property->websiteUrl, PHP_URL_HOST) == $_SERVER['HTTP_HOST'] : '') {
            $profile_id = $profile->id;
            $set_default = TRUE;
          }
        }
      }
    }

    // If no profile ID is set yet, set the first profile in the list.
    if (empty($profile_id)) {
      if (count($options)) {
        $profile_id = key($options[key($options)]);
        $set_default = TRUE;
      }
    }
    if ($set_default) {
      $config
        ->set('profile_id', $profile_id)
        ->save();
    }
    $current_profile = NULL;

    // Load current profile object.
    foreach ($profiles as $profile) {
      if ($profile->id == $profile_id) {
        $current_profile = $profile;
        $config
          ->set('default_page', isset($current_profile->defaultPage) ? '/' . $current_profile->defaultPage : '/')
          ->save();
        break;
      }
    }
    $return = [
      'options' => $options,
      'profile_id' => $profile_id,
      'current_profile' => $current_profile,
    ];
    return $return;
  }
}