Php 为什么我的Google analytics api 4返回错误
你好,我正在使用PHP和Google客户端API进行一个小项目。 我不知道为什么会收到此错误消息: 消息:未定义属性:Google_服务_分析::$reports 因为我的代码是正确的,所以它和谷歌文档网页上的代码是一样的 这是我的代码:Php 为什么我的Google analytics api 4返回错误,php,google-analytics,google-api,google-analytics-api,google-api-php-client,Php,Google Analytics,Google Api,Google Analytics Api,Google Api Php Client,你好,我正在使用PHP和Google客户端API进行一个小项目。 我不知道为什么会收到此错误消息: 消息:未定义属性:Google_服务_分析::$reports 因为我的代码是正确的,所以它和谷歌文档网页上的代码是一样的 这是我的代码: $client = new Google_Client(); $client->setApplicationName("Hello Analytics Reporting"); $client->setAuthConfig(FCPA
$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 );
您正在混合使用Google Analytics core reporting v3和Google Analytics reporting v4 API 用于使用Google analytics core reporting api v3创建分析服务 $analytics=新的Google_服务_分析($client) 用于创建与Google analytics reporting api v4一起使用的分析服务 $analytics=新谷歌服务\分析报告($client)
您已经创建了一个连接Google Analytics core reporting v3的服务,但是您的代码正在尝试连接到Google Analytics reporting v4 API我认为您不需要在viewId@MasivuyeCokile我同意,我认为你也不需要它,但我不认为这会导致这个错误消息。你在哪里
$analytics
已定义?@MasivuyeCokile没有ga的相同问题:@MasivuyeCokile在第5行任何时候都有一个很好的错误:)记住接受anwser,如果它有助于你获得分数。欢迎来到stack@SupportTechnique