Google Analytics(分析数据API V1 Runreport),仅返回JSON

发布于 2025-02-12 15:17:12 字数 1474 浏览 2 评论 0原文

目前,我正在尝试使用Google Analytics Data API V1从GA4获取JSON数据。但是,给出的响应不是纯JSON数据,而是仅使用PHP打印它,它会给我{}。但是,使用预定义的方法,我们可以获取值。我可以知道吗,无论如何我可以获得纯JSON数据吗?

<?php
putenv('GOOGLE_APPLICATION_CREDENTIALS=xxx.json');

require_once 'vendor\autoload.php';

use Google\Analytics\Data\V1beta\BetaAnalyticsDataClient;
use Google\Analytics\Data\V1beta\DateRange;
use Google\Analytics\Data\V1beta\Dimension;
use Google\Analytics\Data\V1beta\Metric;
use Google\Cloud\BigQuery\Connection\Rest;

$property_id = 'xxx'; // GA4 property ID

// Using a default constructor instructs the client to use the credentials
// specified in GOOGLE_APPLICATION_CREDENTIALS environment variable.
$client = new BetaAnalyticsDataClient();

// Make an API call.
$response = $client->runReport([
  'property' => 'properties/' . $property_id,
  'dateRanges' => [
    new DateRange([
      'start_date' => '2022-06-30',
      'end_date' => 'today',
    ]),
  ],
  'dimensions' => [
    new Dimension(
      [
        'name' => 'city',
      ]
    ),
  ],
  'metrics' => [
    new Metric(
      [
        'name' => 'activeUsers',
      ]
    )
  ]
]);

print 'Report result: ' . PHP_EOL;

printVisitorsLocationInNumber($response);

function printVisitorsLocationInNumber($resp) {
  foreach ($resp->getRows() as $row) {
    echo
    $row->getDimensionValues()[0]->getValue() . ' ' . $row->getMetricValues()[0]->getValue() . PHP_EOL . '</br>';;
  }
}

currently I am trying to get the json data from GA4 using Google Analytics Data API v1. However, the response given back is not a pure json data, instead if I just print it using PHP, it gives me {}. However, using the predefined method, we can get the value. May I know, is there anyway I can get a pure json data?

<?php
putenv('GOOGLE_APPLICATION_CREDENTIALS=xxx.json');

require_once 'vendor\autoload.php';

use Google\Analytics\Data\V1beta\BetaAnalyticsDataClient;
use Google\Analytics\Data\V1beta\DateRange;
use Google\Analytics\Data\V1beta\Dimension;
use Google\Analytics\Data\V1beta\Metric;
use Google\Cloud\BigQuery\Connection\Rest;

$property_id = 'xxx'; // GA4 property ID

// Using a default constructor instructs the client to use the credentials
// specified in GOOGLE_APPLICATION_CREDENTIALS environment variable.
$client = new BetaAnalyticsDataClient();

// Make an API call.
$response = $client->runReport([
  'property' => 'properties/' . $property_id,
  'dateRanges' => [
    new DateRange([
      'start_date' => '2022-06-30',
      'end_date' => 'today',
    ]),
  ],
  'dimensions' => [
    new Dimension(
      [
        'name' => 'city',
      ]
    ),
  ],
  'metrics' => [
    new Metric(
      [
        'name' => 'activeUsers',
      ]
    )
  ]
]);

print 'Report result: ' . PHP_EOL;

printVisitorsLocationInNumber($response);

function printVisitorsLocationInNumber($resp) {
  foreach ($resp->getRows() as $row) {
    echo
    $row->getDimensionValues()[0]->getValue() . ' ' . $row->getMetricValues()[0]->getValue() . PHP_EOL . '</br>';;
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

执手闯天涯 2025-02-19 15:17:12
echo $response->serializeToJsonString(); // Prints JSON string

$ wonsevy\ google \ analytics \ data \ v1beta \ runreporportresponse的实例,它是从\ google \ protobuf \ protobuf \ proteobuf \ internals \ internal \ sexpess延长的实例。 。因此,您可以使用serializetojsonstring()相同的方法。

echo $response->serializeToJsonString(); // Prints JSON string

$response is an instance of \Google\Analytics\Data\V1beta\RunReportResponse, which is extended from \Google\Protobuf\Internal\Message. Hence, you can use serializeToJsonString() method of the same.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文