@@ -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