You are here

protected function GAFeed::sanitizeReport in Google Analytics Reports 7

Same name and namespace in other branches
  1. 6 GAFeed.lib.inc \GAFeed::sanitizeReport()

Sanitize report data

1 call to GAFeed::sanitizeReport()
GAFeed::queryReportFeed in ./GAFeed.lib.inc
Query and sanitize report data

File

./GAFeed.lib.inc, line 370
Provides the GAFeed object type and associated methods.

Class

GAFeed
GAFeed class to authorize access to and request data from the Google Analytics Data Export API.

Code

protected function sanitizeReport() {
  $xml = simplexml_load_string($this->response->data);
  $this->results = NULL;
  $results = array();
  $meta = array();
  $totals = array();

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

  /* Save totals */
  foreach ($google_results->aggregates->metric as $aggregate_metric) {
    $metric_value = check_plain(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)) {
      $totals[str_replace('ga:', '', check_plain($aggregate_metric
        ->attributes()->name))] = floatval($metric_value);
    }
    else {
      $totals[str_replace('ga:', '', check_plain($aggregate_metric
        ->attributes()->name))] = intval($metric_value);
    }
  }

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