Skip to content

Commit 8d39348

Browse files
committed
Added improved error message handling for non-JSON errors from Google API
Updated account data request to include easily accessible ProfileId parameter (for use with request report data method)
1 parent 075fc52 commit 8d39348

File tree

3 files changed

+24
-9
lines changed

3 files changed

+24
-9
lines changed

example.account.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@
77

88
foreach($ga->getAccounts() as $result)
99
{
10-
echo $result . ' (' . $result->getId() . ")<br />";
10+
echo $result . ' ' . $result->getId() . ' (' . $result->getProfileId() . ")<br />";
1111
}

example.report.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,4 @@
3838
<th>Total Visits</th>
3939
<td><?php echo $ga->getVisits() ?></td>
4040
</tr>
41-
<tr>
42-
<th>Results Updated</th>
43-
<td><?php echo $ga->getUpdated() ?></td>
44-
</tr>
4541
</table>

gapi.class.php

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -197,15 +197,32 @@ public function requestReportData($report_id, $dimensions=null, $metrics, $sort_
197197
$parameters['max-results'] = $max_results;
198198

199199
$parameters['prettyprint'] = gapi::dev_mode ? 'true' : 'false';
200-
200+
201201
$url = new gapiRequest(gapi::report_data_url);
202202
$response = $url->get($parameters, $this->auth_method->generateAuthHeader());
203203

204204
//HTTP 2xx
205205
if (substr($response['code'], 0, 1) == '2') {
206206
return $this->reportObjectMapper($response['body']);
207207
} else {
208-
throw new Exception('GAPI: Failed to request report data. Error: "' . strip_tags($response['body']) . '"');
208+
throw new Exception('GAPI: Failed to request report data. Error: "' . $this->cleanErrorResponse($response['body']) . '"');
209+
}
210+
}
211+
212+
/**
213+
* Clean error message from Google API
214+
*
215+
* @param String $error Error message HTML or JSON from Google API
216+
*/
217+
private function cleanErrorResponse($error) {
218+
if (strpos($error, '<html') !== false) {
219+
$error = preg_replace('/<(style|title|script)[^>]*>[^<]*<\/(style|title|script)>/i', '', $error);
220+
return trim(preg_replace('/\s+/', ' ', strip_tags($error)));
221+
}
222+
else
223+
{
224+
$json = json_decode($error);
225+
return isset($json->error->message) ? strval($json->error->message) : $error;
209226
}
210227
}
211228

@@ -245,6 +262,9 @@ protected function accountObjectMapper($json_string) {
245262

246263
foreach ($json['items'] as $item) {
247264
foreach ($item['webProperties'] as $property) {
265+
if (isset($property['profiles'][0]['id'])) {
266+
$property['ProfileId'] = $property['profiles'][0]['id'];
267+
}
248268
$results[] = new gapiAccountEntry($property);
249269
}
250270
}
@@ -601,8 +621,7 @@ public function fetchToken($client_email, $key_file, $delegate_email = null) {
601621
"iat" => time(),
602622
);
603623

604-
if(!empty($delegate_email))
605-
{
624+
if(!empty($delegate_email)) {
606625
$claimset["sub"] = $delegate_email;
607626
}
608627

0 commit comments

Comments
 (0)