Community Documentation

Google Analytics Reports

Last updated November 9, 2012. Created by raspberryman on April 26, 2011.
Edited by cmejo, grendzy. Log in to edit this page.

About this page

This page includes information and examples for using Google Analytics Reports and its API module.

Installation

The reports module requires OAuth, Chart, and Autoload. Additionally, your web host must have the OpenSSL PHP extension enabled.

After installing the module, assign the permissions 'administer google analytics reports' and 'access google analytics reports' as needed. Then visit admin/settings/google-analytics-reports to authenticate with Google. The bundled reports are available at admin/reports/google-analytics and via two blocks.

Suggested modules

Having a single, canonical URL for every page will help ensure the accuracy of per-page reports. Installing the Global Redirect or Redirect module is suggested.

Customizing reports

Reports can be customized in several ways:

Preparing your custom report

The best way to brainstorm your perfect report is to use Google's Data Feed Query Explorer. This tool will give you all the fields you need, and help you debug query issues.

The primary API consists of two functions: google_analytics_api_report_data() and google_analytics_api_account_data(). Other helper functions should be considered private, and may change as the UI evolves.

Understand Chart API and Google Charts

Study the examples offered by the Drupal Chart API module located on their wiki page. In addition, Google offers the Chart Wizard, which is a very advanced and helpful tool.

Examples

Here are a few examples to get you started. We're always welcoming additions for this page in our issue queue.

<?php
 
/**
   * Line chart showing the average time spent by
   * mobile users on your site over the past month.
   */
 
$data = array();
 
$dates = array();
 
$params = array(
   
'metrics' => array('ga:avgTimeOnPage'),
   
'dimensions' => array('ga:date'),
   
'segment' => 'gaid::-11',
   
'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;
  }
  foreach (
$feed->results as $row) {
   
$data[] = $row['avgTimeOnPage'];
   
$dates[] = date('d M ', strtotime($row['date']));
  }

 
$chart = array(
   
'#chart_id' => 'mychartid',
   
'#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)),
  );
 
/* Generally speaking, your metric will will be your Y axis... */
 
$chart['#mixed_axis_labels'][CHART_AXIS_Y_LEFT][0][] = chart_mixed_axis_range_label(0, max($data));
 
/* ... and your dimensions will be your X axis. */
 
foreach ($dates as $date) {
   
$chart['#mixed_axis_labels'][CHART_AXIS_X_BOTTOM][1][] = chart_mixed_axis_label($date);
  }
 
$output = chart_render($chart);
?>

<?php
 
/**
   * Pie chart showing the top 10 browsers of new
   * visitors over the past month.
   */ 
 
$params = array(
   
'metrics' => array('ga:visits'),
   
'dimensions' => array('ga:browser'),
   
'segment' => 'gaid::-2',
   
'sort_metric' => array('-ga:visits'),
   
'start_date' => strtotime('-31 days'),
   
'end_date' => strtotime('-1 day'),
   
'max_results' => 10,
  );
 
$feed = google_analytics_api_report_data($params);
  if (
$feed->error) {
    return
FALSE;
  }
  foreach (
$feed->results as $row) {
   
$data[] = $row['visits'];
   
$browsers[] = $row['browser'];
  }
 
$chart = array(
   
'#chart_id' => 'mychartid',
   
'#data' => $data,
   
'#type' => CHART_TYPE_PIE,
   
'#size' => chart_size(400, 200),
   
'#data' => $data,
   
'#labels' => $browsers,
  );
 
$output = chart_render($chart);
?>

Related modules

There are several other contributed modules that make use of the API:

Have examples?

If you have examples for this page, please submit them via an issue.

Page status

No known problems

Log in to edit this page

About this page

Drupal version
Drupal 6.x
Audience
Designers/themers, Programmers, Site administrators

Site Building Guide

Drupal’s online documentation is © 2000-2013 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License. Comments on documentation pages are used to improve content and then deleted.
nobody click here