/** * Performs a Google Analytics query with the given * Google\Analytics\GaDataQuery object and returns the result as a * Google\Analytics\GaData object, or boolean false if the query's data * has been exhausted. * * @param Google\Analytics\GaDataQuery $query * @return Google\Analytics\GaData, boolean */ public function query(GaDataQuery $query) { $query->setAPIInstance($this); $totalResults = $query->getTotalResults(); do { /* If the given query is already active, meaning that we have made a call for its first page of results already, don't pass any arguments to $this->_makeRequest(). */ $hash = $query->getHash(); if ($hash == $this->_activeQuery) { if ($totalResults !== null && $this->_totalRows >= $totalResults) { // No reason to continue $response = false; break; } $response = $this->_makeRequest(); } else { $this->_activeQuery = $hash; $this->_totalRows = 0; $response = $this->_makeRequest(new APIRequest('data/ga', $query->getAsArray())); } /* The response will be boolean false if the query had data, but it has been exhausted. It will be a Google\Analytics\GaData object with no rows if the query returned no data at all. If there's no chance of achieving a response by iterating, we'll consider the operation as having retrieved a response, even if no data came back. */ if ($response && $response->getRows() || !$query instanceof IterativeGaDataQuery || !$query->iterate()) { $gotResponse = true; } else { $gotResponse = false; } } while (!$gotResponse); if ($response) { if ($query->getSamplingLevel() == GaDataQuery::SAMPLING_LEVEL_NONE && $response->containsSampledData()) { throw new SamplingException('The response contains sampled data.'); } /* I have to do this here instead of allowing it to happen automatically so that I can pass the API instance as an argument. */ $response->setColumnHeaders($this->_responseParsed['columnHeaders'], $this); // I also want the rows object to be aware of the columns object $rows = $response->getRows(); $rows->setColumnHeaders($response->getColumnHeaders()); $rowCount = count($rows); if ($rowCount) { $this->_totalRows += $rowCount; if ($totalResults !== null) { $surplus = $this->_totalRows - $totalResults; if ($surplus > 0) { /* Even though we already got the data, behave as we were asked and lop off the extra rows. */ $rows->discard($surplus); } } } } else { $this->_activeQuery = null; } return $response; }