Obtain an access to the Reporting API

knitr::opts_chunk$set(
    comment = "#>",
    collapse = TRUE,
    error = FALSE,
    tidy = FALSE
)

To access the Reporting API Google Analytics RGA package provides the following functions:

All of this functions return a data frame including the data for a view (profile).

The following parameters are available for queries to the API reports:

The following arguments: profileId, start.date, end.date and metrics are required for get_ga() and get_mcf() (get_realtime() requires only profileId and metrics). Notice that all arguments must be character strings of unit length. The exception is profileId which can be both character string and numeric.

This is an example of data obtaining for the last 30 days:

ga_data <- get_ga(profileId = XXXXXXXX, start.date = "30daysAgo", end.date = "yesterday",
                  metrics = "ga:users,ga:sessions,ga:pageviews")

Sometimes it is necessary to obtain the data for the entire monitoring period through service Google Analytics. For these purposes the package RGA provides the function firstdate() which takes a charming profile ID (submission) as an argument:

fdate <- firstdate(profileId = XXXXXXXX)

Now we can use the variable fdate as the argument start.date when we call the get_ga() function:

ga_data <- get_ga(profileId = XXXXXXXX, start.date = fdate, end.date = "yesterday",
                  metrics = "ga:users,ga:sessions,ga:pageviews")

Sampled data

If at least one of the following thresholds is met Analytics samples data accordingly:

In order to avoid this, you can split the query into multiple small queries by month, week or day. You can get this day-by-day data by using the fetch.by param:

get_ga(profileId = XXXXXXXX, start.date = date, end.date = date,
    metrics = "ga:sessions", dimensions = "ga:keyword",
    filter = "ga:medium==organic;ga:keyword!=(not provided);ga:keyword!=(not set)",
    fetch.by = "day")

fetch.by allowed in the get_ga() and get_mcf() functions. We recomend you move from large to smaller date ranges until a sampling data occurs.

Note: fetch.by with the ga:users and ga:NdayUsers metrics is not correct because total users value for several days is not the sum of values for each single day. For details see the documentation.

References



Try the RGA package in your browser

Any scripts or data that you put into this service are public.

RGA documentation built on May 2, 2019, 6:47 a.m.