GA4 – 报告 API – PHP 客户端 – 过滤器错误

huangapple go评论80阅读模式
英文:

GA4 - report API - php client - filter error

问题

在查询中尝试应用过滤器时,出现了致命错误,以下是源代码:

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);

这里是我遇到的错误:

致命错误:未捕获异常:在 /home/Server1/xagyrxxhed/public_html/vendor/google/protobuf/src/Google/Protobuf/Internal/GPBUtil.php:198 中期望 Google\Analytics\Data\V1beta\Filter\StringFilter。堆栈跟踪:#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 在 /home/Server1/xagyrxxhed/public_html/vendor/google/protobuf/src/Google/Protobuf/Internal/GPBUtil.php 第198行发生错误

有什么帮助吗?

英文:

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 ?

答案1

得分: 0

I can help you translate the code portion you provided:

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,
                            ]
                        )
                    ]
                )
            ]
        ),

Please note that I've only translated the code portion and removed the unnecessary characters.

英文:

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,
]
)
]
)
]
),

huangapple
  • 本文由 发表于 2023年5月25日 02:17:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76326394.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定