首页 文章

为什么我的Google Analytics api 4会返回错误

提问于
浏览
1

您好我正在使用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 );

1 回答

  • 2

    您正在混合Google Analtyics核心报告v3和Google Analytics报告v4 API

    使用Google Analytics核心报告API 3创建分析服务

    $ analytics = new Google_Service_Analytics($ client);

    用于创建用于Google Analytics分析报告api v4的分析服务

    $ analytics = new Google_Service_AnalyticsReporting($ client);

    您已创建了一项服务来连接Google Analytics核心报告v3,但您的代码却尝试连接到Google Analytics报告v4 API

相关问题