You are here

function google_analytics_counter_update_storage in Google Analytics Counter 7.3

Get pageviews for nodes and write them either to the Drupal core table node_counter, or to the google_analytics_counter_storage table. This function is triggered by hook_cron().

1 call to google_analytics_counter_update_storage()
google_analytics_counter_cron in ./google_analytics_counter.module
Implements hook_cron().

File

./google_analytics_counter_data.inc, line 420
Parsing and writing the fetched data.

Code

function google_analytics_counter_update_storage() {
  if (variable_get('google_analytics_counter_storage', 0) == 0 && module_exists('statistics')) {

    // See also https://www.drupal.org/node/2275575
    // Using core node_counter table.
    $storage = 'node_counter';
  }
  else {

    // Using table google_analytics_counter_storage.
    $storage = 'google_analytics_counter_storage';
  }

  // Record how long did this chunk take to process.
  $chunkprocessbegin = time();

  //dpm($chunkprocessbegin);

  // The total number of nodes.
  $query = db_select('node', 'n')
    ->fields('n');
  drupal_alter('google_analytics_counter_query', $query);
  $dbresult = $query
    ->execute();
  $resultcount = $dbresult
    ->rowCount();

  //dpm('totalnodes: '.$resultcount);

  // Store it in a variable.
  variable_set('google_analytics_counter_totalnodes', $resultcount);

  // How many node counts to update one cron run.
  // We use the same chunk size as when getting paths in google_analytics_counter_update_path_counts().
  $chunk = variable_get('google_analytics_counter_chunk_to_fetch', 1000);

  // In case there are more than $chunk nodes to process, do just one chunk at a time and register that in $step.
  $step = variable_get('google_analytics_counter_node_data_step', 0);

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

  //dpm('START chunk '.$chunk);

  //dpm('START step '.$step);

  //dpm('START pointer '.$pointer);
  $updated_nids = array();
  $query = db_select('node', 'n');
  if ($storage == 'node_counter') {
    $query
      ->leftJoin('node_counter', 'c', 'c.nid = n.nid');
    $query = $query
      ->fields('c', array(
      'totalcount',
    ));
  }
  else {
    $query
      ->leftJoin('google_analytics_counter_storage', 'c', 'c.nid = n.nid');
    $query = $query
      ->fields('c', array(
      'pageview_total',
    ));
  }
  $query = $query
    ->fields('n', array(
    'nid',
  ))
    ->range($pointer, $chunk);
  drupal_alter('google_analytics_counter_query', $query);
  $dbresults = $query
    ->execute();
  foreach ($dbresults as $dbresult) {
    $path = 'node/' . $dbresult->nid;

    //dpm($path);

    // Get the count for this node (uncached)
    $sum_of_pageviews = google_analytics_counter_get_sum_per_path($path, FALSE);

    // Don't write zeroes.
    if ($sum_of_pageviews == 0) {
      continue;
    }

    // Write the count to the current storage table
    if ($storage == 'node_counter') {
      db_merge('node_counter')
        ->key(array(
        'nid' => $dbresult->nid,
      ))
        ->fields(array(
        'daycount' => 0,
        'totalcount' => $sum_of_pageviews,
        'timestamp' => REQUEST_TIME,
      ))
        ->execute();
      if ($dbresult->totalcount != $sum_of_pageviews) {
        $updated_nids[$dbresult->nid] = $sum_of_pageviews;
      }
    }
    elseif ($dbresult->pageview_total != $sum_of_pageviews) {
      db_merge('google_analytics_counter_storage')
        ->key(array(
        'nid' => $dbresult->nid,
      ))
        ->fields(array(
        'pageview_total' => $sum_of_pageviews,
      ))
        ->execute();
      $updated_nids[$dbresult->nid] = $sum_of_pageviews;
    }
  }
  if (!empty($updated_nids)) {
    module_invoke_all('google_analytics_counter_update', $updated_nids);
  }

  // Set the pointer.
  $pointer += $chunk;

  //dpm('END pointer: '.$pointer);

  //dpm('step: '.$step.' | '.$pointer . ' out of total ' .$resultcount);
  watchdog('Google Analytics Counter', t('Attempted updating %dbresults records in table ' . $storage . ' from Google Analytics data %first-%second.', array(
    '%dbresults' => $dbresults
      ->rowCount(),
    '%first' => $pointer - $chunk + 1,
    '%second' => $pointer - $chunk + $dbresults
      ->rowCount(),
  )), NULL, WATCHDOG_INFO);

  // OK now increase or zero $step
  if ($pointer < $resultcount) {

    // If there are more results than what we've reached with this chunk, increase step to look further during the next run.
    $newstep = $step + 1;

    //dpm('step +1: '.$newstep);
  }
  else {
    $newstep = 0;

    //dpm('step zero: '.$newstep);
  }

  //dpm('newstep: '.$newstep);
  variable_set('google_analytics_counter_node_data_step', $newstep);

  // Record how long did this chunk take to process.
  variable_set('google_analytics_counter_chunk_node_process_time', time() - $chunkprocessbegin);
}