1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

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

Discussão em 'Python' iniciado por Stack, Setembro 16, 2025.

  1. Stack

    Stack Membro Participativo

    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?

    Continue reading...

Compartilhe esta Página