You are here

function yandex_metrics_reports_get_counter_for_current_site in Yandex.Metrics 8.3

Same name and namespace in other branches
  1. 8.2 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.Metrica.

1 call to yandex_metrics_reports_get_counter_for_current_site()
yandex_metrics_reports_report in yandex_metrics_reports/yandex_metrics_reports.module
Menu callback; displays a Summary page containing reports and charts.

File

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

Code

function yandex_metrics_reports_get_counter_for_current_site() {
  $counter_id = variable_get('yandex_metrics_reports_counter_id', '');
  if (!empty($counter_id)) {
    return $counter_id;
  }
  $result = yandex_metrics_reports_retreive_data('/counters', array(
    'field' => 'mirrors',
  ));
  if (isset($result->error)) {
    watchdog('yandex_metrics_reports', 'Counters request seems to be fail, due to "%error".', array(
      '%error' => $result->code . ' ' . $result->error,
    ), WATCHDOG_WARNING);
    return FALSE;
  }
  $counters = json_decode($result->data);
  $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;
  }

  // Collect all registered domains and their mirrors for all counters.
  $registered_hosts = array();
  foreach ($counters->counters as $counter) {

    // If current host is the main counter host.
    if ($counter->site == $current_host) {
      variable_set('yandex_metrics_reports_counter_id', $counter->id);
      return $counter->id;
    }

    // If current host is in site mirrors.
    if (isset($counter->mirrors) && in_array($current_host, $counter->mirrors)) {
      variable_set('yandex_metrics_reports_counter_id', $counter->id);
      return $counter->id;
    }

    // Include counter host and mirrors to the $registered_hosts array
    // in case current host doesn't belong to the counter.
    $registered_hosts[] = $counter->site;
    $registered_hosts = array_merge($registered_hosts, $counter->mirrors);
  }
  watchdog('yandex_metrics_reports', 'The site host does not belong to any Yandex.Metrika counter of the account. Current host: "%host". Known hosts: "%registered_hosts".', array(
    '%host' => $current_host,
    "%registered_hosts" => implode('", "', $registered_hosts),
  ), WATCHDOG_WARNING);
  drupal_set_message(t('The site host does not belong to any Yandex.Metrika counter of the account. Check your counter host and mirrors. Pay special attention to the www. prefix.'), 'error');
  return FALSE;
}