Home  >  Q&A  >  body text

Using the batchRunReports method in PHP: A guide

I just want to understand how to run batchRunReports in php, I tried an example but it gives complex fatal error message. I've looked through the documentation but can't find anything related to my problem. I can run the query I want using the tools in the documentation, but I can't 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: Expecting Google\Analytics\Data\V1beta\RunReportRequest. At 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} throws F:\xampp\htdocs\other\2_template\api- test-completed\google-analytics\vendor\google\protobuf\src\Google\Protobuf\Internal\GPBUtil.php line 198

P粉914731066P粉914731066287 days ago461

reply all(1)I'll reply

  • P粉792026467

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

    You need to use the RunReportRequest object in the "requests" array to run batchRunReports. Don't forget to add the "attribute" like in the batchRunReports request.

    $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"]),
                ]
            ]),
        ]
    ]);
    

    reply
    0
  • Cancelreply