Learn R Programming

googleAnalyticsR (version 1.2.0)

ga_data_filter: Filter DSL for GA4 filters

Description

Use with ga_data to create filters

Usage

ga_data_filter(x)

Value

A FilterExpression object suitable for use in ga_data

Arguments

x

Filter DSL enabled syntax or the output of a previous call to this function - see examples

Details

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.

See Also

Other GA4 functions: ga_data(), ga_data_order()

Examples

Run this code

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")
# inlist string filter
ga_data_filter(city==c("Copenhagen","London"))
# AND string filters
ga_data_filter(city=="Copenhagen" & dayOfWeek == "5")
# ! - invert string filter
ga_data_filter(!(city=="Copenhagen" | city == "London"))

# 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

## numeric filter types
# numeric equal filter
ga_data_filter(sessions==5)
# between numeric filter
ga_data_filter(sessions==c(5,6))
# greater than numeric
ga_data_filter(sessions > 0)
# greater than or equal
ga_data_filter(sessions >= 1)
# less than numeric
ga_data_filter(sessions < 100)
# less than or equal numeric
ga_data_filter(sessions <= 100)

## string filter types
# begins with string
ga_data_filter(city %begins% "Cope")
# ends with string
ga_data_filter(city %ends% "hagen")
# contains string
ga_data_filter(city %contains% "ope")
# regex (full) string
ga_data_filter(city %regex% "^Cope")
# regex (partial) string
ga_data_filter(city %regex_partial% "ope")

# 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")
# ends with string (case insensitive)
ga_data_filter(city %ENDS% "Hagen")
# case insensitive exact
ga_data_filter(city %==%"coPENGhagen")

# avoid validation by making fields strings
ga_data_filter("city" %==%"coPENGhagen")

Run the code above in your browser using DataLab