You are here

function yandex_metrics_reports_geo_chart in Yandex.Metrics 8.3

Same name and namespace in other branches
  1. 8.2 yandex_metrics_reports/yandex_metrics_reports.reports.inc \yandex_metrics_reports_geo_chart()
  2. 7.3 yandex_metrics_reports/yandex_metrics_reports.reports.inc \yandex_metrics_reports_geo_chart()
  3. 7.2 yandex_metrics_reports/yandex_metrics_reports.reports.inc \yandex_metrics_reports_geo_chart()

The function generates pie chart with geographical information on visitors.

Parameters

string $counter_id: Counter id.

string $filter: Date range filter.

1 string reference to 'yandex_metrics_reports_geo_chart'
yandex_metrics_reports_yandex_metrics_reports_list in yandex_metrics_reports/yandex_metrics_reports.module
Implements hook_yandex_metrics_reports_list().

File

yandex_metrics_reports/yandex_metrics_reports.reports.inc, line 232
Report callbacks for Yandex.Metrics Reports module.

Code

function yandex_metrics_reports_geo_chart($counter_id, $filter) {
  $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'],
  );
  $results = yandex_metrics_reports_retreive_data('/stat/geo', $parameters);
  $geo = json_decode($results->data);
  if (empty($geo->data)) {
    return t('There is no information about geography of visits for the selected date range.');
  }
  $chart = array(
    'title' => t('Geography of Visits'),
    'type' => 'pie',
    'fields' => array(
      'country' => array(
        'label' => t('Country'),
        'enabled' => FALSE,
      ),
      'visits' => array(
        'label' => t('Visits'),
        'enabled' => TRUE,
      ),
    ),
    'xAxis' => array(
      'labelField' => 'country',
    ),
  );
  $total_visits = $geo->totals->visits;

  // Exclude unknown visits.
  foreach ($geo->data as $key => $value) {
    if ($value->name == "Не определено") {
      $total_visits -= $value->visits;
      unset($geo->data[$key]);
      break;
    }
  }
  $i = 1;
  $sum_visits = 0;
  foreach ($geo->data as $value) {
    $visits = (int) $value->visits;
    if ($i > 10) {
      $others_visits = $total_visits - $sum_visits;
      $chart['data'][] = array(
        'country' => t('Others'),
        'visits' => $others_visits,
      );
      break;
    }
    $sum_visits += $visits;
    $name = check_plain($value->name);
    $chart['data'][] = array(
      'country' => $name,
      'visits' => $visits,
    );
    $i++;
  }
  return theme('visualization', array(
    'options' => $chart,
  ));
}