knitr::opts_chunk$set( comment = "#>", collapse = TRUE, error = FALSE, tidy = FALSE )
To access the Reporting API Google Analytics
RGA package provides the following functions:
get_ga()- returns Analytics data for a view (profile);
get_mcf()- returns Analytics Multi-Channel Funnels data for a view (profile);
get_realtime()- returns real time data for a view (profile).
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:
profileId- profile (view) ID. Can be obtained using the
list_profiles()or via the web interface Google Analytics;
start.date- the start date for collecting data formatted as YYYY-MM-DD. Also values such as "today", "yesterday", "ndaysAgo" are allowed, where n is the number of days;
end.date- the end date for collecting data formatted as YYYY-MM-DD. Also values such as "today", "yesterday", "ndaysAgo" are allowed, where n is the number of days;
metrics- comma-separated list of values of metrics (metrics), for example,
"ga:sessions,ga:bounces". The number of metrics cannot exceed 10 indicators for a single request;
dimensions- comma-separated list of values of measurements (dimensions), for example,
"ga:browser,ga:city". The number of dimensions cannot exceed 7 measurements at a single request;
sort- comma-separated list of metrics (metrics) and measurements (dimensions) which determine the order and direction of sorting data. The reverse sort order is defined by “-“ before the relevant metric;
filters- comma-separated list of metric (metrics) and measurement (dimensions) filters that are imposed when data is selected;
segment- segments that are used when retrieving data. Can be obtained using the
list_segmentsor via the web interface Google Analytics;
samplingLevel- the desired sampling level. Allowed values: "default", "faster", "higher_precision";
start.index- index of the first returned result (line number);
max.results- maximum number of fields (rows) of the returned results;
token- object of class
Token2.0which contains data with a valid authorization data. Can be obtained using the
The following arguments:
metrics are required for
get_realtime() requires only
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
ga_data <- get_ga(profileId = XXXXXXXX, start.date = fdate, end.date = "yesterday", metrics = "ga:users,ga:sessions,ga:pageviews")
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
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_mcf() functions. We recomend you move from large to smaller date ranges until a sampling data occurs.
fetch.by with the
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.
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.