pythongoogle-analyticsgoogle-analytics-4

How do I use multiple filters in the Google Analytics 4 API from Python?


I am using the Google Analytics 4 Data API with the Python client library to run reports. I'm able to run a report with a single FilterExpression to filter traffic where the country equals "United States". Now I want to add a second filter to return rows where country equals "United States" and region equals "New York".

However, I've only been able to get the code running with a single filter. Attempting to pass multiple filters has resulted in errors.

Working code with one filter:

from google.analytics.data_v1beta import BetaAnalyticsDataClient
from google.analytics.data_v1beta.types import (
    DateRange,
    Dimension,
    Filter,
    FilterExpression,
    Metric,
    RunReportRequest,
)


def sample_run_report(property_id):
    client = BetaAnalyticsDataClient()
    request = RunReportRequest(
        property=f"properties/{property_id}",
        dimensions=[
            Dimension(name="country"),
            Dimension(name="region")
        ],
        metrics=[Metric(name="activeUsers")],
        date_ranges=[DateRange(start_date="2020-03-31", end_date="today")],
        dimension_filter=FilterExpression(
            filter=Filter(
                field_name="country",
                string_filter=Filter.StringFilter(
                    value="United States",
                    match_type=Filter.StringFilter.MatchType.EXACT
                )
            )
        ), # TODO: Add second filter for New York region
    )
    response = client.run_report(request)

    print("Report result:")
    for row in response.rows:
        dimension_values = [dimension.value for dimension in row.dimension_values]
        metric_values = [metric.value for metric in row.metric_values]
        print(" | ".join(dimension_values + metric_values))

if __name__ == '__main__':
    # property_id is user-specific
    sample_run_report(property_id="236813742")

What I've tried:

Providing a list of Filters for filter=

dimension_filter=FilterExpression(
    filter=[
        Filter(
            field_name="country",
            string_filter=Filter.StringFilter(
                value="United States",
                match_type=Filter.StringFilter.MatchType.EXACT
            )
        ),
        Filter(
            field_name="region",
            string_filter=Filter.StringFilter(
                value="New York",
                match_type=Filter.StringFilter.MatchType.EXACT
            )
        )
    ]
)
# TypeError: Message must be initialized with a dict: google.analytics.data.v1beta.FilterExpression

Giving a list of FilterExpressions for dimension_filter=

dimension_filter=[
    FilterExpression(
        filter=Filter(
            field_name="country",
            string_filter=Filter.StringFilter(
                value="United States",
                match_type=Filter.StringFilter.MatchType.EXACT
            )
        )
    ),
    FilterExpression(
        filter=Filter(
            field_name="region",
            string_filter=Filter.StringFilter(
                value="New York",
                match_type=Filter.StringFilter.MatchType.EXACT
            )
        )
    )
]
# TypeError: Message must be initialized with a dict: google.analytics.data.v1beta.FilterExpression

Specifying an andGroup with a FilterExpressionList with filters specified in expressions=.

dimension_filter=FilterExpression(
    and_group=FilterExpressionList(
        expressions=[
            Filter(
                field_name="country",
                string_filter=Filter.StringFilter(
                    value="United States",
                    match_type=Filter.StringFilter.MatchType.EXACT
                )
            ),
            Filter(
                field_name="region",
                string_filter=Filter.StringFilter(
                    value="New York",
                    match_type=Filter.StringFilter.MatchType.EXACT
                )
            )
        ]
    )
)
# TypeError: Parameter to MergeFrom() must be instance of same class: expected <class 'FilterExpression'> got <class 'google.analytics.data_v1beta.types.data.Filter'>.

Question:

How do I specify more than one dimension filter when using the Google Analytics 4 API from the official Python client library?


Solution

  • To use multiple filters in dimension_filter, you have to nest each use a FilterExpression with and_group= set to a FilterExpressionList with expressions= set to a list of your desired FilterExpression objects.

    dimension_filter=FilterExpression(
        and_group=FilterExpressionList(
            expressions=[
                FilterExpression(
                    filter=Filter(
                        field_name="country",
                        string_filter=Filter.StringFilter(
                            value="United States",
                            match_type=Filter.StringFilter.MatchType.EXACT
                        )
                    )
                ),
                FilterExpression(
                    filter=Filter(
                        field_name="region",
                        string_filter=Filter.StringFilter(
                            value="New York",
                            match_type=Filter.StringFilter.MatchType.EXACT
                        )
                    )
                )
            ]
        )
    )
    

    Full code:

    from google.analytics.data_v1beta import BetaAnalyticsDataClient
    from google.analytics.data_v1beta.types import (
        DateRange,
        Dimension,
        Filter,
        FilterExpression,
        Metric,
        RunReportRequest,
        FilterExpressionList,
    )
    
    
    def sample_run_report(property_id):
        client = BetaAnalyticsDataClient()
    
        request = RunReportRequest(
            property=f"properties/{property_id}",
            dimensions=[
                Dimension(name="country"),
                Dimension(name="region")
            ],
            metrics=[Metric(name="activeUsers")],
            date_ranges=[DateRange(start_date="2020-03-31", end_date="today")],
            dimension_filter=FilterExpression(
                and_group=FilterExpressionList(
                    expressions=[
                        FilterExpression(
                            filter=Filter(
                                field_name="country",
                                string_filter=Filter.StringFilter(
                                    value="United States",
                                    match_type=Filter.StringFilter.MatchType.EXACT
                                )
                            )
                        ),
                        FilterExpression(
                            filter=Filter(
                                field_name="region",
                                string_filter=Filter.StringFilter(
                                    value="New York",
                                    match_type=Filter.StringFilter.MatchType.EXACT
                                )
                            )
                        )
                    ]
                )
            )
        )
        response = client.run_report(request)
    
        print("Report result:")
        for row in response.rows:
            dimension_values = [dimension.value for dimension in row.dimension_values]
            metric_values = [metric.value for metric in row.metric_values]
            print(" | ".join(dimension_values + metric_values))
    
    if __name__ == '__main__':
        sample_run_report(property_id="236813742")