【问题标题】:why my Google analytics api 4 return an error为什么我的 Google Analytics api 4 返回错误
【发布时间】: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


【解决方案1】:

您正在混合使用 Google Analtyics 核心报告 v3 和 Google Analytics(分析)报告 v4 API

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

$analytics = new Google_Service_Analytics($client);

用于创建与 Google 分析报告 API v4 一起使用的分析服务

$analytics = new Google_Service_AnalyticsReporting($client);

您已创建连接 Google Analytics 核心报告 v3 的服务,但您的代码正在尝试连接到 Google Analytics 报告 v4 API

【讨论】:

猜你喜欢
  • 2018-03-16
  • 1970-01-01
  • 1970-01-01
  • 2019-08-16
  • 1970-01-01
  • 1970-01-01
  • 2019-04-03
  • 2021-08-12
  • 1970-01-01
相关资源
最近更新 更多