You are here

protected function gapi::reportObjectMapper in Google Analytics Statistics 7

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

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

Parameters

String $xml_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 311

Class

gapi
GAPI - Google Analytics PHP Interface

Code

protected function reportObjectMapper($xml_string) {
  $xml = simplexml_load_string($xml_string);
  $this->results = null;
  $results = array();
  $report_root_parameters = array();
  $report_aggregate_metrics = array();

  //Load root parameters
  $report_root_parameters['updated'] = strval($xml->updated);
  $report_root_parameters['generator'] = strval($xml->generator);
  $report_root_parameters['generatorVersion'] = strval($xml->generator
    ->attributes());
  $open_search_results = $xml
    ->children('http://a9.com/-/spec/opensearchrss/1.0/');
  foreach ($open_search_results as $key => $open_search_result) {
    $report_root_parameters[$key] = intval($open_search_result);
  }
  $google_results = $xml
    ->children('http://schemas.google.com/analytics/2009');
  foreach ($google_results->dataSource->property as $property_attributes) {
    $report_root_parameters[str_replace('ga:', '', $property_attributes
      ->attributes()->name)] = strval($property_attributes
      ->attributes()->value);
  }
  $report_root_parameters['startDate'] = strval($google_results->startDate);
  $report_root_parameters['endDate'] = strval($google_results->endDate);

  //Load result aggregate metrics
  foreach ($google_results->aggregates->metric as $aggregate_metric) {
    $metric_value = strval($aggregate_metric
      ->attributes()->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:', '', $aggregate_metric
        ->attributes()->name)] = floatval($metric_value);
    }
    else {
      $report_aggregate_metrics[str_replace('ga:', '', $aggregate_metric
        ->attributes()->name)] = intval($metric_value);
    }
  }

  //Load result entries
  foreach ($xml->entry as $entry) {
    $metrics = array();
    foreach ($entry
      ->children('http://schemas.google.com/analytics/2009')->metric as $metric) {
      $metric_value = strval($metric
        ->attributes()->value);

      //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:', '', $metric
          ->attributes()->name)] = floatval($metric_value);
      }
      else {
        $metrics[str_replace('ga:', '', $metric
          ->attributes()->name)] = intval($metric_value);
      }
    }
    $dimensions = array();
    foreach ($entry
      ->children('http://schemas.google.com/analytics/2009')->dimension as $dimension) {
      $dimensions[str_replace('ga:', '', $dimension
        ->attributes()->name)] = strval($dimension
        ->attributes()->value);
    }
    $results[] = new gapiReportEntry($metrics, $dimensions);
  }
  $this->report_root_parameters = $report_root_parameters;
  $this->report_aggregate_metrics = $report_aggregate_metrics;
  $this->results = $results;
  return $results;
}