You are here

function yandex_metrics_reports_traffic_hourly_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_traffic_hourly_chart()
  2. 7.3 yandex_metrics_reports/yandex_metrics_reports.reports.inc \yandex_metrics_reports_traffic_hourly_chart()
  3. 7.2 yandex_metrics_reports/yandex_metrics_reports.reports.inc \yandex_metrics_reports_traffic_hourly_chart()

The function generates chart with information about hourly traffic.

Parameters

string $counter_id: Counter id.

string $filter: Date range filter.

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

Code

function yandex_metrics_reports_traffic_hourly_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'],
  );
  if (isset($date_range['group'])) {
    $parameters['group'] = $date_range['group'];
  }
  $results = yandex_metrics_reports_retreive_data('/stat/traffic/hourly', $parameters);
  $hourly_report = json_decode($results->data);
  if (empty($hourly_report->data)) {
    return t('There is no information about hourly traffic for the selected date range.');
  }
  $chart = array(
    'title' => t('Hourly Traffic'),
    'type' => 'column',
    'fields' => array(
      'hours' => array(
        'label' => t('Hours'),
        'enabled' => FALSE,
      ),
      'avg_visits' => array(
        'label' => t('Visits per hour'),
        'enabled' => TRUE,
      ),
      'denials' => array(
        'label' => t('Denials (visits with only one page view)'),
        'enabled' => TRUE,
      ),
    ),
    'xAxis' => array(
      'labelField' => 'hours',
    ),
  );
  foreach ($hourly_report->data as $hour_data) {
    list($hour, ) = explode(':', $hour_data->hours);
    $chart['data'][] = array(
      'hours' => $hour,
      'avg_visits' => round($hour_data->avg_visits, 2),
      // Convert denials from percents.
      'denials' => round($hour_data->avg_visits * $hour_data->denial, 2),
    );
  }
  return theme('visualization', array(
    'options' => $chart,
  ));
}