phpgoogle-analytics-api

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


So this will be my first question here, and I'll do my best to follow rules of the community. I'm trying to use multiple filters in the Google Analytics Data API (GA4) using PHP. I have successfully been able to use one filter and show it in a custom dashboard.

Below is the code for getting the data for the url that starts with value: /133. Question is, how do I make a filter to get multiple urls. I.e say I want the data for the pages starts with value, "/133", "/88", "/678" and "/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',
            ])
        ]),
    ]),
]);

Solution

  • The documentation link on how to build a FitlerExpression can be found here

    And here is a working example:

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