You are here

public function GAFeed::query in Google Analytics Counter 7.2

Public query method for all Data Export API features.

9 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::queryAccounts in ./GAFeed.lib.inc
Query Management API - Accounts
GAFeed::queryGoals in ./GAFeed.lib.inc
Query Management API - Goals
GAFeed::queryProfiles in ./GAFeed.lib.inc
Query Management API - Profiles

... See full list

File

./GAFeed.lib.inc, line 165
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(
    'start-index' => 1,
    'max-results' => 1000,
  );
  $params += $params_defaults;

  /* Provide cache defaults if a developer did not override them */
  $cache_defaults = array(
    'cid' => NULL,
    'expire' => google_analytics_counter_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->results = json_decode($this->response->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);
}