You are here

function ga_stats_ga_data in Google Analytics Statistics 7.2

Same name and namespace in other branches
  1. 7 includes/ga.inc \ga_stats_ga_data()
  2. 7.x inc/ga.inc \ga_stats_ga_data()

Use the client library to query GA for fresh data.

Parameters

gapi $client: @param array|string metrics The metrics to retrieve. @param int start_date Beginning of the time range. @param int end_date End of the time range. @param $filter

@return array Retrieve an array of "external_statistics_count" objects.

1 call to ga_stats_ga_data()
ga_stats_collect_data in includes/ga.inc
Retrieve data for all configured metrics and time-frames.

File

includes/ga.inc, line 133

Code

function ga_stats_ga_data($client, $metrics, $start_date = 0, $end_date = 0, $filter = FALSE) {
  $url_dim = 'pagePath';
  if (!is_array($metrics)) {
    $metrics = array(
      $metrics,
    );
  }
  $request['dimensions'] = array(
    $url_dim,
  );
  $request['metrics'] = $metrics;
  $request['start_date'] = $start_date ? date('Y-m-d', $start_date) : NULL;
  $request['end_date'] = $end_date ? date('Y-m-d', $end_date) : NULL;
  $request['sort_metric'] = "-" . $metrics[0];
  $request['max_results'] = variable_get('ga_stats_max_results', "100");
  if ($filter) {
    $request['filter'] = $filter;
  }
  $data_raw = ga_stats_query_data($client, $request);

  // @todo move "data array" stuff up the callstack so this message can follow.
  if (empty($data_raw)) {
    drupal_set_message(t('Failed to retrieve data from Google Analytics.'), 'error', FALSE);
  }
  $data_array = ga_stats_ga_data_array($data_raw);
  foreach ($data_array as $k => $d) {
    $data_array[$k]['url'] = $d[$url_dim];
  }
  return $data_array;
}