-
Can you use both 'and' and 'or' filters in a single Google Analytics API request?
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”
}
`
Log in to reply.