Get Google Analytics v4 data (single request)
Will perform automatic batching if over the 10000 row per API call limit.
google_analytics_4(viewId, date_range = NULL, metrics = NULL, dimensions = NULL, dim_filters = NULL, met_filters = NULL, filtersExpression = NULL, order = NULL, segments = NULL, pivots = NULL, cohorts = NULL, max = 1000, samplingLevel = c("DEFAULT", "SMALL", "LARGE"), metricFormat = NULL, histogramBuckets = NULL, anti_sample = FALSE, anti_sample_batches = "auto", slow_fetch = FALSE)
viewId of data to get.
character or date vector of format
c(start, end)or for two date ranges:
Metric to fetch. Supports calculated metrics.
Dimensions to fetch.
A v3 API style simple filter string. Not used with other filters.
An order_type object
List of segments as created by segment_ga4
Pivots of the data as created by pivot_ga4
Cohorts created by make_cohort_group
Maximum number of rows to fetch. Defaults at 1000. Use -1 to fetch all results.
If supplying calculated metrics, specify the metric type
For numeric dimensions such as hour, a list of buckets of data. See details in make_ga_4_req
If TRUE will split up the call to avoid sampling.
"auto" default, or set to number of days per batch. 1 = daily.
For large results this slows down the API requests to avoid 500 errors
A Google Analytics data.frame
anti_sample being TRUE ignores
max as the API call is split over days
to mitigate the sampling session limit, in which case a row limit won't work. Take the top rows
of the result yourself instead e.g.
If you are lucky enough to need sub-day sampling, it will attempt to fetch per hour, but you are
restricted to not using
dim_filter argument if this is the case.
anti_sample being TRUE will also set
samplingLevel='LARGE' to minimise
the number of calls.
library(googleAnalyticsR) ## authenticate, or use the RStudio Addin "Google API Auth" with analytics scopes set ga_auth() ## get your accounts account_list <- google_analytics_account_list() ## account_list will have a column called "viewId" account_list$viewId ## View account_list and pick the viewId you want to extract data from ga_id <- 123456 ## simple query to test connection google_analytics_4(ga_id, date_range = c("2017-01-01", "2017-03-01"), metrics = "sessions", dimensions = "date")