GoogleCloudPlatform / php-docs-samples

A collection of samples that demonstrate how to call Google Cloud services from PHP.
http://cloud.google.com/php
Apache License 2.0
959 stars 1.03k forks source link

BetaAnalyticsDataClient: unable to fetch data using quickstart guide #1926

Closed CarloRew closed 3 months ago

CarloRew commented 11 months ago

Hello there, I am trying to fetch data from G4A using this guide: https://github.com/GoogleCloudPlatform/php-docs-samples/blob/main/analyticsdata/quickstart.php

so using the example above I am getting this output Fatal error: Uncaught TypeError: Argument 1 passed to Google\Analytics\Data\V1beta\Gapic\BetaAnalyticsDataGapicClient::runReport() must be of the type array, object given, called in /var/www/app/httpdocs/backend/cron/g4a_testing.php on line 49 and defined in /var/www/app/vendor/google/analytics-data/src/V1beta/Gapic/BetaAnalyticsDataGapicClient.php on line 994 TypeError: Argument 1 passed to Google\Analytics\Data\V1beta\Gapic\BetaAnalyticsDataGapicClient::runReport() must be of the type array, object given, called in /var/www/app/httpdocs/backend/cron/g4a_testing.php on line 49 in /var/www/app/vendor/google/analytics-data/src/V1beta/Gapic/BetaAnalyticsDataGapicClient.php on line 994 Call Stack: 0.0004 385000 1. {main}() /var/www/app/httpdocs/backend/cron/g4a_testing.php:0 0.1107 3783400 2. Google\Analytics\Data\V1beta\BetaAnalyticsDataClient->runReport() /var/www/app/httpdocs/backend/cron/g4a_testing.php:49

And then i adjust my code to use hardcoded array like this: `$request_2 = [ 'property' => 'properties/' . $property_id, 'dateRanges' => [ 'start_date' => '2020-03-31', 'end_date' => 'today' ] ];

$response = $client->runReport($request_2);`

With this I am getting this output Fatal error: Uncaught Exception: Expect Google\Analytics\Data\V1beta\DateRange. in /var/www/app/vendor/google/protobuf/src/Google/Protobuf/Internal/GPBUtil.php on line 198 Exception: Expect Google\Analytics\Data\V1beta\DateRange. in /var/www/app/vendor/google/protobuf/src/Google/Protobuf/Internal/GPBUtil.php on line 198 Call Stack: 0.0003 385000 1. {main}() /var/www/app/httpdocs/backend/cron/g4a_testing.php:0 0.1097 3783400 2. Google\Analytics\Data\V1beta\BetaAnalyticsDataClient->runReport() /var/www/app/httpdocs/backend/cron/g4a_testing.php:49 0.1098 3784656 3. Google\Analytics\Data\V1beta\RunReportRequest->setDateRanges() /var/www/app/vendor/google/analytics-data/src/V1beta/Gapic/BetaAnalyticsDataGapicClient.php:1012 0.1098 3784680 4. Google\Protobuf\Internal\GPBUtil::checkRepeatedField() /var/www/app/vendor/google/analytics-data/src/V1beta/RunReportRequest.php:332 0.1098 3784792 5. Google\Protobuf\Internal\RepeatedField->offsetSet() /var/www/app/vendor/google/protobuf/src/Google/Protobuf/Internal/GPBUtil.php:210 0.1098 3784816 6. Google\Protobuf\Internal\GPBUtil::checkMessage() /var/www/app/vendor/google/protobuf/src/Google/Protobuf/Internal/RepeatedField.php:187

Please advise! Thanks!

bshaffer commented 3 months ago

Hi @CarloRew You are using the wrong Client class. As you can see from the sample, it imports the following class:

use Google\Analytics\Data\V1beta\Client\BetaAnalyticsDataClient;

Your error is coming from this class (which is the previous, deprecated client):

Google\Analytics\Data\V1beta\BetaAnalyticsDataClient