Use with ga_data to create filters
ga_data_filter(x)
Filter DSL enabled syntax or the output of a previous call to this function - see examples
A FilterExpression
object suitable for use in ga_data
This uses a specific filter DSL syntax to create GA4 filters that can be passed to ga_data arguments dim_filters
or met_filters
. Ensure that the fields you use are either all metrics or all dimensions.
The syntax uses operators and the class of the value you are setting (string, numeric or logical) to construct the filter expression object.
Fields including custom fields for your propertyId can be imported if you fetch them via ga_meta("data", propertyId = 12345)
before you construct a filter. If you do not want filters to be validated, then send them in as strings ("field").
The DSL rules are:
Single filters can be used without wrapping in filter expressions
A single filter syntax is (field) (operator) (value)
(field) is a dimension or metric for your web property, which you can review via ga_meta
(field) can be validated if you fetch metadata before you construct the filter. If you do this, you can call the fields without quote strings e.g. city
and not "city"
(operator) for metrics can be one of: ==, >, >=, <, <=
(operator) for dimensions can be one of: ==, \%begins\%, \%ends\%, \%contains\%, \%in\%, \%regex\%, \%regex_partial\%
for dimensions
dimension (operator) are by default case sensitive. Make them case insensitive by using UPPER case variations \%BEGINS\%, \%ENDS\%, ...
or ===
for exact matches
(value) can be strings ("dim1"
), numerics (55
), string vectors (c("dim1", "dim2")
), numeric vectors (c(1,2,3)
) or boolean (TRUE
) - the type will created different types of filters - see examples
Create filter expressions for multiple filters when using the operators: &, |, !
for logical combinations of AND, OR and NOT respectively.
Other GA4 functions:
ga_data_order()
,
ga_data()
if (FALSE) {
# start by calling ga_meta("data") to put valid field names in your environment
meta <- ga_meta("data")
# if you have custom fields, supply your propertyId to ga_meta()
custom_meta <- ga_meta("data", propertyId = 206670707)
custom_meta[grepl("^customEvent", custom_meta$apiName),]
}
## filter clauses
# OR string filter
ga_data_filter(city=="Copenhagen" | city == "London")
#> ===orGroup:
#> [[1]]
#> --GA4 Filter:
#> --| city
#> ----stringFilter:
#> value: Copenhagen | matchType: EXACT | caseSensitive: TRUE
#> [[2]]
#> --GA4 Filter:
#> --| city
#> ----stringFilter:
#> value: London | matchType: EXACT | caseSensitive: TRUE
# inlist string filter
ga_data_filter(city==c("Copenhagen","London"))
#> --| city
#> ----inListFilter:
#> values: Copenhagen London
#> caseSensitive: TRUE
# AND string filters
ga_data_filter(city=="Copenhagen" & dayOfWeek == "5")
#> ===andGroup:
#> [[1]]
#> --GA4 Filter:
#> --| city
#> ----stringFilter:
#> value: Copenhagen | matchType: EXACT | caseSensitive: TRUE
#> [[2]]
#> --GA4 Filter:
#> --| dayOfWeek
#> ----stringFilter:
#> value: 5 | matchType: EXACT | caseSensitive: TRUE
# ! - invert string filter
ga_data_filter(!(city=="Copenhagen" | city == "London"))
#> ===notExpression:
#> ===orGroup:
#> [[1]]
#> --GA4 Filter:
#> --| city
#> ----stringFilter:
#> value: Copenhagen | matchType: EXACT | caseSensitive: TRUE
#> [[2]]
#> --GA4 Filter:
#> --| city
#> ----stringFilter:
#> value: London | matchType: EXACT | caseSensitive: TRUE
# multiple filter clauses
f1 <- ga_data_filter(city==c("Copenhagen","London","Paris","New York") &
(dayOfWeek=="5" | dayOfWeek=="6"))
# build up complicated filters
f2 <- ga_data_filter(f1 | sessionSource=="google")
f3 <- ga_data_filter(f2 & !sessionMedium=="cpc")
f3
#> ===============andGroup:
#> [[1]]
#> ===========orGroup:
#> [[1]]
#> =======andGroup:
#> [[1]]
#> --GA4 Filter:
#> --| city
#> ----inListFilter:
#> values: Copenhagen London Paris New York
#> caseSensitive: TRUE
#>
#> [[2]]
#> ===orGroup:
#> [[1]]
#> --GA4 Filter:
#> --| dayOfWeek
#> ----stringFilter:
#> value: 5 | matchType: EXACT | caseSensitive: TRUE
#> [[2]]
#> --GA4 Filter:
#> --| dayOfWeek
#> ----stringFilter:
#> value: 6 | matchType: EXACT | caseSensitive: TRUE
#>
#>
#> [[2]]
#> --GA4 Filter:
#> --| sessionSource
#> ----stringFilter:
#> value: google | matchType: EXACT | caseSensitive: TRUE
#>
#> [[2]]
#> notExpression:
#> --GA4 Filter:
#> --| sessionMedium
#> ----stringFilter:
#> value: cpc | matchType: EXACT | caseSensitive: TRUE
## numeric filter types
# numeric equal filter
ga_data_filter(sessions==5)
#> --| sessions
#> ----numericFilter:
#> operation: EQUAL | value: 5
# between numeric filter
ga_data_filter(sessions==c(5,6))
#> --| sessions
#> ----betweenFilter:
#> from: 5 to: 6
# greater than numeric
ga_data_filter(sessions > 0)
#> --| sessions
#> ----numericFilter:
#> operation: GREATER_THAN | value: 0
# greater than or equal
ga_data_filter(sessions >= 1)
#> --| sessions
#> ----numericFilter:
#> operation: GREATER_THAN_OR_EQUAL | value: 1
# less than numeric
ga_data_filter(sessions < 100)
#> --| sessions
#> ----numericFilter:
#> operation: LESS_THAN | value: 100
# less than or equal numeric
ga_data_filter(sessions <= 100)
#> --| sessions
#> ----numericFilter:
#> operation: LESS_THAN_OR_EQUAL | value: 100
## string filter types
# begins with string
ga_data_filter(city %begins% "Cope")
#> --| city
#> ----stringFilter:
#> value: Cope | matchType: BEGINS_WITH | caseSensitive: TRUE
# ends with string
ga_data_filter(city %ends% "hagen")
#> --| city
#> ----stringFilter:
#> value: hagen | matchType: ENDS_WITH | caseSensitive: TRUE
# contains string
ga_data_filter(city %contains% "ope")
#> --| city
#> ----stringFilter:
#> value: ope | matchType: CONTAINS | caseSensitive: TRUE
# regex (full) string
ga_data_filter(city %regex% "^Cope")
#> --| city
#> ----stringFilter:
#> value: ^Cope | matchType: FULL_REGEXP | caseSensitive: TRUE
# regex (partial) string
ga_data_filter(city %regex_partial% "ope")
#> --| city
#> ----stringFilter:
#> value: ope | matchType: PARTIAL_REGEXP | caseSensitive: TRUE
# by default string filters are case sensitive.
# Use UPPERCASE operator to make then case insensitive
# begins with string (case insensitive)
ga_data_filter(city %BEGINS% "cope")
#> --| city
#> ----stringFilter:
#> value: cope | matchType: BEGINS_WITH | caseSensitive: FALSE
# ends with string (case insensitive)
ga_data_filter(city %ENDS% "Hagen")
#> --| city
#> ----stringFilter:
#> value: Hagen | matchType: ENDS_WITH | caseSensitive: FALSE
# case insensitive exact
ga_data_filter(city %==%"coPENGhagen")
#> --| city
#> ----stringFilter:
#> value: coPENGhagen | matchType: EXACT | caseSensitive: FALSE
# avoid validation by making fields strings
ga_data_filter("city" %==%"coPENGhagen")
#> --| city
#> ----stringFilter:
#> value: coPENGhagen | matchType: EXACT | caseSensitive: FALSE