繁体   English   中英

如何在使用 PHP 的 Google Analytics Data API (GA4) 中使用多个过滤器

[英]How to use multiple filters with Google Analytics Data API (GA4) using PHP

所以这将是我在这里的第一个问题,我会尽力遵守社区规则。 我正在尝试使用 PHP 在 Google Analytics Data API (GA4) 中使用多个过滤器。 我已经成功地能够使用一个过滤器并将其显示在自定义仪表板中。

下面是获取以 value: /133 开头的 url 的数据的代码。 问题是,如何制作过滤器以获取多个网址。 也就是说,我希望页面的数据以值“/133”、“/88”、“/678”和“/67”开头?

$response = $client->runReport([
    'property' => 'properties/' . $property_id,
    'dateRanges' => [
        new DateRange([
            'start_date' => '2022-01-01',
            'end_date' => 'today',
        ]),
    ],
    'dimensions' => [
        new Dimension(['name' => 'pageTitle',]),
        new Dimension(['name' => 'fullPageUrl',]),
    ],
    'metrics' => [
        new Metric(['name' => 'screenPageViews',]),
        new Metric(['name' => 'activeUsers',]),
        new Metric(['name' => 'newUsers',]),
        new Metric(['name' => 'userEngagementDuration',]),
    ],
    'dimensionFilter' => new FilterExpression([
        'filter' => new Filter([
            'field_name' => 'pagePath',
            'string_filter' => new Filter\StringFilter([
                'match_type' => Filter\StringFilter\MatchType::BEGINS_WITH,
                'value' => '/133',
            ])
        ]),
    ]),
]);

这是工作示例:

<?php
require '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\FilterExpressionList;

$property_id = 'XXXXXX';

$client = new BetaAnalyticsDataClient();

$response = $client->runReport([
    'property' => 'properties/' . $property_id,
    'dateRanges' => [
        new DateRange([
            'start_date' => '2022-01-01',
            'end_date' => 'today',
        ]),
    ],
    'dimensions' => [
        new Dimension(['name' => 'pageTitle',]),
        new Dimension(['name' => 'fullPageUrl',]),
    ],
    'metrics' => [
        new Metric(['name' => 'screenPageViews',]),
        new Metric(['name' => 'activeUsers',]),
        new Metric(['name' => 'newUsers',]),
        new Metric(['name' => 'userEngagementDuration',]),
    ],
    'dimensionFilter' => new FilterExpression([
        'or_group' => new FilterExpressionList([
            'expressions' => [
                new FilterExpression([
                    'filter' => new Filter([
                        'field_name' => 'pagePath',
                        'string_filter' => new Filter\StringFilter([
                            'match_type' => Filter\StringFilter\MatchType::BEGINS_WITH,
                            'value' => '/133',
                        ])
                    ]),
                ]),
                new FilterExpression([
                    'filter' => new Filter([
                        'field_name' => 'pagePath',
                        'string_filter' => new Filter\StringFilter([
                            'match_type' => Filter\StringFilter\MatchType::BEGINS_WITH,
                            'value' => '/88',
                        ])
                    ]),
                ]),
            ]
        ]),
    ]),
]);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM