You are here

function yandex_metrics_get_counter_for_current_site in Yandex.Metrics 6

Same name and namespace in other branches
  1. 7 yandex_metrics.module \yandex_metrics_get_counter_for_current_site()

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

2 calls to yandex_metrics_get_counter_for_current_site()
yandex_metrics_report in ./yandex_metrics.module
Menu callback; displays a Summary page containing reports and charts.
yandex_metrics_save_popular_content in ./yandex_metrics.module
Fetch Popuplar content from Yandex.metrika and save it to the database.

File

./yandex_metrics.module, line 800
The main code of Yandex.Metrics module.

Code

function yandex_metrics_get_counter_for_current_site() {
  $counter_id = variable_get('yandex_metrics_counter_id', '');
  if (!empty($counter_id)) {
    return $counter_id;
  }
  $result = yandex_metrics_retreive_data('/counters', array(
    'field' => 'mirrors',
  ));
  if (isset($result->error)) {
    watchdog('yandex_metrics', 'Counters request seems to be fail, due to "%error".', array(
      '%error' => $result->code . ' ' . $result->error,
    ), WATCHDOG_WARNING);
    return;
  }
  $counters = json_decode($result->data);
  $current_host = $_SERVER['HTTP_HOST'];

  // Try to decode national domain.
  $decoded_domain = _yandex_metrics_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) {
      variable_set('yandex_metrics_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)) {
      variable_set('yandex_metrics_counter_id', $counter->id);
      return $counter->id;
    }
  }
  return FALSE;
}