You are here

function seotools_fetch_ga_data in Drupal SEO Tools 6

Same name and namespace in other branches
  1. 7 seotools.report.inc \seotools_fetch_ga_data()
5 calls to seotools_fetch_ga_data()
seotools_dashboard_analytics_box in ./seotools.report.inc
seotools_dashboard_map_box in ./seotools.report.inc
seotools_dashboard_referrers_box in ./seotools.report.inc
seotools_dashboard_sources_box in ./seotools.report.inc
seotools_generate_report_top_and_trends in ./seotools.report.inc
valid ids: keywords

File

./seotools.report.inc, line 435

Code

function seotools_fetch_ga_data($request, $group_by = 'date') {
  $data = google_analytics_api_report_data($request);

  //dsm($request);

  //dsm($group_by);

  //dsm($data);

  // Add up all the statistics for different paths.
  $rows = array();
  foreach ($request['dimensions'] as $v) {
    $dimension = str_replace('ga:', '', $v);
    $req_dimensions[] = $dimension;
  }
  foreach ($request['metrics'] as $v) {
    $req_metrics[] = str_replace('ga:', '', $v);
  }

  // filter off 'ga:' from group bys
  if (is_array($group_by)) {
    foreach ($group_by as $v) {
      $a[] = str_replace('ga:', '', $v);
    }
    $group_by = $a;
  }
  else {
    $group_by = str_replace('ga:', '', $group_by);
  }

  //dsm($req_dimensions);

  //dsm($req_metrics);
  $rows = array();
  foreach ($data->results as $item) {

    //$dimensions = $item->getDimensions();
    $dimensions = array();
    foreach ($req_dimensions as $d) {
      $dimensions[$d] = $item[$d];
    }

    //dsm($dimensions);
    if (is_array($group_by)) {
      $group_name = '';
      foreach ($group_by as $group_by_item) {
        $group_name .= $dimensions[$group_by_item];
      }
    }
    else {
      $group_name = $dimensions[$group_by];
    }

    //dsm($group_name);
    if (!isset($rows[$group_name])) {
      $rows[$group_name] = array();
    }
    foreach ($req_metrics as $m) {
      $rows[$group_name][$m] += $item[$m];
    }
  }

  //dsm($rows);

  //dsm('end');

  //dsm('end');
  return $rows;
}