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: profile.id, start.date, end.date and metrics are required for get_ga() and get_mcf() (get_realtime() requires only profile.id and metrics). Notice that all arguments must be character strings of unit length. The exception is profile.id which can be both character string and numeric.

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

ga_data <- get_ga(profile.id = 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:

first_date <- firstdate(profile.id = XXXXXXXX)

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

ga_data <- get_ga(profile.id = XXXXXXXX, start.date = first_date, 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 (day-by-day). You can get this day-by-day data by using the following code:

dates <- seq(as.Date("2012-01-01"), as.Date("2012-02-01"), by = "days")
ga_data <- do.call(rbind, lapply(dates, function(date) {
    get_ga(profile.id = 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)")
}))
ga_data <- aggregate(. ~ keyword, FUN = sum, data = ga_data)

Note: a formula in aggregate() function should include all Google Analyitcs dimensions without prefix ("ga:" or "mcf:").

References



jdeboer/RGA-1 documentation built on May 18, 2019, 11:29 p.m.