You are here

public function GoogleAnalyticsQuery::execute in Google Analytics Reports 8.3

Executes the query and fills the associated view object with according values.

Values to set: $view->result, $view->total_rows, $view->execute_time, $view->pager['current_page'].

$view->result should contain an array of objects. The array must use a numeric index starting at 0.

Parameters

view $view: The view which is executed.

Overrides QueryPluginBase::execute

File

src/Plugin/views/query/GoogleAnalyticsQuery.php, line 341

Class

GoogleAnalyticsQuery
Defines a Views query class for Google Analytics Reports API.

Namespace

Drupal\google_analytics_reports\Plugin\views\query

Code

public function execute(ViewExecutable $view) {

  // Initial check to see if we should attempt to run the query.
  if (!$this->state
    ->get('google_analytics_reports_api.access_token')) {

    // Optionally do not warn users on every query attempt before auth.
    $this->messenger
      ->addMessage($this
      ->t('You must <a href=":url">authorize your site</a> to use your Google Analytics account before you can view reports.', [
      ':url' => Url::fromRoute('google_analytics_reports_api.settings')
        ->toString(),
    ]));
    return;
  }
  $query = $view->build_info['query'];
  $count_query = $view->build_info['count_query'];
  $start = microtime(TRUE);

  // Query for total number of items.
  $count_query['max_results'] = 9999;
  $count_query['start_index'] = 1;
  $count_feed = google_analytics_reports_api_report_data($count_query);

  // Process only if data is available.
  if (!empty($count_feed->results->rows)) {
    $view->pager->total_items = count($count_feed->results->rows);
    $view->pager
      ->updatePageInfo();

    // Adjust based on the pager's modifications to limit and offset.
    if (!empty($this->limit) || !empty($this->offset)) {
      $query['max_results'] = intval(!empty($this->limit) ? $this->limit : 1000);
      $query['start_index'] = intval(!empty($this->offset) ? $this->offset : 0) + 1;
    }
    $feed = google_analytics_reports_api_report_data($query);
    $rows = $feed->results->rows;
    $views_result = [];
    $count = 0;
    foreach ($rows as $row) {
      $row['index'] = $count;
      $views_result[] = new ResultRow($row);
      $count++;
    }
    $view->result = isset($views_result) ? $views_result : [];
    $view->execute_time = microtime(TRUE) - $start;
    if ($view->pager
      ->usePager()) {
      $view->total_rows = $view->pager
        ->getTotalItems();
    }

    // Add to build_info['query'] to render query in Views UI query summary
    // area.
    $view->build_info['query'] = print_r($feed->results->query, TRUE);
  }
  else {

    // Set empty query instead of current query array to prevent error
    // in Views UI.
    $view->build_info['query'] = '';

    // Display the error from Google.
    if (!empty($count_feed->response->data)) {
      $response_data = json_decode($count_feed->response->data);
      if (isset($response_data['error']['message'])) {
        $this->messenger
          ->addMessage(Html::escape($response_data['error']['message']), 'error');
      }
    }
  }
}