@@ -197,15 +197,32 @@ public function requestReportData($report_id, $dimensions=null, $metrics, $sort_
197
197
$ parameters ['max-results ' ] = $ max_results ;
198
198
199
199
$ parameters ['prettyprint ' ] = gapi::dev_mode ? 'true ' : 'false ' ;
200
-
200
+
201
201
$ url = new gapiRequest (gapi::report_data_url);
202
202
$ response = $ url ->get ($ parameters , $ this ->auth_method ->generateAuthHeader ());
203
203
204
204
//HTTP 2xx
205
205
if (substr ($ response ['code ' ], 0 , 1 ) == '2 ' ) {
206
206
return $ this ->reportObjectMapper ($ response ['body ' ]);
207
207
} 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 ;
209
226
}
210
227
}
211
228
@@ -245,6 +262,9 @@ protected function accountObjectMapper($json_string) {
245
262
246
263
foreach ($ json ['items ' ] as $ item ) {
247
264
foreach ($ item ['webProperties ' ] as $ property ) {
265
+ if (isset ($ property ['profiles ' ][0 ]['id ' ])) {
266
+ $ property ['ProfileId ' ] = $ property ['profiles ' ][0 ]['id ' ];
267
+ }
248
268
$ results [] = new gapiAccountEntry ($ property );
249
269
}
250
270
}
@@ -601,8 +621,7 @@ public function fetchToken($client_email, $key_file, $delegate_email = null) {
601
621
"iat " => time (),
602
622
);
603
623
604
- if (!empty ($ delegate_email ))
605
- {
624
+ if (!empty ($ delegate_email )) {
606
625
$ claimset ["sub " ] = $ delegate_email ;
607
626
}
608
627
0 commit comments