ga_data: Google Analytics Data for GA4 (App+Web)

View source: R/ga_data.R

ga_dataR Documentation

Google Analytics Data for GA4 (App+Web)

Description

[Experimental]

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

Usage

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,
  metricAggregations = NULL,
  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

metricAggregations

Default NULL, pass in character vector of one or multiple of c("TOTAL","MAXIMUM","MINIMUM","COUNT") to return extra metadata

raw_json

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

Details

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

Value

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

See Also

Documentation on Data API

Other GA4 functions: ga_data_filter(), ga_data_order()

Examples


## Not run: 

# 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) 
# includes metricAggregation metadata
realtime <- ga_data(
  206670707,
  metrics = "activeUsers",
  dimensions = c("city","unifiedScreenName"),
  limit = 100,
  realtime = TRUE,
  metricAggregations = c("TOTAL","MAXIMUM","MINIMUM"))

# 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)
  )

## End(Not run)



googleAnalyticsR documentation built on Oct. 16, 2022, 1:06 a.m.