googleAnalyticsR (version 0.4.2)

make_ga_4_req: Make a Google Analytics v4 API fetch

Description

This function constructs the Google Analytics API v4 call to be called via fetch_google_analytics_4

Usage

make_ga_4_req(viewId, date_range = NULL, metrics = NULL,
  dimensions = NULL, dim_filters = NULL, met_filters = NULL,
  filtersExpression = NULL, order = NULL, segments = NULL,
  pivots = NULL, cohorts = NULL, pageToken = 0, pageSize = 1000,
  samplingLevel = c("DEFAULT", "SMALL", "LARGE"), metricFormat = NULL,
  histogramBuckets = NULL)

Arguments

viewId

viewId of data to get.

date_range

character or date vector of format c(start, end) or for two date ranges: c(start1,end1,start2,end2)

metrics

Metric to fetch. Supports calculated metrics.

dimensions

Dimensions to fetch.

dim_filters
met_filters
filtersExpression

A v3 API style simple filter string. Not used with other filters.

order

An order_type object

segments

List of segments as created by segment_ga4

pivots

Pivots of the data as created by pivot_ga4

cohorts

Cohorts created by make_cohort_group

pageToken

Where to start the data fetch

pageSize

How many rows to fetch. Max 10000 each batch.

samplingLevel

Sample level

metricFormat

If supplying calculated metrics, specify the metric type

histogramBuckets

For numeric dimensions such as hour, a list of buckets of data. See details in make_ga_4_req

Metrics

Metrics support calculated metrics like ga:users / ga:sessions if you supply them in a named vector.

You must supply the correct 'ga:' prefix unlike normal metrics

You can mix calculated and normal metrics like so:

customMetric <- c(sessionPerVisitor = "ga:sessions / ga:visitors", "bounceRate", "entrances")

You can also optionally supply a metricFormat parameter that must be the same length as the metrics. metricFormat can be: METRIC_TYPE_UNSPECIFIED, INTEGER, FLOAT, CURRENCY, PERCENT, TIME

All metrics are currently parsed to as.numeric when in R.

Dimensions

Supply a character vector of dimensions, with or without ga: prefix.

Optionally for numeric dimension types such as ga:hour, ga:browserVersion, ga:sessionsToTransaction, etc. supply histogram buckets suitable for histogram plots.

If non-empty, we place dimension values into buckets after string to int64. Dimension values that are not the string representation of an integral value will be converted to zero. The bucket values have to be in increasing order. Each bucket is closed on the lower end, and open on the upper end. The "first" bucket includes all values less than the first boundary, the "last" bucket includes all values up to infinity. Dimension values that fall in a bucket get transformed to a new dimension value. For example, if one gives a list of "0, 1, 3, 4, 7", then we return the following buckets: -

  • bucket #1: values < 0, dimension value "<0"

  • bucket #2: values in [0,1), dimension value "0"

  • bucket #3: values in [1,3), dimension value "1-2"

  • bucket #4: values in [3,4), dimension value "3"

  • bucket #5: values in [4,7), dimension value "4-6"

  • bucket #6: values >= 7, dimension value "7+"

See Also

Other GAv4 fetch functions: fetch_google_analytics_4_slow, fetch_google_analytics_4, google_analytics_4

Examples

Run this code
# NOT RUN {
# }
# NOT RUN {
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()

## pick a profile with data to query

ga_id <- account_list[23,'viewId']

ga_req1 <- make_ga_4_req(ga_id, 
                         date_range = c("2015-07-30","2015-10-01"),
                         dimensions=c('source','medium'), 
                         metrics = c('sessions'))

ga_req2 <- make_ga_4_req(ga_id, 
                         date_range = c("2015-07-30","2015-10-01"),
                         dimensions=c('source','medium'), 
                         metrics = c('users'))
                         
fetch_google_analytics_4(list(ga_req1, ga_req2))

# }
# NOT RUN {

# }

Run the code above in your browser using DataCamp Workspace