You are here

protected function GoogleAnalyticsCounterAppManager::updateCounterStorage in Google Analytics Counter 8.3

Merge the sum of pageviews into google_analytics_counter_storage.

Parameters

int $nid: Node id value.

int $sum_pageviews: Count of page views.

string $bundle: The content type of the node.

int $vid: Revision id value.

Throws

\Exception

1 call to GoogleAnalyticsCounterAppManager::updateCounterStorage()
GoogleAnalyticsCounterAppManager::gacUpdateStorage in src/GoogleAnalyticsCounterAppManager.php
Save the pageview count for a given node.

File

src/GoogleAnalyticsCounterAppManager.php, line 354

Class

GoogleAnalyticsCounterAppManager
Class GoogleAnalyticsCounterAppManager.

Namespace

Drupal\google_analytics_counter

Code

protected function updateCounterStorage($nid, $sum_pageviews, $bundle, $vid) {
  $this->connection
    ->merge('google_analytics_counter_storage')
    ->key('nid', $nid)
    ->fields([
    'pageview_total' => $sum_pageviews,
  ])
    ->execute();

  // Update the Google Analytics Counter field if it exists.
  if (!$this->connection
    ->schema()
    ->tableExists(static::TABLE)) {
    return;
  }

  // Todo: This can be more performant by adding only the bundles that have been selected.
  $this->connection
    ->upsert('node__field_google_analytics_counter')
    ->key('revision_id')
    ->fields([
    'bundle',
    'deleted',
    'entity_id',
    'revision_id',
    'langcode',
    'delta',
    'field_google_analytics_counter_value',
  ])
    ->values([
    'bundle' => $bundle,
    'deleted' => 0,
    'entity_id' => $nid,
    'revision_id' => $vid,
    'langcode' => \Drupal::languageManager()
      ->getDefaultLanguage()
      ->getId(),
    'delta' => 0,
    'field_google_analytics_counter_value' => $sum_pageviews,
  ])
    ->execute();
}