英文:
Error when using inListFilter filter in GA4 Data API V1 Beta
问题
我将只翻译代码部分,以下是代码的翻译:
// [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';
// 明确使用服务帐号凭据,通过指定
// 私钥文件。
$client = new BetaAnalyticsDataClient(['credentials' =>
$credentials_json_path]);
// [END analyticsdata_json_credentials_initialize]
// [START analyticsdata_json_credentials_run_report]
// 进行API调用。
$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 inListFilter([
'values' => ["edm", "naver_blog", "(direct)", "fourfour", "advertise", "kakao", "alliance"],
'caseSensitive' => true,
]),
],
]),
],
]),
],
'orderBys' => [
new OrderBy([
'dimension' => new 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]
// 打印API调用的结果。
print '报告结果: ' . 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]
希望这有助于你的代码理解。
英文:
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]
答案1
得分: 1
如果你正在使用Composer,请尝试:
composer dump-autoload --no-scripts --no-interaction
> 在项目的根目录执行。
但如果这不能解决问题,那么请尝试将:
use Google\Analytics\Data\V1beta\Filter\inListFilter;
改为:
use Google\Analytics\Data\V1beta\Filter\InListFilter;
> 另外,你可以在这里找到一个示例:
> run_report_with_dimension_in_list_filter.php
英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论