Search code examples
phpgoogle-analytics-apigoogle-analytics-4

Error when using inListFilter filter in GA4 Data API V1 Beta


I am going to import the data using the inListFilter of idimensionFilter. However, an error message appears as below. "Fatal error: Uncaught Error: Class 'Google\Analytics\Data\V1beta\Filter\inListFilter' not found" Please check which part is wrong.

I've declared all the libraries I need, but I don't understand that I can't find the class. Other than the filter, the data is imported because it works well, but there are many examples of string filter, but few examples of list filter are found, so it is impossible to implement.

// [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]

Solution

  • If you're using Composer, try:

    composer dump-autoload --no-scripts --no-interaction
    

    In root of project.

    But if that does not fix the issue, then instead of:

    use Google\Analytics\Data\V1beta\Filter\inListFilter;
    

    Try:

    use Google\Analytics\Data\V1beta\Filter\InListFilter;
    

    Also, a sample can be found at: run_report_with_dimension_in_list_filter.php