首頁  >  問答  >  主體

在 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\google \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\ testdocs\other\2_template\api-test-completed\google-analytics\ google .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粉914731066238 天前414

全部回覆(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
  • 取消回覆