You are here

function yandex_metrics_reports_sources_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_sources_chart()
  2. 7.3 yandex_metrics_reports/yandex_metrics_reports.reports.inc \yandex_metrics_reports_sources_chart()
  3. 7.2 yandex_metrics_reports/yandex_metrics_reports.reports.inc \yandex_metrics_reports_sources_chart()

The function generates pie chart with traffic sources summary.

Parameters

string $counter_id: Counter id.

string $filter: Date range filter.

1 string reference to 'yandex_metrics_reports_sources_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 62
Report callbacks for Yandex.Metrics Reports module.

Code

function yandex_metrics_reports_sources_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/sources/summary', $parameters);
  $summary = json_decode($results->data);
  if (empty($summary->data)) {
    return t('There is no information about traffic sources for the selected date range.');
  }
  $chart = array(
    'title' => t('Traffic Sources'),
    'type' => 'pie',
    'fields' => array(
      'sources' => array(
        'label' => t('Sources'),
        'enabled' => FALSE,
      ),
      'visits' => array(
        'label' => t('Visits'),
        'enabled' => TRUE,
      ),
    ),
    'xAxis' => array(
      'labelField' => 'sources',
    ),
  );
  $sum = $summary->totals->visits;
  $i = 1;
  foreach ($summary->data as $value) {
    $name = check_plain($value->name);
    $sources_leneged = $i . '. ' . $name . ' (' . round($value->visits * 100 / $sum) . '%' . ')';
    $chart['data'][] = array(
      'sources' => $sources_leneged,
      'visits' => (int) $value->visits,
    );
    $i++;
  }
  return theme('visualization', array(
    'options' => $chart,
  ));
}