You are here

function _google_analytics_reports_visits in Google Analytics Reports 7

Same name and namespace in other branches
  1. 6 google_analytics_reports/google_analytics_reports.pages.inc \_google_analytics_reports_visits()

Renders an img element with a chart of the number of visits over the past 30 days.

1 call to _google_analytics_reports_visits()
google_analytics_reports_summary_page in google_analytics_reports/google_analytics_reports.pages.inc
Page callback for admin/reports/google-analytics.

File

google_analytics_reports/google_analytics_reports.pages.inc, line 63
Page callbacks for google analytics.

Code

function _google_analytics_reports_visits() {
  $params = array(
    'metrics' => array(
      'ga:visits',
    ),
    'dimensions' => array(
      'ga:date',
    ),
    'sort_metric' => array(
      'ga:date',
    ),
    'start_date' => strtotime('-31 days'),
    'end_date' => strtotime('-1 day'),
  );
  $feed = google_analytics_api_report_data($params);
  if ($feed->error) {
    return FALSE;
  }
  $max_visits = 0;
  foreach ($feed->results as $row) {
    $data[] = $row['visits'];
    $max_visits = max($row['visits'], $max_visits);
  }
  $chart = array(
    '#title' => '',
    '#chart_id' => 'visits_large_30d',
    '#data' => $data,
    '#type' => CHART_TYPE_LINE . ':nda',
    '#size' => chart_size(1000, 80),
    '#adjust_resolution' => TRUE,
    '#data_colors' => array(
      'AAAAAA',
    ),
    '#chart_fill' => chart_fill('bg', '00000000'),
    '#shape_markers' => array(
      chart_shape_marker(0, 0, 'B', 0, $color = 'EEEEEE'),
    ),
    '#line_styles' => array(
      chart_line_style(2, 10, 0),
    ),
  );
  $last_day = end($feed->results);
  $title = t('The most visits on a single day was @max.  Yesterday there were @yesterday visits.', array(
    '@max' => $max_visits,
    '@yesterday' => $last_day['visits'],
  ));
  $chart['#attributes'] = array(
    'title' => $title,
    'style' => 'height:' . $chart['#size']['#height'] . 'px; width:100%',
  );
  $output = theme('chart', array(
    'chart' => $chart,
  ));
  return $output;
}