You are here

public function GoogleAnalyticsCounterAppManager::gacUpdateStorage in Google Analytics Counter 8.3

Save the pageview count for a given node.

Parameters

integer $nid: The node id.

string $bundle: The content type of the node.

int $vid: Revision id value.

Throws

\Exception

Overrides GoogleAnalyticsCounterAppManagerInterface::gacUpdateStorage

File

src/GoogleAnalyticsCounterAppManager.php, line 286

Class

GoogleAnalyticsCounterAppManager
Class GoogleAnalyticsCounterAppManager.

Namespace

Drupal\google_analytics_counter

Code

public function gacUpdateStorage($nid, $bundle, $vid) {

  // Get all the aliases for a given node id.
  $aliases = [];
  $path = '/node/' . $nid;
  $aliases[] = $path;
  foreach ($this->languageManager
    ->getLanguages() as $language) {
    $alias = $this->aliasManager
      ->getAliasByPath($path, $language
      ->getId());
    $aliases[] = $alias;
    if ($language
      ->getId()) {
      $aliases[] = '/' . $language
        ->getId() . $path;
      $aliases[] = '/' . $language
        ->getId() . $alias;
    }
  }

  // Add also all versions with a trailing slash.
  $aliases = array_merge($aliases, array_map(function ($path) {
    return $path . '/';
  }, $aliases));

  // It's the front page
  if ($this->pathMatcher
    ->isFrontPage()) {
    $sum_pageviews = $this
      ->sumPageviews([
      '/',
    ]);
    $this
      ->updateCounterStorage($nid, $sum_pageviews, $bundle, $vid);
  }
  else {
    $sum_pageviews = $this
      ->sumPageviews(array_unique($aliases));
    $this
      ->updateCounterStorage($nid, $sum_pageviews, $bundle, $vid);
  }
}