Advertisement
zniki

Yandex.Metrics 1.6

Sep 18th, 2013
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.87 KB | None | 0 0
  1. /**
  2.  * Gets counter ID for the current site from Yandex.Metrics.
  3.  */
  4. function yandex_metrics_get_counter_for_current_site() {
  5.  
  6.   $counter_id = variable_get('yandex_metrics_counter_id', '');
  7.   if (!empty($counter_id)) {
  8.     return $counter_id;
  9.   }
  10.  
  11.   $result = yandex_metrics_retreive_data('/counters', array('field' => 'mirrors'));
  12.  
  13.   if (isset($result->error)) {
  14.     watchdog('yandex_metrics', 'Counters request seems to be fail, due to "%error".', array('%error' => $result->code . ' ' . $result->error), WATCHDOG_WARNING);
  15.     return;
  16.   }
  17.  
  18.   $counters = json_decode($result->data);
  19.  
  20.   $current_host = $_SERVER['HTTP_HOST'];
  21.  
  22.   // Try to decode national domain.
  23.   $decoded_domain = _yandex_metrics_idna_decode($current_host);
  24.  
  25.   if ($decoded_domain != FALSE && $decoded_domain != $current_host) {
  26.       $current_host = $decoded_domain;
  27.   }
  28.  
  29.   // list of domains, mirrors for current counter;
  30.   $available_domains = array();
  31.   foreach ($counters->counters as $key => $counter) {
  32.     if ($counter->site == $current_host) {
  33.       variable_set('yandex_metrics_counter_id', $counter->id);
  34.       return $counter->id;
  35.     }
  36.     else {
  37.       $available_domains[] = $counter->site;
  38.     }
  39.    
  40.     // If current host is equal of any site mirror.
  41.     if (isset($counter->mirrors)) {
  42.       if(in_array($current_host, $counter->mirrors)) {
  43.         variable_set('yandex_metrics_counter_id', $counter->id);
  44.         return $counter->id;
  45.       }
  46.       else {
  47.         $available_domains = array_merge($available_domains, $counter->mirrors);
  48.       }
  49.     }
  50.   }
  51.  
  52.   if (!empty($available_domains)) {
  53.     watchdog('yandex_metrics', 'No legal domains or mirrors for this host. Current host: "%host"; available domains: "%available_domains".', array('%error' => $current_host, "%available_domains" => implode(", ", $available_domains) ), WATCHDOG_WARNING);
  54.   }
  55.   return FALSE;
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement