phpgoogle-analyticsgoogle-analytics-api

GA4 - report API - php client - filter error


Im try to apply a filter on a query, and getting an fatal error here is the source code:

require 'vendor/autoload.php';

use Google\Analytics\Data\V1beta\BetaAnalyticsDataClient;
use Google\Analytics\Data\V1beta\OrderBy;
use Google\Analytics\Data\V1beta\OrderBy\MetricOrderBy;
use Google\Analytics\Data\V1beta\DateRange;
use Google\Analytics\Data\V1beta\Dimension;
use Google\Analytics\Data\V1beta\Metric;
use Google\Analytics\Data\V1beta\Filter;
use Google\Analytics\Data\V1beta\Filter\StringFilter;
use Google\Analytics\Data\V1beta\FilterExpression;
use Google\Analytics\Data\V1beta\Filter\StringFilter\MatchType;
$property_id = XXXX;
$response_cpc = $client->runReport([
'property' => 'properties/' . $property_id,
'dateRanges' => [
new DateRange([
'start_date' => $_GET["date"],
'end_date' => $_GET["date"],
]),
],

'dimensions' => [new Dimension(
[
'name' => 'firstUserSourceMedium',
]
)],[new Dimension(
    [
        'name' => 'firstUserCampaignName',
    ]
)
],
'metrics' => [new Metric(
[
'name' => 'sessions',
]
),new Metric(
[
'name' => 'bounceRate',
]
),
new Metric(
[
'name' => 'conversions:Newsletter_signup',
]
),
new Metric(
[
'name' => 'screenPageViewsPerSession',
]
),
new Metric(
[
'name' => 'transactions',
]
),
new Metric(
[
'name' => 'conversions:go_to_amazon',
]
)

],
'dimensionFilter' => [new FilterExpression (
            [
                'filter' => [new Filter(
                    [
                        'field_name' => 'firstUserSourceMedium',
                        'string_filter' => [new StringFilter([
                            'match_type' => Filter\StringFilter\MatchType::BEGINS_WITH,
                             'value' => 'cpc',
                            'case_sensitive'=> false,
                            ]
                    )]
                    ]
                )]
            ]
        )],
    'limit'=> 25,
    'orderBys' =>[ new OrderBy([
    'metric' => new MetricOrderBy([
        'metric_name' =>'sessions']
        ),
    'desc'=>true,
    ])],

]);

print_r($response_cpc);

and here is the error I'm getting:

Fatal error: Uncaught Exception: Expect Google\Analytics\Data\V1beta\Filter\StringFilter. in /home/Server1/xagyrxxhed/public_html/vendor/google/protobuf/src/Google/Protobuf/Internal/GPBUtil.php:198 Stack trace: #0 /home/Server1/xagyrxxhed/public_html/vendor/google/analytics-data/src/V1beta/Filter.php(116): Google\Protobuf\Internal\GPBUtil::checkMessage() #1 /home/Server1/xagyrxxhed/public_html/vendor/google/protobuf/src/Google/Protobuf/Internal/Message.php(1082): Google\Analytics\Data\V1beta\Filter->setStringFilter() #2 /home/Server1/xagyrxxhed/public_html/vendor/google/protobuf/src/Google/Protobuf/Internal/Message.php(80): Google\Protobuf\Internal\Message->mergeFromArray() #3 /home/Server1/xagyrxxhed/public_html/vendor/google/analytics-data/src/V1beta/Filter.php(54): Google\Protobuf\Internal\Message->__construct() #4 /home/Server1/xagyrxxhed/public_html/quickstart.php(280): Google\Analytics\Data\V1beta\Filter->__construc in /home/Server1/xagyrxxhed/public_html/vendor/google/protobuf/src/Google/Protobuf/Internal/GPBUtil.php on line 198

Any help ?


Solution

  • Fixed with: 1- memory increase 2- change in the code: Remove some [

    'dimensionFilter' => new FilterExpression (
            ``[
                'filter' => new Filter(
                    [
                        'field_name' => 'firstUserSourceMedium',
                        'string_filter' => new Filter\StringFilter([
                                'match_type' => 4,
                                'value' => 'cpc',
                                'case_sensitive'=> false,
                            ]
                        )
                    ]
                )
            ]
        ),