Leveraging Google Analytics with R
Introduction
If you are using Google Analytics and would like to perform analysis using R, you can treat Google Analytics as a repository of datasets that you can download.
To do this, let me introduce to you to RGoogleAnalytics package. This package is a wrapper around Google Analytics API, specifically the Google Analytics Core Reporting API Version 3.0. It allows you to extract data from Google Analytics into R.
To follow this tutorial, you need to have a Google Analytics account and also R/RStudio.
Packages in R
The RGoogleAnalytics package resides in this repository. We will require devtools package to install this package in R.
# install package: devtools
install.packages("devtools")
# install package from github: RGoogleAnalytics
install_github("Tatvic/RGoogleAnalytics")
# load package: devtools
library(devtools)
# load package: RGoogleAnalytics
library(RGoogleAnalytics)
Google Analytics Core Reporting API
Now that we have completed the initial set up in R, we need to get our Client ID and Client Secret from Google Cloud Platform.
First, log into your Google Cloud Platform and create a new project. You should see a window like this to create the project.
Once created, on the left hand side menu bar, go to APIs & Services -> Credentials.
Click on the OAuth consent screen tab and fill in the necessary details.
Next, click on Credentials tab and click Create credentials, choose OAuth Client ID and then choose the Application type, set a client name and click Create.
Once you click create, you should be able to see your Client ID and Client Secret.
- Client ID: it is a public identifier for apps
- Client Secret: it is a secret known only to the application and the authorisation server
You can download the file. It will be in .json format.
Pulling Data from Google Analytics
Now that you have your Client ID and Client Secret, we will return to RStudio and create a token by authorising the Google Analytics account.
We will save Client ID and Client Secret in separate variables and create a token variable using Auth()
. R will ask you if you want to “Use a local file to cache OAuth access credentials between R sessions?”, type 1 and follow the instructions on the browser.
Note: This chunk of code needs to be run once (first run). We will save the access token.
# client ID and client secret (only run this on the first run)
client.id <- "paste your client id here in these quotes"
client.secret <- "paste your client secret here in these quotes"
# Authorise
token <- Auth(client.id, client.secret)
# Save the token
save(token,file="./token_file")
When you want to run the script again, you don’t have to run the previous code. We only need to load the saved access token. You can validate the token to see if it is successful or not.
Note: Run this chunk of code on any subsequent run.
# Load the token (use this after the first run of the script)
load("./token_file")
# Validate the token
ValidateToken(token)
Building Your Query
At this point, you have an access to the data on Google Analytics. To load your data, you need to build your query. The best thing to do this is to use Query Explorer on Google Analytics Demos & Tools.
Query Explorer allows you to set the parameters and if your query parameters are valid, it will return results, otherwise you need to fix the parameters. Once you are satisfied, you can copy the labels of the parameters into R.
Note: table.id is the ids on Query Explorer.
Here’s a sample of a query.
# Build a list of all the Query Parameters for campaigns
query.list <- Init(start.date = "2018-07-09",
end.date = "2018-07-09",
dimensions = "ga:dim1, ga:dim2",
metrics = "ga:met1,ga:met2",
sort = "ga:met1",
table.id = "ga:ids")
# Build query builder
query <- QueryBuilder(query.list)
# Pull data into a dataframe
data <- GetReportData(query, token)
To learn more on building your query, head over to this tutorial and Segments Developer Guide where you can learn on how to develop your segment parameter.
You can now analyse the data to your heart content using your favourite libraries in R.
You can also extract data directly from AdWords using its API, however, as AdWords contains a lot of raw data and potentially private data, you need to fill in a form (quite long) for Google to process your request.
I’ll post a quick tutorial when I have done the steps.