我只是想了解如何在 php 中運行batchRunReports,我嘗試了一個示例,但它給出了復雜的致命錯誤消息。我查看了文檔,但找不到與我的問題相關(guān)的任何內(nèi)容。我可以使用文檔中的工具運行我想要的查詢,但無法將其傳遞給 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 行
您需要使用“requests”數(shù)組中的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"]), ] ]), ] ]);