Extract data from Google Analytics with R

Web Analytics
Analytics R
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.

Frequently asked questions

How can data be extracted from Google Analytics with R?

To extract Google Analytics data with R, use a compatible library, authenticate the account, select the property, metrics, dimensions and dates, and save the results for later analysis.

Quick answer: Extract data from Google Analytics with R should be approached as a practical decision framework: clarify the objective, validate reliable data, prioritize actions with measurable impact and review results regularly. The goal is to connect each recommendation with business context, audience needs and continuous improvement.

AreaWhat to reviewUseful indicator
ObjectiveDefine the business goal, audience and expected outcome before applying any tactic.Qualified traffic, leads, sales or efficiency improvement.
Data and measurementCheck that the information used to decide is reliable, comparable and aligned with the channel.Conversion rate, attribution quality and trend evolution.
OptimizationPrioritize actions by impact, effort and learning potential instead of applying isolated changes.Improvement after implementation and next action identified.
Why connect Google Analytics with R?

It helps automate reports, combine data with other sources, create reproducible analyses, apply statistical models and generate custom visualizations beyond standard dashboards.

What knowledge is needed to use R with Analytics?

Basic R, APIs, Analytics metrics and dimensions, authentication, data cleaning and table structure are useful. It is also important to understand the analysis objective.

What mistakes should be avoided when extracting Analytics data with R?

Avoid requesting incompatible metrics, failing to document queries, ignoring sampling or API limits, mixing properties and not validating that data matches the Analytics configuration.