【发布时间】:2019-05-12 07:57:32
【问题描述】:
您好,我正在使用 PHP 和 Google Client API 开发一个小项目。 我不知道为什么会收到此错误消息:
消息:未定义的属性:Google_Service_Analytics::$reports
因为我的代码是正确的,所以与 Google 文档网页上的代码相同。
这是我的代码:
$client = new Google_Client();
$client->setApplicationName("Hello Analytics Reporting");
$client->setAuthConfig(FCPATH.'trim-tide-225210-c7xxxxssz7f4c.json');
$client->setScopes(['https://www.googleapis.com/auth/analytics.readonly']);
$analytics = new Google_Service_Analytics($client);
$VIEW_ID = "ga:159882843";
// Create the DateRange object.
$dateRange = new Google_Service_AnalyticsReporting_DateRange();
$dateRange->setStartDate("7daysAgo");
$dateRange->setEndDate("today");
// Create the Metrics object.
$sessions = new Google_Service_AnalyticsReporting_Metric();
$sessions->setExpression("ga:sessions");
$sessions->setAlias("sessions");
// Create the ReportRequest object.
$request = new Google_Service_AnalyticsReporting_ReportRequest();
$request->setViewId($VIEW_ID);
$request->setDateRanges($dateRange);
$request->setMetrics(array($sessions));
$body = new Google_Service_AnalyticsReporting_GetReportsRequest();
$body->setReportRequests( array( $request) );
return $analytics->reports->batchGet( $body );
【问题讨论】:
-
我认为您在 viewId 之前不需要
ga: -
@MasivuyeCokile 我同意我认为您也不需要它,但我认为这不会导致此错误消息。
-
$analytics定义在哪里? -
@MasivuyeCokile 没有 ga 的同样问题:
-
@MasivuyeCokile 在第 5 行
标签: php google-analytics google-api google-analytics-api google-api-php-client