R is an object-oriented language intended for statistical analysis. A,in all likelihood, one of the most used programming languages by statisticians and is used in many fields, from biotechnology to web analytics.
One of the great advantages of R is its versatility. If you’re not a programmer, it’s a language that shouldn’t scare you, and, if you follow this little tutorial, you’ll be extracting data from Google Analytics with R in less than an hour.
Table of Contents
For this we will first install R.
1. Download and install R
You will find the latest version for your operating system (Linux, Mac or Windows) on the official website of the R project
https://cran.r-project.org/bin/windows/Rtools/
Next we will install RStudio. RStudio is an integrated development environment (IDE) for R, which will allow us to operate with R in a more intuitive way. You can download its latest version on the official site:
https://www.rstudio.com/products/rstudio/download/
Once we have RStudio, we must install RTools:
https://cran.r-project.org/bin/windows/Rtools/
2. Using R with RStudio
Once we have installed the software we can start the RStudio program.
In the left console, after the symbol > we will enter the following commands:
1) We install and load the devtools package
install.packages(“devtools”)
library(devtools)
2) We install and load the rga package
This package will be the connector that allows us to start the extraction of data from Google Analytics
install_github(“rga”, “skardhamar”)
library(rga)
3) Authentication in Google Analytics
rga.open(instance=”ga”)
Then a tab will open in our browser requesting access and we will be provided with a password that we will enter in the console.
4) View ID selection
We need to define what the id of the view on which we are going to perform the query will be. We can get a list of all the views with the command:
ga$getProfiles()
We will obtain a list, from which we will choose the profile we want (for example: 12345678), and we will save it in a variable with the following command:
id < – “12345678”
5) Data query to Google Analytics
For the query we will use the command ga$getData.
Let’s look at a query as an example:
ga$getData(id,
start.date=”2015-01-01″,
end.date=”2015-03-31″,
metrics=”ga:sessions,ga:bounceRate, ga:percentNewSessions,ga:avgSessionDuration”,
dimensions=”ga:yearMonth”,
filters=”ga:source==google”,
segment=”gaid::-5″)
This query will return the monthly data of sessions, bounce rate, % of new sessions and average duration of the session of the first quarter of 2005, for organic searches of the Google search engine.
Parameters that have been used in this query:
- id: we have defined it as the view number
- start.date: The start date of the query. It has to go in YYYY-MM-DD format (year-month-day)
- end.date: End date of the query. It has to go in YYYY-MM-DD format (year-month-day)
- metrics: metrics
- dimensions: dimensions
- filters: filters
- segment: segment
If you have any questions about which metrics or dimensions to use, you can always check in developers.google.com/analytics/devguides/reporting/core/dimsmets
As you can see, this is just the beginning, but it is clarifying enough to see that you do not have to be a programmer to use R, and that anyone, with a little patience, can learn to use this useful language to perform advanced statistics.