You are here

function google_analytics_counter_update_path_counts in Google Analytics Counter 7.3

Same name and namespace in other branches
  1. 7.2 google_analytics_counter_data.inc \google_analytics_counter_update_path_counts()

Find how many distinct paths does Google Analytics have for this profile. This function is triggered by hook_cron().

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

File

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

Code

function google_analytics_counter_update_path_counts() {

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

  // Needing to stay under the Google Analytics API quota, let's count how many API retrievals were made in the last 24 hours.
  // @todo We should better take into consideration that the quota is reset at midnight PST (note: time() always returns UTC).
  $dayquota = variable_get('google_analytics_counter_dayquota', array(
    0,
    0,
  ));
  if (REQUEST_TIME - $dayquota[0] >= 86400) {

    // If last API request was more than a day ago, set monitoring time to now.
    $dayquota[0] = REQUEST_TIME;
    $dayquota[1] = 0;
    variable_set('google_analytics_counter_dayquota', array(
      $dayquota[0],
      $dayquota[1],
    ));
  }

  //dpm($dayquota);

  // Are we over the GA API limit?
  $maxdailyrequests = variable_get('google_analytics_counter_api_dayquota', 10000);

  // See http://code.google.com/apis/analytics/docs/gdata/gdataDeveloperGuide.html#quota
  if ($dayquota[1] > $maxdailyrequests) {
    watchdog('Google Analytics Counter', t('Google Analytics API quota of %maxdailyrequests requests has been reached. Will NOT fetch data from Google Analytics for the next %dayquota seconds. See <a href="/admin/config/system/google_analytics_counter">the Google Analytics Counter settings page</a> for more info.', array(
      '%maxdailyrequests' => check_plain($maxdailyrequests),
      '%dayquota' => check_plain($dayquota[0] + 86400 - REQUEST_TIME),
    )), NULL, WATCHDOG_ERROR);
    return;
  }

  // How many results to ask from GA in one request. Default on 1000 to fit most systems (for example those with no external cron).
  $chunk = variable_get('google_analytics_counter_chunk_to_fetch', 1000);

  // In case there are more than $chunk path/counts to retrieve from GA, do just one chunk at a time and register that in $step.
  $step = variable_get('google_analytics_counter_data_step', 0);

  // Which GA result to look for first. Must be between 1 - infinity.
  $pointer = $step * $chunk + 1;

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

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

  //dpm('START pointer '.$pointer);
  $start_date = google_analytics_counter_get_start_date();

  // The earliest valid start-date for Google Analytics is 2005-01-01.
  $request = array(
    'dimensions' => array(
      'ga:pagePath',
    ),
    // date would not be necessary for totals, but we also calculate stats of views per day, so we need it
    'metrics' => array(
      'ga:pageviews',
    ),
    'start_date' => strtotime($start_date),
    'end_date' => strtotime('tomorrow'),
    // Using 'tomorrow' to offset any timezone shift between the hosting and Google servers.
    'start_index' => $pointer,
    'max_results' => $chunk,
  );

  //dpm($start_date);
  drupal_alter('google_analytics_counter_request', $request);
  $resultcount = FALSE;
  $cachehere = array(
    'cid' => 'google_analytics_counter_' . md5(serialize($request)),
    'expire' => google_analytics_counter_cache_time(),
    'refresh' => FALSE,
  );
  $new_data = @google_analytics_counter_report_data($request, $cachehere);
  if (!$new_data->fromCache) {

    // Don't write anything to google_analytics_counter if this GA data comes from cache (would be writing the same again).
    // This was a live request. Increase the GA request limit tracker.
    variable_set('google_analytics_counter_dayquota', array(
      $dayquota[0],
      $dayquota[1] + 1,
    ));
    if (!empty($new_data->error)) {

      // If NULL then there is no error.
      watchdog('Google Analytics Counter', t('Problem fetching data from Google Analytics: %new_dataerror. Did you authenticate any Google Analytics profile? See <a href="/admin/config/system/google_analytics_counter/authentication">here</a>.', array(
        '%new_dataerror' => $new_data->error,
      )), NULL, WATCHDOG_ERROR);

      // Nothing to do; return.

      //return;
    }
    else {
      $resultsretrieved = $new_data->results->rows;
      foreach ($resultsretrieved as $val) {

        // http://drupal.org/node/310085
        db_merge('google_analytics_counter')
          ->key(array(
          'pagepath_hash' => md5($val['pagePath']),
        ))
          ->fields(array(
          'pagepath' => check_plain(utf8_encode($val['pagePath'])),
          // check_plain: https://www.drupal.org/node/2381703 // utf8_encode: https://www.drupal.org/node/2382319
          'pageviews' => check_plain($val['pageviews']),
        ))
          ->execute();
      }
    }
  }

  // The total number of records for this profile.
  $resultcount = @$new_data->results->totalResults;

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

  // The total number of hits for all records for this profile.
  $totalhits = @$new_data->results->totalsForAllResults['pageviews'];
  variable_set('google_analytics_counter_totalhits', $totalhits);

  //dpm('totalhits: '.$totalhits);

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

  //dpm('step: '.$step.' | '.$pointer . ' out of total ' .$resultcount);
  watchdog('Google Analytics Counter', t('Retrieved %sizeof items from Google Analytics data for paths %first-%second.', array(
    '%sizeof' => sizeof(@$new_data->results->rows),
    '%first' => $pointer - $chunk,
    '%second' => $pointer - $chunk - 1 + sizeof(@$new_data->results->rows),
  )), 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;
  }
  else {
    $newstep = 0;
  }

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

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