You are here

function google_analytics_counter_update_node_counter in Google Analytics Counter 7.2

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

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

File

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

Code

function google_analytics_counter_update_node_counter() {

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

  //dpm($chunkprocessbegin);

  // The total number of nodes.
  $dbresult = db_select('node', 'n')
    ->fields('n')
    ->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);
  $dbresults = db_select('node', 'n')
    ->fields('n', array(
    'nid',
  ))
    ->range($pointer, $chunk)
    ->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 Drupal core table node_counter
    db_merge('node_counter')
      ->key(array(
      'nid' => $dbresult->nid,
    ))
      ->fields(array(
      'daycount' => 0,
      'totalcount' => $sum_of_pageviews,
      'timestamp' => REQUEST_TIME,
    ))
      ->execute();
  }

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

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

  //dpm('step: '.$step.' | '.$pointer . ' out of total ' .$resultcount);
  watchdog(t('Google Analytics Counter'), t('Attempted updating %dbresults records in node_counter 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);
}