You are here

protected function gapi::reportObjectMapper in Google Analytics Statistics 7.2

Same name and namespace in other branches
  1. 7 includes/gapi.class.php \gapi::reportObjectMapper()
  2. 7.x inc/gapi.class.php \gapi::reportObjectMapper()

Report Object Mapper to convert the JSON to array of useful PHP objects

Parameters

String $json_string:

Return value

Array of gapiReportEntry objects

1 call to gapi::reportObjectMapper()
gapi::requestReportData in includes/gapi.class.php
Request report data from Google Analytics

File

includes/gapi.class.php, line 269

Class

gapi
GAPI - Google Analytics PHP Interface

Code

protected function reportObjectMapper($json_string) {
  $json = json_decode($json_string, true);
  $this->results = null;
  $results = array();
  $report_aggregate_metrics = array();

  //Load root parameters

  // Start with elements from the root level of the JSON that aren't themselves arrays.
  $report_root_parameters = array_filter($json, function ($var) {
    return !is_array($var);
  });

  // Get the items from the 'query' object, and rename them slightly.
  foreach ($json['query'] as $index => $value) {
    $new_index = lcfirst(str_replace(' ', '', ucwords(str_replace('-', ' ', $index))));
    $report_root_parameters[$new_index] = $value;
  }

  // Now merge in the profileInfo, as this is also mostly useful.
  array_merge($report_root_parameters, $json['profileInfo']);

  //Load result aggregate metrics
  foreach ($json['totalsForAllResults'] as $index => $metric_value) {

    //Check for float, or value with scientific notation
    if (preg_match('/^(\\d+\\.\\d+)|(\\d+E\\d+)|(\\d+.\\d+E\\d+)$/', $metric_value)) {
      $report_aggregate_metrics[str_replace('ga:', '', $index)] = floatval($metric_value);
    }
    else {
      $report_aggregate_metrics[str_replace('ga:', '', $index)] = intval($metric_value);
    }
  }

  //Load result entries
  if (isset($json['rows'])) {
    foreach ($json['rows'] as $row) {
      $metrics = array();
      $dimensions = array();
      foreach ($json['columnHeaders'] as $index => $header) {
        switch ($header['columnType']) {
          case 'METRIC':
            $metric_value = $row[$index];

            //Check for float, or value with scientific notation
            if (preg_match('/^(\\d+\\.\\d+)|(\\d+E\\d+)|(\\d+.\\d+E\\d+)$/', $metric_value)) {
              $metrics[str_replace('ga:', '', $header['name'])] = floatval($metric_value);
            }
            else {
              $metrics[str_replace('ga:', '', $header['name'])] = intval($metric_value);
            }
            break;
          case 'DIMENSION':
            $dimensions[str_replace('ga:', '', $header['name'])] = strval($row[$index]);
            break;
          default:
            throw new Exception("GAPI: Unrecognized columnType '{$header['columnType']}' for columnHeader '{$header['name']}'");
        }
      }
      $results[] = new gapiReportEntry($metrics, $dimensions);
    }
  }
  $this->report_root_parameters = $report_root_parameters;
  $this->report_aggregate_metrics = $report_aggregate_metrics;
  $this->results = $results;
  return $results;
}