GA4 - 报告 API - php 客户端 - 过滤器错误

问题描述 投票:0回答:1

我尝试对查询应用过滤器,并收到致命错误 这是源代码:

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

这是我收到的错误:

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

有什么帮助吗?

php google-analytics google-analytics-api
1个回答
0
投票

修复: 1-内存增加 2-更改代码:删除一些[

'dimensionFilter' => new FilterExpression (
        ``[
            'filter' => new Filter(
                [
                    'field_name' => 'firstUserSourceMedium',
                    'string_filter' => new Filter\StringFilter([
                            'match_type' => 4,
                            'value' => 'cpc',
                            'case_sensitive'=> false,
                        ]
                    )
                ]
            )
        ]
    ),
© www.soinside.com 2019 - 2024. All rights reserved.