I just want understand how i can run batchRunReports in php, I tried an example but it gives a complex Fatal Error message. I looked at the documentation but couldn't find anything related to my problem. I can run the query I want with the tool in the documentation, but I could not pass it to 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"]),
]
],
]
]);
Fatal error: Uncaught Exception: Expect Google\Analytics\Data\V1beta\RunReportRequest. in F:\xampp\htdocs\other\2_template\api-test-completed\google-analytics\vendor\google\protobuf\src\Google\Protobuf\Internal\GPBUtil.php:198 Stack trace:
#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} thrown in F:\xampp\htdocs\other\2_template\api-test-completed\google-analytics\vendor\google\protobuf\src\Google\Protobuf\Internal\GPBUtil.php on line 198
You need to use the RunReportRequest objects inside the array in "requests" to run a batchRunReports. Don't forget to add the "property" as you did in your requests for the 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"]),
]
]),
]
]);