You are here

public function GoogleAnalyticsCounterAppManager::gacDisplayCount in Google Analytics Counter 8.3

Get the count of pageviews for a path.

Return value

string Count of page views.

Overrides GoogleAnalyticsCounterAppManagerInterface::gacDisplayCount

File

src/GoogleAnalyticsCounterAppManager.php, line 426

Class

GoogleAnalyticsCounterAppManager
Class GoogleAnalyticsCounterAppManager.

Namespace

Drupal\google_analytics_counter

Code

public function gacDisplayCount() {

  // Make sure the path starts with a slash.
  $path = \Drupal::service('path.current')
    ->getPath();
  $path = '/' . trim($path, ' /');
  $sum_pageviews = 0;

  // It's the front page.
  if ($this->pathMatcher
    ->isFrontPage()) {
    $aliases = [
      '/',
    ];
    $sum_pageviews = $this
      ->sumPageviews($aliases);
  }
  else {
    if ($node = \Drupal::routeMatch()
      ->getParameter('node')) {
      if ($node instanceof \Drupal\node\NodeInterface) {
        $query = $this->connection
          ->select('google_analytics_counter_storage', 'gacs');
        $query
          ->fields('gacs', [
          'pageview_total',
        ]);
        $query
          ->condition('nid', $node
          ->id());
        $sum_pageviews = $query
          ->execute()
          ->fetchField();
      }
    }
    else {

      // Look up the alias, with, and without trailing slash.
      // todo: The array is an accommodation to sumPageViews()
      $aliases = [
        $this->aliasManager
          ->getAliasByPath($path),
      ];

      // dsm($aliases, '$aliases');
      $sum_pageviews = $this
        ->sumPageviews($aliases);

      // dsm($sum_pageviews, '$sum_pageviews');
    }
  }
  return number_format($sum_pageviews);
}