menu

Questions & Answers

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',
            ])
        ]),
    ]),
]);
Answers(1) :

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',
                        ])
                    ]),
                ]),
            ]
        ]),
    ]),
]);
Comments:
2023-01-18 23:01:19
Thanks for giving it a shot! I however received an error when trying out your sample. Fatal error: Uncaught Exception: Expect Google\Analytics\Data\V1beta\FilterExpression.
2023-01-18 23:01:19
@luciano Edited the answer with working example.
2023-01-18 23:01:19
thanks so much! that worked just perfectly! I initially did an ugly workaround and made a php loop of the whole code for each unique url. Performance was least to say shitty. But your code made it much better and cleaner as it should be. Thanks again @dikesh!
2023-01-18 23:01:20
one more thing. How would I do a loop for the Filterexpression using php. The values (/133 & /88) are dynamically generated from mysqli request. I tried making a while loop within the "expressions=>" section to dynamically add new "new FilterExpression([ 'filter' => new Filter(['field_name' => 'pagePath', 'string_filter' => new Filter\StringFilter([ 'match_type' => Filter\StringFilter\MatchType::BEGINS_WITH, 'value' => 'dynamic value', ])]),])," with the values. But keep getting "Parse error: syntax error, unexpected token "while", expecting "]" "
2023-01-18 23:01:20
You can create a separate array of FilterExpression objects and then use that array with expressions like 'expressions' => $filter_expressions
2023-01-18 23:01:20
Yet again, you have helped me! Worked out just fine using the array approach :) Thank you!