[Experimental]

Fetches Google Analytics from the Data API for Google Analytics 4 (Previously App+Web)

ga_data(
  propertyId,
  metrics,
  date_range = NULL,
  dimensions = NULL,
  dim_filters = NULL,
  dimensionDelimiter = "/",
  met_filters = NULL,
  orderBys = NULL,
  limit = 100,
  page_size = 100000L,
  realtime = FALSE,
  raw_json = NULL
)

Arguments

propertyId

A GA4 property Id

metrics

The metrics to request - see ga_meta - set to NULL to only see dimensions

date_range

A vector with start and end dates in YYYY-MM-DD format - can send in up to four date ranges at once

dimensions

The dimensions to request - see ga_meta

dim_filters

Filter on the dimensions of the request - a filter object created by ga_data_filter

dimensionDelimiter

If combining dimensions in one column, the delimiter for the value field

met_filters

Filter on the metrics of the request - a filter object created by ga_data_filter

orderBys

How to order the response - an order object created by ga_data_order

limit

The number of rows to return - use -1 to return all rows

page_size

The size of API pages - default is 100000L rows

realtime

If TRUE then will call the real-time reports, that have a more limited set of dimensions/metrics - see valid real-time dimensions

raw_json

You can send in the raw JSON string for a Data API request which will skip all checks

Value

A data.frame tibble, including attributes metadata, metricAggregations and rowCount. Use ga_data_aggregations to extract the data.frames of metricAggregations

Details

This is the main function to call the Google Analytics 4 Data API.

See also

Examples


if (FALSE) {

# send up to 4 date ranges
multi_date <- ga_data(
  206670707,
  metrics = c("activeUsers","sessions"),
  dimensions = c("date","city","dayOfWeek"),
  date_range = c("2020-03-31", "2020-04-27", "2020-04-30", "2020-05-27"),
  dim_filters = ga_data_filter("city"=="Copenhagen"),
  limit = 100
  )


# metric and dimension expressions

# create your own named metrics
met_expression <- ga_data(
  206670707,
  metrics = c("activeUsers","sessions",sessionsPerUser = "sessions/activeUsers"),
  dimensions = c("date","city","dayOfWeek"),
  date_range = c("2020-03-31", "2020-04-27"),
  limit = 100
  )

# create your own aggregation dimensions
dim_expression <- ga_data(
  206670707,
  metrics = c("activeUsers","sessions"),
  dimensions = c("date","city","dayOfWeek", cdow = "city/dayOfWeek"),
  date_range = c("2020-03-31", "2020-04-27"),
  limit = 100
  )
  
# run a real-time report (no date dimension allowed)
realtime <- ga_data(
  206670707,
  metrics = "activeUsers",
  dimensions = c("city","unifiedScreenName"),
  limit = 100,
  realtime = TRUE)

# extract meta data from the table
ga_data_aggregations(realtime)

# add ordering
a <- ga_data_order(-sessions)
b <- ga_data_order(-dayOfWeek, type = "NUMERIC")

ga_data(
  206670707,
  metrics = c("activeUsers","sessions"),
  dimensions = c("date","city","dayOfWeek"),
  date_range = c("2020-03-31", "2020-04-27"),
  orderBys = c(a, b)
  )
}