You are here

public function GAFeed::query in Google Analytics Reports 6

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

Public query method for all Data Export API features.

5 calls to GAFeed::query()
GAFeed::getAccessToken in ./GAFeed.lib.inc
OAuth step #3: Fetch access token.
GAFeed::getRequestToken in ./GAFeed.lib.inc
OAuth step #1: Fetch request token.
GAFeed::queryAccountFeed in ./GAFeed.lib.inc
Query and sanitize account data
GAFeed::queryReportFeed in ./GAFeed.lib.inc
Query and sanitize report data
GAFeed::revokeToken in ./GAFeed.lib.inc
Revoke OAuth token.

File

./GAFeed.lib.inc, line 156
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

public function query($path, $params = array(), $method = 'GET', $cache_options = array()) {
  $params_defaults = array(
    'v' => self::gaFeedVersion,
  );
  $params += $params_defaults;

  /* Provide cache defaults if a developer did not override them */
  $cache_defaults = array(
    'cid' => NULL,
    'expire' => google_analytics_reports_cache_time(),
    'refresh' => FALSE,
  );
  $cache_options += $cache_defaults;

  /* Provide a query MD5 for the cid if the developer did not provide one */
  if (empty($cache_options['cid'])) {
    $cache_options['cid'] = 'GAFeed:' . md5(serialize(array_merge($params, array(
      $path,
      $method,
    ))));
  }
  $cache = cache_get($cache_options['cid']);
  if (!$cache_options['refresh'] && isset($cache) && !empty($cache->data)) {
    $this->response = $cache->data;
    $this->fromCache = TRUE;
  }
  else {
    $request = OAuthRequest::from_consumer_and_token($this->consumer, $this->token, $method, $this->queryPath, $params);
    $request
      ->sign_request($this->signatureMethod, $this->consumer, $this->token);
    switch ($method) {
      case 'GET':
        $this
          ->request($request
          ->to_url());
        break;
      case 'POST':
        $this
          ->request($request
          ->get_normalized_http_url(), $request
          ->get_parameters(), 'POST');
        break;
    }

    /* Do not cache erroneous queries */
    if (empty($this->error)) {
      cache_set($cache_options['cid'], $this->response, 'cache', $cache_options['expire']);
    }
  }
  return empty($this->error);
}