Learn business growth with Google Analytics 4 Forums Google Analytics 4 Can you use both 'and' and 'or' filters in a single Google Analytics API request?

  • Can you use both 'and' and 'or' filters in a single Google Analytics API request?

    Posted by Oliver on 8 August 2022 at 3:42 am

    Hey there! I’m trying to run a Google Analytics report using the API, and I need it filtered with both “and” and “or” expressions. If I understand correctly, we can only use one at a time – “and” or “or”.

    I need the report to filter stuff that matches conditions like: A OR B AND C OR D. Where A, B, C, and D are a combo of metrics and dimensions.

    Can you help me construct a sample request that does this?

    Here’s a sample of a request I tried, but it threw back an error that says: “Invalid value at ‘dimension_filter’ (oneof), oneof field ‘expr’ is already set. Cannot set ‘andGroup'”.

    I’ve attached my attempt here, maybe you can spot where I went wrong? And yeah, I also want to use “and” and “or” filtering for metric filters in the same request if that’s doable.

    I’m looking forward to hearing what you think! Cheers!

    `json
    {
    “dateRanges”: [
    {
    “startDate”: “2019-01-01”,
    “endDate”: “2023-05-02”,
    “name”: “0”
    }
    ],
    “keepEmptyRows”: “TRUE”,
    “metrics”: [
    {“name”: “Conversions”},
    {“name”: “EngagementRate”},
    {“name”: “EngagedSessions”},
    {“name”: “EventCount”},
    {“name”: “NewUsers”},
    {“name”: “TotalRevenue”},
    {“name”: “TotalUsers”}
    ],
    “dimensions”: [
    {“name”: “Browser”}
    ],
    “dimensionFilter”: {
    “orGroup”: {
    “expressions”: [
    {
    “filter”: {
    “fieldName”: “Browser”,
    “stringFilter”: {
    “matchType”: “CONTAINS”,
    “value”: “moz”
    }
    }
    },
    {
    “filter”: {
    “fieldName”: “Browser”,
    “stringFilter”: {
    “matchType”: “CONTAINS”,
    “value”: “test”
    }
    }
    }
    ]
    },
    “andGroup”: {
    “expressions”: [
    {
    “filter”: {
    “fieldName”: “Browser”,
    “stringFilter”: {
    “matchType”: “CONTAINS”,
    “value”: “ed”
    }
    }
    },
    {
    “filter”: {
    “fieldName”: “Browser”,
    “stringFilter”: {
    “matchType”: “CONTAINS”,
    “value”: “ch”
    }
    }
    }
    ]
    }
    },
    “offset”: “0”,
    “limit”: “10000”
    }
    `

    William replied 1 year, 4 months ago 2 Members · 1 Reply
  • 1 Reply
  • William

    Member
    29 March 2023 at 7:15 pm

    Sure, I’d be happy to help! You’re correct in that you can’t specify both andGroup and orGroup in the same filter, but there’s a way to work around this. You can nest one group within another.

    Here’s how you can do that:
    `json
    “dimensionFilter”: {
    “orGroup”: {
    “expressions”: [
    {
    “filter”: {
    “fieldName”: “Browser”,
    “stringFilter”: {
    “matchType”: “CONTAINS”,
    “value”: “moz”
    }
    }
    },
    {
    “andGroup”: {
    “expressions”: [
    {
    “filter”: {
    “fieldName”: “Browser”,
    “stringFilter”: {
    “matchType”: “CONTAINS”,
    “value”: “ed”
    }
    }
    },
    {
    “filter”: {
    “fieldName”: “Browser”,
    “stringFilter”: {
    “matchType”: “CONTAINS”,
    “value”: “ch”
    }
    }
    }
    ]
    }
    },
    {
    “filter”: {
    “fieldName”: “Browser”,
    “stringFilter”: {
    “matchType”: “CONTAINS”,
    “value”: “test”
    }
    }
    }
    ]
    }
    }
    `
    This would match a Browser field that either contains “moz”, contains both “ed” and “ch”, or contains “test”. Hope this helps!

    As for using “and” and “or” filters with metric filters in the same request, you can use a similar structure. However, do note that only certain metrics can be used for metric filters, and not all metrics will work. You should refer to the Google Analytics API documentation for more information.

Log in to reply.