You are here

public function GoogleAnalyticsCounterAppManager::reportData in Google Analytics Counter 8.3

Request report data.

Parameters

array $parameters: An associative array containing:

  • profile_id: required [default='ga:profile_id']
  • dimensions: optional [ga:pagePath]
  • metrics: required [ga:pageviews]
  • sort: optional [ga:pageviews]
  • start-date: [default=-1 week]
  • end_date: optional [default=today]
  • start_index: [default=1]
  • max_results: optional [default=10,000].
  • filters: optional [default=none]
  • segment: optional [default=none]

array $cache_options: An optional associative array containing:

  • cid: optional [default=md5 hash]
  • expire: optional [default=CACHE_TEMPORARY]
  • refresh: optional [default=FALSE].

Return value

\Drupal\google_analytics_counter\GoogleAnalyticsCounterFeed|object A new GoogleAnalyticsCounterFeed object

Overrides GoogleAnalyticsCounterAppManagerInterface::reportData

1 call to GoogleAnalyticsCounterAppManager::reportData()
GoogleAnalyticsCounterAppManager::gacUpdatePathCounts in src/GoogleAnalyticsCounterAppManager.php
Update the path counts.

File

src/GoogleAnalyticsCounterAppManager.php, line 160

Class

GoogleAnalyticsCounterAppManager
Class GoogleAnalyticsCounterAppManager.

Namespace

Drupal\google_analytics_counter

Code

public function reportData($parameters = [], $cache_options = []) {
  $config = $this->config;
  $step = $this->state
    ->get('google_analytics_counter.data_step');
  $chunk = $config
    ->get('general_settings.chunk_to_fetch');

  // Initialize the pointer.
  $pointer = $step * $chunk + 1;
  $parameters = [
    'profile_id' => 'ga:' . $config
      ->get('general_settings.profile_id'),
    'dimensions' => [
      'ga:pagePath',
    ],
    'metrics' => [
      'ga:pageviews',
    ],
    'sort_metric' => NULL,
    'filters' => NULL,
    'segment' => NULL,
    'start_date' => !empty($config
      ->get('general_settings.start_date')) ? strtotime($config
      ->get('general_settings.start_date')) : strtotime($config
      ->get('general_settings.custom_start_date')),
    'end_date' => !empty($config
      ->get('general_settings.end_date')) ? strtotime($config
      ->get('general_settings.end_date')) : strtotime($config
      ->get('general_settings.custom_end_date')),
    'start_index' => $pointer,
    'max_results' => $chunk,
  ];
  $cache_options = [
    'cid' => 'google_analytics_counter_' . md5(serialize($parameters)),
    'expire' => GoogleAnalyticsCounterHelper::cacheTime(),
    'refresh' => FALSE,
  ];

  //Instantiate a new GoogleAnalyticsCounterFeed object.
  $feed = $this
    ->gacGetFeed($parameters, $cache_options);

  // Set the total number of pagePaths for this profile from start_date to end_date.
  $this->state
    ->set('google_analytics_counter.total_paths', $feed->results->totalResults);

  // The last time the Data was refreshed by Google. Not always available from Google.
  if (!empty($feed->results->dataLastRefreshed)) {
    $this->state
      ->set('google_analytics_counter.data_last_refreshed', $feed->results->dataLastRefreshed);
  }

  // The first selfLink query to Google. Helpful for debugging in the dashboard.
  $this->state
    ->set('google_analytics_counter.most_recent_query', $feed->results->selfLink);

  // The total number of pageViews for this profile from start_date to end_date.
  $this->state
    ->set('google_analytics_counter.total_pageviews', $feed->results->totalsForAllResults['pageviews']);

  // The total number of pagePaths for this profile from start_date to end_date.
  $this->state
    ->set('google_analytics_counter.total_paths', $feed->results->totalResults);

  // The number of results from Google Analytics in one request.
  $chunk = $config
    ->get('general_settings.chunk_to_fetch');

  // Do one chunk at a time and register the data step.
  $step = $this->state
    ->get('google_analytics_counter.data_step');

  // Which node to look for first. Must be between 1 - infinity.
  $pointer = $step * $chunk + 1;

  // Set the pointer equal to the pointer plus the chunk.
  $pointer += $chunk;
  $t_args = [
    '@size_of' => sizeof($feed->results->rows),
    '@first' => $pointer - $chunk,
    '@second' => $pointer - $chunk - 1 + sizeof($feed->results->rows),
  ];
  $this->logger
    ->info('Retrieved @size_of items from Google Analytics data for paths @first - @second.', $t_args);

  // Increase the step or set the step to 0 depending on whether
  // the pointer is less than or equal to the total results.
  if ($pointer <= $feed->results->totalResults) {
    $new_step = $step + 1;
  }
  else {
    $new_step = 0;
  }
  $this->state
    ->set('google_analytics_counter.data_step', $new_step);
  return $feed;
}