You are here

function google_analytics_counter_cron in Google Analytics Counter 7.3

Same name and namespace in other branches
  1. 8.3 google_analytics_counter.module \google_analytics_counter_cron()
  2. 7.2 google_analytics_counter.module \google_analytics_counter_cron()

Implements hook_cron().

File

./google_analytics_counter.module, line 289
Basic functions for this module.

Code

function google_analytics_counter_cron() {

  // Set a watchdog error if there is no Google Analytics profile enabled.
  // It's a weak test but better than none.
  if (variable_get('google_analytics_counter_profile_id') == '') {
    watchdog('Google Analytics Counter', t('No Google Analytics profile has been authenticated! Google Analytics Counter can not fetch any new data. Please ' . l(t('authenticate here'), 'admin/config/system/google_analytics_counter/authentication') . '.'), NULL, WATCHDOG_ERROR);
    return;
  }

  // Defaults to an hourly interval. Of course, cron has to be running at least hourly for this to work.
  $interval = 60 * variable_get('google_analytics_counter_cron_interval', 30);

  // $interval must contain value in seconds.
  // We don't want to act every time cron runs (which could be every minute) so keep a time for the next run in a variable.
  if (REQUEST_TIME >= variable_get('google_analytics_counter_cron_next_execution', 0)) {

    // Important to set it before the job because if they take long and there is another cron triggered...
    variable_set('google_analytics_counter_cron_next_execution', REQUEST_TIME + $interval);

    // Retrieve path with counts from Google Analytics into a local table.
    google_analytics_counter_update_path_counts();

    // Now also update the storage table from the local table with the GA data.
    google_analytics_counter_update_storage();
  }
}