You are here

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

The function generates content of search phrases table ordered by popularity.

Parameters

string $counter_id: Counter id.

string $filter: Date range filter.

Return value

mixed Content table.

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

Code

function yandex_metrics_reports_search_phrases($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'],
  );
  $report_phrases = yandex_metrics_reports_retreive_data('/stat/sources/phrases', $parameters);
  $phrases = json_decode($report_phrases->data);
  if (empty($phrases->data)) {
    return t('There is no information about search phrases for the selected date range.');
  }
  $phrases_totals_visits = $phrases->totals->visits;
  $header = array(
    t('Visits (%)'),
    t('Phrase'),
  );
  $data = array();
  $i = 1;
  foreach ($phrases->data as $value) {
    if ($i > YANDEX_METRICS_REPORTS_SEARCH_PHRASES_QUANTITY) {
      break;
    }
    $percentage = round(100 * $value->visits / $phrases_totals_visits, 2);
    $data[] = array(
      $percentage,
      check_plain($value->phrase),
    );
    $i++;
  }
  return theme('table', array(
    'header' => $header,
    'rows' => $data,
    'caption' => t("Popular Search Phrases"),
  ));
}