我正在根据Reporting API v4 (PHP)运行一些GA请求
我正在遍历多个View ID,其中一些没有正确的权限。 (我知道需要什么权限,但是,有时出于某种原因我没有必要的访问权限)。发生这种情况时,我收到403请求错误:"User does not have sufficient permissions for this profile."
...据我了解
虽然我正在遍历ID,但如果一个视图ID返回这样的错误,则会中断我的循环。
我想在执行$analytics->reports->batchGet( $body );
之前检查请求状态,以免破坏我的循环。 (即,如果返回错误,请不要运行它。)
我的问题:在运行batchGet()
之前可以调用哪种方法来检查请求状态?
我的代码
foreach($view_ids as $view_id){
// Create the DateRange object.
$dateRange = new Google_Service_AnalyticsReporting_DateRange();
$dateRange->setStartDate("2015-06-15");
$dateRange->setEndDate("2015-06-30");
// Create the Metrics object.
$sessions = new Google_Service_AnalyticsReporting_Metric();
$sessions->setExpression("ga:sessions");
$sessions->setAlias("sessions");
//Create the Dimensions object.
$browser = new Google_Service_AnalyticsReporting_Dimension();
$browser->setName("ga:browser");
// Create the ReportRequest object.
$request = new Google_Service_AnalyticsReporting_ReportRequest();
$request->setViewId($view_id);
$request->setDateRanges($dateRange);
$request->setDimensions(array($browser));
$request->setMetrics(array($sessions));
$body = new Google_Service_AnalyticsReporting_GetReportsRequest();
$body->setReportRequests( array( $request) );
// THIS IS WHERE I'M GETTING STUCK, DON'T KNOW WHICH METHOD TO CALL
//$request_status_code = $analyticsreporting->reports->[whichMethod]???
if($request_status_code == 200){
return $analyticsreporting->reports->batchGet( $body );
}
}
让用户选择他们想要访问的视图,然后针对该视图运行您的代码。此代码将向您展示如何列出用户有权访问的视图。
/**
* Note: This code assumes you have an authorized Analytics service object.
* See the Account Summaries Developer Guide for details.
*/
/**
* Example #1:
* Requests a list of all account summaries for the authorized user.
*/
try {
$accounts = $analytics->management_accountSummaries
->listManagementAccountSummaries();
} catch (apiServiceException $e) {
print 'There was an Analytics API service error '
. $e->getCode() . ':' . $e->getMessage();
} catch (apiException $e) {
print 'There was a general API error '
. $e->getCode() . ':' . $e->getMessage();
}
/**
* Example #2:
* The results of the list method are stored in the accounts object.
* The following code shows how to iterate through them.
*/
foreach ($accounts->getItems() as $account) {
$html = <<<HTML
<pre>
Account id = {$account->getId()}
Account kind = {$account->getKind()}
Account name = {$account->getName()}
HTML;
// Iterate through each Property.
foreach ($account->getWebProperties() as $property) {
$html .= <<<HTML
Property id = {$property->getId()}
Property kind = {$property->getKind()}
Property name = {$property->getName()}
Internal property id = {$property->getInternalWebPropertyId()}
Property level = {$property->getLevel()}
Property URL = {$property->getWebsiteUrl()}
HTML;
// Iterate through each view (profile).
foreach ($property->getProfiles() as $profile) {
$html .= <<<HTML
Profile id = {$profile->getId()}
Profile kind = {$profile->getKind()}
Profile name = {$profile->getName()}
Profile type = {$profile->getType()}
HTML;
}
}
$html .= '</pre>';
print $html;
}