You are here

function yandex_metrics_reports_save_popular_content in Yandex.Metrics 8.2

Same name and namespace in other branches
  1. 6.2 yandex_metrics_reports/yandex_metrics_reports.module \yandex_metrics_reports_save_popular_content()
  2. 7.2 yandex_metrics_reports/yandex_metrics_reports.module \yandex_metrics_reports_save_popular_content()

Fetch Popuplar content from Yandex.metrika and save it to the database.

Parameters

bool $cron - Indicates that we run function in cron or another place.:

1 call to yandex_metrics_reports_save_popular_content()
yandex_metrics_reports_cron in yandex_metrics_reports/yandex_metrics_reports.module
Implements hook_cron().

File

yandex_metrics_reports/yandex_metrics_reports.module, line 172
The main code of Yandex.Metrics Reports module.

Code

function yandex_metrics_reports_save_popular_content($cron = FALSE) {
  $counter_code = \Drupal::config('yandex_metrics.settings')
    ->get('counter_code');
  if (empty($counter_code) && !$cron) {
    drupal_set_message(t('Perhaps you have not yet placed Yandex.Metrics counter code on the site. You can do this !link.', array(
      '!link' => l(t('here'), 'admin/settings/yandex_metrics'),
    )), 'notice');
    return;
  }
  $counter_id = yandex_metrics_reports_get_counter_for_current_site();
  if (empty($counter_id)) {
    if ($cron) {
      watchdog('yandex_metrics_reports', 'Cron: counter ID is not set.', array(), WATCHDOG_WARNING);
    }
    else {
      drupal_set_message(t('Please create Yandex.Metrics counter for the site first. See more details !link.', array(
        '!link' => l(t('here'), 'admin/config/system/yandex_metrics'),
      )), 'error');
    }
    return;
  }
  $authorisation_token = yandex_services_auth_info('token');
  if (empty($authorisation_token)) {
    if ($cron) {
      watchdog('yandex_metrics_reports', 'Cron: application is not authorised.', array(), WATCHDOG_WARNING);
    }
    else {
      drupal_set_message(t('Please make sure that your application is authorized !link.', array(
        '!link' => l(t('here'), 'admin/settings/yandex_metrics/authorization'),
      )), 'error');
    }
    return;
  }
  $filter = \Drupal::config('yandex_metrics_reports.settings')
    ->get('popular_content_date_period');
  $date_range = _yandex_metrics_reports_filter_to_date_range($filter);
  $parameters = array(
    'id' => $counter_id,
    'date1' => $date_range['start_date'],
    'date2' => $date_range['end_date'],
  );

  // Fetch popular content urls.
  $report_content = yandex_metrics_reports_retreive_data('/stat/content/popular', $parameters);
  $content = json_decode($report_content->data);

  // Check for errors.
  if (isset($content->errors) && count($content->errors) > 0 && $content->errors[0]->code != 'ERR_NO_DATA') {
    watchdog('yandex_metrics_reports', 'Fetching of popular content failed due to error %error.', array(
      '%error' => $content->errors[0]->text,
    ), WATCHDOG_ERROR);
    return;
  }

  // Remove outdated data.
  db_truncate('yandex_metrics_reports_popular_content')
    ->execute();
  if (empty($content->data)) {
    watchdog('yandex_metrics_reports', 'There is no popular content for the selected date period %filter.', array(
      '%filter' => $filter,
    ), WATCHDOG_NOTICE);
    return;
  }
  $counter = 1;
  foreach ($content->data as $value) {
    $parsed_url = parse_url($value->url);

    // Ignore external urls.
    // Let's consider third level domains as internal, e.g. es.domain.com.
    global $base_root;
    $base_root_parts = parse_url($base_root);
    $pattern = "/(\\.|^)" . preg_quote($base_root_parts['host'], "/") . "\$/i";
    if (!preg_match($pattern, $parsed_url['host'], $matches)) {
      continue;
    }

    // Obtain page language.
    $page_language = yandex_metrics_reports_language_from_url($value->url);

    // Obtain page title if it is possible.
    $page_title = yandex_metrics_reports_obtain_page_title($parsed_url['path'], $page_language);
    db_insert('yandex_metrics_reports_popular_content')
      ->fields(array(
      'url' => $value->url,
      'language' => $page_language,
      'page_title' => $page_title,
      'page_views' => $value->page_views,
    ))
      ->execute();
    if ($counter++ >= YANDEX_METRICS_REPORTS_POPULAR_CONTENT_BLOCK_LIMIT) {
      break;
    }
  }
  watchdog('yandex_metrics_reports', 'Popular content for %filter has been fetched.', array(
    '%filter' => $filter,
  ), WATCHDOG_NOTICE);
}