You are here

function google_analytics_cron in Google Analytics 4.x

Same name and namespace in other branches
  1. 8.3 google_analytics.module \google_analytics_cron()
  2. 8.2 google_analytics.module \google_analytics_cron()

Implements hook_cron().

File

./google_analytics.module, line 452
Drupal Module: Google Analytics.

Code

function google_analytics_cron() {
  $config = \Drupal::config('google_analytics.settings');
  $request_time = \Drupal::time()
    ->getRequestTime();
  $javascript_service = \Drupal::service('google_analytics.javascript_cache');
  $ga_accounts = \Drupal::service('google_analytics.accounts');

  // Return prematurely if no default measurement ID was found.
  if (empty($ga_accounts
    ->getDefaultMeasurementId())) {
    return;
  }

  // Regenerate the tracking code file every day.
  if ($request_time - \Drupal::state()
    ->get('google_analytics.last_cache') >= 86400 && $config
    ->get('cache')) {
    $javascript_service
      ->fetchGoogleAnalyticsJavascript($ga_accounts
      ->getDefaultMeasurementId(), TRUE);
    \Drupal::state()
      ->set('google_analytics.last_cache', $request_time);
  }
}