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

Description Usage Arguments Details Value See Also Examples

View source: R/ga_data.R

Description

[Experimental]

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

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
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

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

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

## End(Not run)

googleAnalyticsR documentation built on Oct. 7, 2021, 9:06 a.m.