You are here

function yandex_metrics_reports_get_counter_for_current_site in Yandex.Metrics 8.2

Same name and namespace in other branches
  1. 8.3 yandex_metrics_reports/yandex_metrics_reports.module \yandex_metrics_reports_get_counter_for_current_site()
  2. 6.2 yandex_metrics_reports/yandex_metrics_reports.module \yandex_metrics_reports_get_counter_for_current_site()
  3. 7.3 yandex_metrics_reports/yandex_metrics_reports.module \yandex_metrics_reports_get_counter_for_current_site()
  4. 7.2 yandex_metrics_reports/yandex_metrics_reports.module \yandex_metrics_reports_get_counter_for_current_site()

Gets counter ID for the current site from Yandex.Metrics.

2 calls to yandex_metrics_reports_get_counter_for_current_site()
YandexMetricsReportsController::report in yandex_metrics_reports/src/Controller/YandexMetricsReportsController.php
Menu callback; displays a Summary page containing reports and charts.
yandex_metrics_reports_save_popular_content in yandex_metrics_reports/yandex_metrics_reports.module
Fetch Popuplar content from Yandex.metrika and save it to the database.

File

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

Code

function yandex_metrics_reports_get_counter_for_current_site() {
  $counter_id = \Drupal::state()
    ->get('yandex_metrics_reports_counter_id');
  if (!empty($counter_id)) {
    return $counter_id;
  }
  $result = yandex_metrics_reports_retreive_data('/counters', array(
    'field' => 'mirrors',
  ));
  if (!$result) {
    watchdog('yandex_metrics_reports', 'Counters request seems to be fail.', WATCHDOG_WARNING);
    return;
  }
  $counters = json_decode($result
    ->getBody(TRUE));
  $current_host = $_SERVER['HTTP_HOST'];

  // Try to decode national domain.
  $decoded_domain = _yandex_metrics_reports_idna_decode($current_host);
  if ($decoded_domain != FALSE && $decoded_domain != $current_host) {
    $current_host = $decoded_domain;
  }
  foreach ($counters->counters as $key => $counter) {
    if ($counter->site == $current_host) {
      \Drupal::state()
        ->set('yandex_metrics_reports_counter_id', $counter->id);
      return $counter->id;
    }

    // If current host is equal of any site mirror.
    if (isset($counter->mirrors) && in_array($current_host, $counter->mirrors)) {
      \Drupal::state()
        ->set('yandex_metrics_reports_counter_id', $counter->id);
      return $counter->id;
    }
  }
  return FALSE;
}