在使用GA4数据API V1 Beta中的inListFilter筛选器时发生错误。

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

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 &#39;/home/hosting_users/fedex/www/innofish/google_api/vendor/autoload.php&#39;;

 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 = &#39;0000000000&#39;;

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

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

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

// [END analyticsdata_json_credentials_run_report]

// [START analyticsdata_json_credentials_run_report_response]
// Print results of an API call.
print &#39;Report result: &#39; . PHP_EOL . &#39;&lt;br&gt;&#39;;
foreach ($response-&gt;getRows() as $row) {
    print $row-&gt;getDimensionValues()[0]-&gt;getValue(). &#39; &#39; . $row-&gt;getDimensionValues()[1]-&gt;getValue(). &#39; &#39; .$row-&gt;getMetricValues()[0]-&gt;getValue() . PHP_EOL . &#39;&lt;br&gt;&#39;;
}
// [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

huangapple
  • 本文由 发表于 2023年2月27日 16:16:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/75578118.html
匿名

发表评论

匿名网友

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

确定