Table of Contents
1. Introduction to R ................................................................................................................................... 3
2. Set-up Project Credentials .................................................................................................................... 3
3. Process ...................................................................................................................................................... 5
3.1 Authentication: ................................................................................................................................... 5
3.2 Initiation: ............................................................................................................................................. 5
4. Benefits of using R: ................................................................................................................................... 6
4.1 Visualizations: ..................................................................................................................................... 6
4.2 Predictive Modeling: ........................................................................................................................... 7
1. Introduction to R
The goal is to encourage all data scientists and analysts to utilize R for their data crunching purposes.
This document walks you through on how to connect R and Google Analytics and see how we can
extract data.
R is a popular statistical language for computing and is a powerful tool for analyzing and drawing insights
from the data. When you are combing Google Analytics Data with R, we can perform different types of
statistical analysis and generate data visualizations to improve your business.
You can Install R here.
Since RGoogleAnalytics uses the Google Analytics Core Reporting API. Every request to the API should be
authorized under the OAuth2.0 protocol. An initial setup is required to get a unique set of project
credentials (Client ID and Client Secret).
2. Set-up Project Credentials
1. Navigate to Google Developers Console
2. Create a new project and open credentials page
3. Create credentials by selecting OAuth client ID
To proceed further, you will be asked to Configure consent screen first
After the consent screen configuration, in next step select Application Type - Other and click
Create.
The above step will generate OAuth client as below.
Once your Client ID and Client Secret are created, copy them to your R Script.
Enable the Google Analytics API from API Manager
3. Process
3.1 Authentication:
Once the project is set-up and the credentials are ready, we need to authenticate your Google Analytics
Account with your app. This ensures that your app (R Script) can access your Google Analytics data.
Once authentication is done, you will get a pair of tokens (Access Token and Refresh Token). An Access
Token is appended with each API request so that Google’s servers know that the requests came from
your app and they are authentic. Access Tokens will expire after 1 hour so they need to be regenerated
using the Refresh Token.
Install.packages(“RGoogleAnalytics”)
Library(RGoogleAnalytics)
# Authorize the Google Analytics account
# This need not be executed in every session once the token object is created and saved
client.id <- "xxxxxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com"
client.secret <- "xxxxxxxxxxxxxxxd_TknUI"
token <- Auth(client.id,client.secret)
# Save the token object for future sessions
save(token,file="./token_file")
3.2 Initiation:
The code below initiates a query with the Standard Query Parameters – Start Date, End Date,
Dimensions, Metrics and hits the query to the Google Analytics API. The API response is converted in the
form of a R Data-Frame.
query.list <- Init(start.date = "2017-01-14", //Start Date
end.date = "2017-01-16", //End Date
dimensions = "ga:city,ga:dimension5,ga:dimension6, //Dimensions
ga:productCategoryHierarchy,ga:productName",
metrics = "ga:productDetailViews,ga:productAddsToCart,ga:productCheckouts,
ga:uniquePurchases,ga:itemRevenue", //Metrics
max.results = 10000, //Maximum results required to initiate
sort = "-ga:productDetailViews", //Can use sorting by your own metric
table.id = "ga:XXXXXXXX") // Table-ID is View-ID of your Google Analytics
3.3 Validation:
# Create the Query Builder object so that the query parameters are validated
ga.query <- QueryBuilder(query.list)
3.4 Extraction:
# Extract the data and store it in a data-frame
ga.data <- GetReportData(ga.query,token,paginate_query =TRUE)
Data1 <- write.csv(ga.data,file="filename.csv") //Store in your local drive
4. Benefits of using R:
4.1 Visualizations:
One of the most appealing thing about R is its ability to create visualizations. There are 2 different kinds
of visualizations in R.
1. Basic Visualization
Histogram
Bar/line chart
Box plot
Scatterplot
2. Advanced Visualization
Heat Map
Mosaic Map
Map visualization
3D Graphs
Correlogram
4.2 Predictive Modeling:
One of the most important aspect using R is its usage in Predictive Modelling. There are different kinds
of predictive modeling we can apply in R by extracting the Google Analytics Data. Here are a few use
cases:
How do you predict whether a customer will make a purchase, click on an ad or churn from
your business? Business Problems like these require a mathematical framework called
Logistic Regression.
How do you know which of the actions of ecommerce store like Add to Wishlist, Add to Cart,
is most influential in driving Revenue?
Conclusion
Our goal here is to give analysts and data scientists a peek into R with Google Analytics from the vast
ocean of information that there is. Hopefully, this document has proven to be time efficient for you and
you’re now ready to start pulling the data from GA and play in R.
Please get in touch with us for any further information or in case of any queries.