Update! This documentation has been moved into child pages (see below).

Google Analytics Reports module provides graphical reporting of your site's tracking data. Graphical reports include small path-based report in blocks, and a full path-based report.

For installation and configuration help, plus examples, please visit one of the following child pages:

Google Analytics Reports 6.x
Google Analytics Reports 7.x

Comments

mastermindg’s picture

The chart example above is no longer valid. When attempting to run this I'm getting:

[28-May-2013 23:15:07 UTC] PHP Fatal error: Cannot use object of type stdClass as array in /sites/all/modules/custom/mymodule/includes/ga_examples.inc on line 19

I can dump the contents of the array but it looks like it needs to be called as objects ($feed[0]->visits) rather than as an array.

## Note: The above code is fictitious. Any resemblance to real or actual working code is purely coincidental.

eugenesia’s picture

The examples on this page seem to be for Drupal 6, and are not working with Drupal 7 and the latest dev versions of chart (7.x-1.1+12-dev) and google_analytics_reports (7.x-3.x-dev) modules. Here are the updated examples for Drupal 7. Where changes have been made, I have made comments starting with "UPDATED".

Updated examples for Drupal 7.x

  /** 
   * 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;
  }

  // UPDATED: Was using "$feed->results".
  foreach ($feed->results->rows as $row) {
    $data[] = $row['avgTimeOnPage'];
    $dates[] = date('d M ', strtotime($row['date']));
  }

  $chart = array(
    // UPDATED: Added title to chart. A PHP notice will be emitted for an
    // untitled chart.
    '#title' => 'My chart',
    '#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);
  }
  // UPDATED: Was using obsolete function "chart_render($chart)".
  $output = theme('chart', array('chart' => $chart));
  /** 
   * 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;
  }
  // UPDATED: Was using "$feed->results".
  foreach ($feed->results->rows as $row) {
    $data[] = $row['visits'];
    $browsers[] = $row['browser'];
  }
  $chart = array(
    // UPDATED: Added title to chart.
    '#title' => 'My chart 2',
    // UPDATED: Changed chart id to be different from previous chart's id.
    // If both charts appear together and have the same id, both will
    // appear the same which is wrong.
    '#chart_id' => 'mychartid2',
    '#data' => $data,
    '#type' => CHART_TYPE_PIE,
    '#size' => chart_size(400, 200),
    '#labels' => $browsers,
  );
  // UPDATED: Was using obsolete function "chart_render($chart)".
  $output = theme('chart', array('chart' => $chart));
mihai7221’s picture

Hello, thanks for the updated examples, but were do I type this code ?

OlyN’s picture

You can output it in a page callback which you call using hook_menu(), or in a function generating markup for a block, called using the pair of hooks hook_block_info() and hook_block_view(). All this would be added in a module. Hope that helps :)

kylethebaker’s picture

There are even more changes now that havn't been reflected in the docs. The function to get report data is no longer google_analytics_api_report_data(). Now it is google_analytics_reports_api_report_data(). I'm sure there will be others too, but this is the first one I encountered.

Input’s picture

imho there is a little lack of documentation.

i like the views integration cause i'm more the click guy ;)

can someone confirm if the stuff below is really working as i'm thinking?

created block via views
Fields: Time Dimensions: ga:date and Page Tracking Metrics: ga:pageviews
Filter: Google Analytics: Start/End date of report
Sort: Time Dimensions: ga:date (aufsteigend)

Had some trouble to get this block working for the actual page and ended up with this as contextual filter:
Page Tracking Dimensions: ga:pagePath
with return request_uri() . ';ga:pagePath=~' . request_uri() . '(#.*)?$'; as "Provide Default Value"

only using request_uri etc.. to return something link ga:pagePath=@/path which returns also subpaths. =@ only checks for containing substring. by this way "/path1/path" would also be checked by "/path1" . so extending it with the rest of the line above (regular expression) combines the filters. the views integration always sets the filter to "ga:pagePath=@" So I think combination matches the current url.

sorry if i'm posting this at wrong position. but i think it's more documentation than an issue. Maybe there should be a subpage called "My freaky GAR-Views-Setup" or so

MakeOnlineShop’s picture

Thank you for this module and explanation

dougouverson’s picture

I've been investigating solutions that generate email Analytics reports: https://briefmetrics.com and https://www.measureful.com/ to name a few.

I was curious if it's possible to have Drupal email "prettified" Analytics report?