如何使用 PHP 与 Google Analytics Data API (GA4) 一起使用多个过滤器

问题描述 投票:0回答:2

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

下面是获取以/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 google-analytics-api
2个回答
11
投票

有关如何构建 FitlerExpression 的文档链接可以在 here

找到

这是一个工作示例:

<?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',
                        ])
                    ]),
                ]),
            ]
        ]),
    ]),
]);

0
投票

我在想@Dikesh 解决方案会返回两个不同的计数。但结果与使用

inListFilter
的结果相同,将两个计数相加并返回一个聚合。

  $dimensionFilter = new FilterExpression([
    'filter' => new Filter([
      'field_name' => 'pagePath',
      'in_list_filter' => new inListFilter([
        'values' => ['/form/test', '/form/contact']
      ])
    ])
  ]);
© www.soinside.com 2019 - 2024. All rights reserved.