ga_data_filter: Filter DSL for GA4 filters

Description Usage Arguments Details Value See Also Examples

View source: R/ga_data_filter.R

Description

Use with ga_data to create filters

Usage

1

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:

Value

A FilterExpression object suitable for use in ga_data

See Also

Other GA4 functions: ga_data_order(), ga_data()

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
## Not run: 
# 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),]

## End(Not run)
## 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")

googleAnalyticsR documentation built on April 17, 2021, 5:06 p.m.