搜索

首页  >  问答  >  正文

在 PHP 中使用 batchRunReports 方法:指南

我只是想了解如何在 php 中运行batchRunReports,我尝试了一个示例,但它给出了复杂的致命错误消息。我查看了文档,但找不到与我的问题相关的任何内容。我可以使用文档中的工具运行我想要的查询,但无法将其传递给 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\Analytics\Data\V1beta\MetricAggregation;

$property = "properties/XXXXXXXXX";

$client = new BetaAnalyticsDataClient();
$client->batchRunReports([
    "requests" => [
        [   
            "property" => $property,
            "dataRanges" => [
                new DateRange(["start_date" => "7daysAgo"], ["end_date" => "today"]),
            ],
            "dimensions" => [
                new Dimension(["name" => "eventName"]),
            ],
            "metrics" => [
                new Metric(["name" => "eventCount"]),
            ]
        ],
        [
            "property" => $property,
            "dataRanges" => [
                new DateRange(["start_date" => "7daysAgo"], ["end_date" => "today"]),
            ],
            "dimensions" => [
                new Dimension(["name" => "deviceCategory"]),
            ],
            "metrics" => [
                new Metric(["name" => "activeUsers"]),
            ]
        ],
    ]
]);

致命错误:未捕获异常:期望 Google\Analytics\Data\V1beta\RunReportRequest。在 F:\xampp\htdocs\other\2_template\api-test-completed\google-analytics\vendor\google\protobuf\src\Google\Protobuf\Internal\GPBUtil.php:198 堆栈跟踪:

#0 F:\xampp\htdocs\other\2_template\api-test-completed\google-analytics\vendor\google\protobuf\src\Google\Protobuf\Internal\RepeatedField.php(187): Google\Protobuf\Internal \GPBUtil::checkMessage(Array, 'Google\Analytic...')

#1 F:\xampp\htdocs\other\2_template\api-test-completed\google-analytics\vendor\google \protobuf\src\Google\Protobuf\Internal\GPBUtil.php(210): Google\Protobuf\Internal\RepeatedField->offsetSet(NULL, Array)

#2 F:\xampp\htdocs\other\ 2_template\api-test-completed\google-analytics\vendor\google\analytics-data\src\V1beta\BatchRunReportsRequest.php(126): Google\Protobuf\Internal\GPBUtil::checkRepeatedField(Array, 11, 'Google\Analytic ...')

#3 F:\xampp\htdocs\other\2_template\api-test-completed\google-analytics\vendor\google\analytics-data\src\V1beta\Gapic\BetaAnalyticsDataGapicClient .php(421): Google\Analytics\Data\V1beta\BatchRunReportsRequest->setRequests(Array)

#4 F:\xampp\htdocs\other\2_template\api-test-completed\google-analytics\ test.php(46): Google\Analytics\Data\V1beta\Gapic\BetaAnalyticsDataGapicClient->batchRunReports(Array)

#5 {main} 抛出 F:\xampp\htdocs\other\2_template\api- test-completed\google-analytics\vendor\google\protobuf\src\Google\Protobuf\Internal\GPBUtil.php 第 198 行

P粉914731066P粉914731066304 天前475

全部回复(1)我来回复

  • P粉792026467

    P粉7920264672024-01-30 10:45:43

    您需要使用“requests”数组中的RunReportRequest对象来运行batchRunReports。不要忘记像在batchRunReports 请求中那样添加“属性”。

    $response = $client->batchRunReports([
        "property" => $property,
          "requests" => [
            new RunReportRequest(
            [   
                "property" => $property,
                "date_ranges" => [
                    new DateRange(["start_date" => "7daysAgo", "end_date" => "today"]),
                ],
                "dimensions" => [
                    new Dimension(["name" => "eventName"]),
                ],
                "metrics" => [
                    new Metric(["name" => "eventCount"]),
                ]
            ]),
            new RunReportRequest([
                "property" => $property,
                "date_ranges" => [
                    new DateRange(["start_date" => "7daysAgo", "end_date" => "today"]),
                ],
                "dimensions" => [
                    new Dimension(["name" => "deviceCategory"]),
                ],
                "metrics" => [
                    new Metric(["name" => "activeUsers"]),
                ]
            ]),
        ]
    ]);
    

    回复
    0
  • 取消回复