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,, 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, = "30daysAgo", = "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 when we call the get_ga() function:

ga_data <- get_ga(profileId = XXXXXXXX, = fdate, = "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 param:

get_ga(profileId = XXXXXXXX, = date, = date,
    metrics = "ga:sessions", dimensions = "ga:keyword",
    filter = "ga:medium==organic;ga:keyword!=(not provided);ga:keyword!=(not set)", = "day") 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: 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.