Google Analytics data to R

Exporting Google Analytics data to R

If you are using Google Analytics with just the default data, you are probably using only 30 percent of what Google Analytics is capable of. So how to use Google Analytics to maximize it’s utility? Adding custom dimensions and custom metrics can be one way. Integrating GA with other statistical tools can actually give a whole lot of new insights. In the following post, I’ll be listing down the steps to import Google Analytics data to R programming.

Before directly jumping to the steps, I would like to mention a few advantages of using R for GA data.

  • Google Analytics only takes a small fraction of the entire data, often referred to as the sampling effect. Importing data to R enables us to overcome this hurdle and gives us a clearer picture of the data.
  • Visualizations always help us to get a general idea of the data – trends, volumes, outliers and what not! That’s where the R’s ggplot2 package can be used, to identify the underlying pattern as well as make projections from the data.

Steps for exporting data from Google Analytics to R

Installing required packages in R


Authorization process

In order to extract data from Google Analytics, an access token is required. In order to generate the access token, we require ClientID and ClientSecret (You can call them credentials) which we can get from Google Developer’s Console.

Steps to create ClientID and ClientSecret

  • Go to Google Developer Console and sign in with the email ID you want API access for.
  • Create a new project.
  • Select the Analytics API and click on enable.

  • Select OAuth client ID from Create credentials list.
  • Click at configure consent screen.
  • Enter product name as RStudio and click Save.
  • Save these credentials in respective variables i.e. and client.secret.

After getting the ClientID and ClientSecret, you just need to generate the access token by the following command:

token <- Auth(,client.secret)

Note: If during your analysis ‘Invalid Credentials’ shows up, the token has to be refreshed/validated again. The token is valid only for 30 minutes.

Error in ParseDataFeedJSON(GA.Data) :

code : 401 Reason : Invalid Credentials

Type the following command in your R console:


Exporting GA data

Query explorer can be used to see what all data can be imported. It allows you to try various dimensions and metrics and also to apply filters to the data.

After getting the glimpse of the data, you can finally export Google Analytics data to R with the following code snippet.

query.list<-Init( = "2017-02-01", = "2017-12-31",

dimensions = "ga:date",

metrics = "ga:users",

query<- QueryBuilder(query.list)

data<- GetReportData(query,token,split_daywise = TRUE)

NOTE: In order to avoid the sampling effect of the data, ‘split_daywise = TRUE’ has to be used.

The limitation of this process is that we can only import 10000 records at a time. To overcome this limitation, a small piece of code can be added.

GetReportData(query,token,paginate_query = TRUE).
Pagination allows us to avoid the limitation of extracting 10k records at a time.

We hope we were able to take you through the process.

So let’s expand the Google Analytics’ data analysis capabilities and dig deep with the statistical powerhouse – R programming.

Share this: