google_analytics: Get Google Analytics v4 data

Description Usage Arguments Value Row requests Anti-sampling Resource Quotas Caching See Also Examples

View source: R/ga_v4_get.R

Description

Fetch Google Analytics data using the v4 API. For the v3 API use google_analytics_3. See website help for lots of examples: Google Analytics Reporting API v4 in R

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
google_analytics(
  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,
  useResourceQuotas = NULL,
  rows_per_call = 10000L
)

google_analytics_4(...)

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(s) to fetch as a character vector. You do not need to supply the "ga:" prefix. See meta for a list of dimensons and metrics the API supports. Also supports your own calculated metrics.

dimensions

Dimension(s) to fetch as a character vector. You do not need to supply the "ga:" prefix. See meta for a list of dimensons and metrics the API supports.

dim_filters

A filter_clause_ga4 wrapping dim_filter

met_filters

A filter_clause_ga4 wrapping met_filter

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

max

Maximum number of rows to fetch. Defaults at 1000. Use -1 to fetch all results. Ignored when anti_sample=TRUE.

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

anti_sample

If TRUE will split up the call to avoid sampling.

anti_sample_batches

"auto" default, or set to number of days per batch. 1 = daily.

slow_fetch

For large, complicated API requests this bypasses some API hacks that may result in 500 errors. For smaller queries, leave this as FALSE for quicker data fetching.

useResourceQuotas

If using GA360, access increased sampling limits. Default NULL, set to TRUE or FALSE if you have access to this feature.

rows_per_call

Set how many rows are requested by the API per call, up to a maximum of 100000.

...

Arguments passed to google_analytics

Value

A Google Analytics data.frame, with attributes showing row totals, sampling etc.

Row requests

By default the API call will use v4 batching that splits requests into 5 separate calls of 10k rows each. This can go up to 100k, so this means up to 500k rows can be fetched per API call, however the API servers will fail with a 500 error if the query is too complicated as the processing time at Google's end gets too long. In this case, you may want to tweak the rows_per_call argument downwards, or fall back to using slow_fetch = FALSE which will send an API request one at a time. If fetching data via scheduled scripts this is recommended as the default.

Anti-sampling

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. head(ga_data_unsampled, 50300)

anti_sample being TRUE will also set samplingLevel='LARGE' to minimise the number of calls.

Resource Quotas

If you are on GA360 and have access to resource quotas, set the useResourceQuotas=TRUE and set the Google Cloud client ID to the project that has resource quotas activated, via gar_set_client or options.

Caching

By default local caching is turned on for v4 API requests. This means that making the same request as one this session will read from memory and not make an API call. You can also set the cache to disk via the ga_cache_call function. This can be useful when running RMarkdown reports using data.

See Also

Other GAv4 fetch functions: fetch_google_analytics_4_slow(), fetch_google_analytics_4(), make_ga_4_req()

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
## Not run: 
library(googleAnalyticsR)

## authenticate, or use the RStudio Addin "Google API Auth" with analytics scopes set

ga_auth()

## get your accounts

account_list <- ga_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

# examine the meta table to see metrics and dimensions you can query
meta

## simple query to test connection
google_analytics(ga_id, 
                 date_range = c("2017-01-01", "2017-03-01"), 
                 metrics = "sessions", 
                 dimensions = "date")

## change the quotaUser to fetch under
google_analytics(1234567, date_range = c("30daysAgo", "yesterday"), metrics = "sessions")

options("googleAnalyticsR.quotaUser" = "test_user")
google_analytics(1234567, date_range = c("30daysAgo", "yesterday"), metrics = "sessions")


## End(Not run)

MarkEdmondson1234/googleAnalyticsR_public documentation built on Nov. 27, 2020, 5:27 p.m.