You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

GA4 Data API V1 Beta使用inListFilter时类找不到错误排查

问题描述

尝试使用GA4的DimensionFilter中的inListFilter过滤数据时,出现错误:

Fatal error: Uncaught Error: Class 'Google\Analytics\Data\V1beta\Filter\inListFilter' not found

已引入所有依赖库,除过滤器外其他数据查询功能正常,因inListFilter相关示例较少,无法定位错误原因。相关代码如下:

// [START analytics_data_quickstart]
require '/home/hosting_users/fedex/www/innofish/google_api/vendor/autoload.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\Filter;
 use Google\Analytics\Data\V1beta\FilterExpression;
 use Google\Analytics\Data\V1beta\Filter\inListFilter;
 use Google\Analytics\Data\V1beta\OrderBy;

// GA4 property ID
$property_id = '0000000000';

// [START analyticsdata_json_credentials_initialize]
$credentials_json_path = '/home/hosting_users/fedex/www/innofish/google_api/json/0000000000.json';

// Explicitly use service account credentials by specifying
// the private key file.
$client = new BetaAnalyticsDataClient(['credentials' =>
    $credentials_json_path]);
// [END analyticsdata_json_credentials_initialize]

// [START analyticsdata_json_credentials_run_report]
// Make an API call.
$response = $client->runReport([
    'property' => 'properties/' . $property_id,
    'dateRanges' => [
        new DateRange([
            'start_date' => '2023-02-08',
            'end_date' => 'yesterday',
        ]),
    ],
    'dimensions' => [
        new Dimension([
            'name' => 'campaignName',
        ]),
        new Dimension([
            'name' => 'sourceMedium',
        ]),
    ],
    'metrics' => [
        new Metric([
            'name' => 'sessions',
        ]),
    ],
    'dimensionFilter' => [ 
        new FilterExpression([
            'filter' => [ 
                new filter([
                    'field_name' => 'campaignName',
                    'in_list_filter' => [ new Filter\inListFilter([
                        'values' => ["edm", "naver_blog", "(direct)", "fourfour", "advertise", "kakao", "alliance" ],
                        'caseSensitive' => true,
                    ]),
                    ],
                ]),
            ],  
        ]),
    ],
    'orderBys' => [
        new OrderBy([
            'dimension' => new OrderBy\DimensionOrderBy([
                'dimension_name' => 'campaignName', 
                'order_type' => OrderBy\DimensionOrderBy\OrderType::ALPHANUMERIC
            ]),
            'desc' => false,
        ]),
    ],
    'keepEmptyRows' => true,
    
]);

// [END analyticsdata_json_credentials_run_report]

// [START analyticsdata_json_credentials_run_report_response]
// Print results of an API call.
print 'Report result: ' . PHP_EOL . '<br>';
foreach ($response->getRows() as $row) {
    print $row->getDimensionValues()[0]->getValue(). ' ' . $row->getDimensionValues()[1]->getValue(). ' ' .$row->getMetricValues()[0]->getValue() . PHP_EOL . '<br>';
}
// [END analytics_data_quickstart]
错误原因分析

PHP是大小写敏感的编程语言,Google Analytics Data API的PHP客户端中,inListFilter类的正确命名是InListFilter(首字母大写),代码中存在三处大小写错误:

  1. use语句里的Filter\inListFilter应为Filter\InListFilter
  2. 实例化inListFilter时的new Filter\inListFilter(...)应为new Filter\InListFilter(...)
  3. 实例化Filter类时写成了小写的new filter(...),需改为new Filter(...)
修正后的代码
// [START analytics_data_quickstart]
require '/home/hosting_users/fedex/www/innofish/google_api/vendor/autoload.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\Filter;
 use Google\Analytics\Data\V1beta\FilterExpression;
 use Google\Analytics\Data\V1beta\Filter\InListFilter; // 修正类名大小写
 use Google\Analytics\Data\V1beta\OrderBy;

// GA4 property ID
$property_id = '0000000000';

// [START analyticsdata_json_credentials_initialize]
$credentials_json_path = '/home/hosting_users/fedex/www/innofish/google_api/json/0000000000.json';

// Explicitly use service account credentials by specifying
// the private key file.
$client = new BetaAnalyticsDataClient(['credentials' =>
    $credentials_json_path]);
// [END analyticsdata_json_credentials_initialize]

// [START analyticsdata_json_credentials_run_report]
// Make an API call.
$response = $client->runReport([
    'property' => 'properties/' . $property_id,
    'dateRanges' => [
        new DateRange([
            'start_date' => '2023-02-08',
            'end_date' => 'yesterday',
        ]),
    ],
    'dimensions' => [
        new Dimension([
            'name' => 'campaignName',
        ]),
        new Dimension([
            'name' => 'sourceMedium',
        ]),
    ],
    'metrics' => [
        new Metric([
            'name' => 'sessions',
        ]),
    ],
    'dimensionFilter' => [ 
        new FilterExpression([
            'filter' => [ 
                new Filter([ // 修正类名大小写
                    'field_name' => 'campaignName',
                    'in_list_filter' => [ new Filter\InListFilter([ // 修正类名大小写
                        'values' => ["edm", "naver_blog", "(direct)", "fourfour", "advertise", "kakao", "alliance" ],
                        'caseSensitive' => true,
                    ]),
                    ],
                ]),
            ],  
        ]),
    ],
    'orderBys' => [
        new OrderBy([
            'dimension' => new OrderBy\DimensionOrderBy([
                'dimension_name' => 'campaignName', 
                'order_type' => OrderBy\DimensionOrderBy\OrderType::ALPHANUMERIC
            ]),
            'desc' => false,
        ]),
    ],
    'keepEmptyRows' => true,
    
]);

// [END analyticsdata_json_credentials_run_report]

// [START analyticsdata_json_credentials_run_report_response]
// Print results of an API call.
print 'Report result: ' . PHP_EOL . '<br>';
foreach ($response->getRows() as $row) {
    print $row->getDimensionValues()[0]->getValue(). ' ' . $row->getDimensionValues()[1]->getValue(). ' ' .$row->getMetricValues()[0]->getValue() . PHP_EOL . '<br>';
}
// [END analytics_data_quickstart]

内容的提问来源于stack exchange,提问作者Ivan Choo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.30 07:27:12