Skip to content
Advertisement

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 FilterStringFilter([
                'match_type' => FilterStringFilterMatchType::BEGINS_WITH,
                'value' => '/133',
            ])
        ]),
    ]),
]);

Advertisement

Answer

Here is the working example:

<?php
require 'vendor/autoload.php';

use GoogleAnalyticsDataV1betaBetaAnalyticsDataClient;
use GoogleAnalyticsDataV1betaDateRange;
use GoogleAnalyticsDataV1betaDimension;
use GoogleAnalyticsDataV1betaMetric;
use GoogleAnalyticsDataV1betaFilter;
use GoogleAnalyticsDataV1betaFilterExpression;
use GoogleAnalyticsDataV1betaFilterExpressionList;

$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 FilterStringFilter([
                            'match_type' => FilterStringFilterMatchType::BEGINS_WITH,
                            'value' => '/133',
                        ])
                    ]),
                ]),
                new FilterExpression([
                    'filter' => new Filter([
                        'field_name' => 'pagePath',
                        'string_filter' => new FilterStringFilter([
                            'match_type' => FilterStringFilterMatchType::BEGINS_WITH,
                            'value' => '/88',
                        ])
                    ]),
                ]),
            ]
        ]),
    ]),
]);
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement