在 PHP 中使用 batchRunReports 方法:指南
P粉914731066
P粉914731066 2024-01-29 11:20:15
0
1
457

我只是想了解如何在 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粉914731066
P粉914731066

全部回覆(1)
P粉792026467

您需要使用「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"]),
            ]
        ]),
    ]
]);
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!