luciano
luciano

Reputation: 83

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

Upvotes: 8

Views: 7630

Answers (2)

No Sssweat
No Sssweat

Reputation: 418

I was thinking @Dikesh solution would return two different counts. But turns out it's same result as using inListFilter which adds up the two counts and returns one aggregate.

  $dimensionFilter = new FilterExpression([
    'filter' => new Filter([
      'field_name' => 'pagePath',
      'in_list_filter' => new inListFilter([
        'values' => ['/form/test', '/form/contact']
      ])
    ])
  ]);

Upvotes: 0

dikesh
dikesh

Reputation: 3125

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

Upvotes: 12

Related Questions