You are here

public function GoogleAnalyticsCounterAppManager::gacUpdatePathCounts in Google Analytics Counter 8.3

Update the path counts.

Parameters

int $index: The index of the chunk to fetch and update.

This function is triggered by hook_cron().

Throws

\Exception

Overrides GoogleAnalyticsCounterAppManagerInterface::gacUpdatePathCounts

File

src/GoogleAnalyticsCounterAppManager.php, line 251

Class

GoogleAnalyticsCounterAppManager
Class GoogleAnalyticsCounterAppManager.

Namespace

Drupal\google_analytics_counter

Code

public function gacUpdatePathCounts($index = 0) {
  $feed = $this
    ->reportData($index);
  foreach ($feed->results->rows as $value) {

    // Use only the first 2047 characters of the pagepath. This is extremely long
    // but Google does store everything and bots can make URIs that exceed that length.
    $page_path = Html::escape($value['pagePath']);
    $string = strlen($value['pagePath']) > 2047 ? substr($value['pagePath'], 0, 2047) : $value['pagePath'];

    // Update the Google Analytics Counter.
    $this->connection
      ->merge('google_analytics_counter')
      ->key('pagepath_hash', md5($page_path))
      ->fields([
      'pagepath' => $string,
      'pageviews' => $value['pageviews'],
    ])
      ->execute();
  }

  // Log the results.
  $this->logger
    ->info($this
    ->t('Merged @count paths from Google Analytics into the database.', [
    '@count' => count($feed->results->rows),
  ]));
}