Beruflich Dokumente
Kultur Dokumente
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.
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).
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)
# This need not be executed in every session once the token object is created and saved
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.
ga:productCategoryHierarchy,ga:productName",
metrics = "ga:productDetailViews,ga:productAddsToCart,ga:productCheckouts,
ga:uniquePurchases,ga:itemRevenue", //Metrics
# Create the Query Builder object so that the query parameters are validated
3.4 Extraction:
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.