Google Analytics API Pulls
Lucy D'Agostino McGowan and Nick Strayer
June 10, 2016
We recently attempted to pull Google Analytics data and found the process a bit confusing, so we put together an overly specific step by step guide.
Go to the Google API Dashboard
- On the top right there is a "Select a Project" drop down. Click "Create a Project" to create one.
- In the Overview tab there is a "Google APIs" tab and an "Enabled APIs" tab on the right with a search bar below them. Search "Analytics API" and select the result by the same name.
- Select Application Type, use "Other UI"
- Follow steps to create a client id, then download the JSON data and extract the client ID and secret and put it in your script (or somewhere safer).
- The first time you implement this code, you will put the client id and client secret in (as demonstrated below). After this, you can save these tokens and load them for future use.
- load the
- For the first run, input the client id and client secret in quotes
- Create a
#Update the client.id and client.secret from the file you downloaded library(RGoogleAnalytics) client.id <- "client.id" client.secret <- "client.secret" token <- Auth(client.id,client.secret)
This will ask you if you want to "Use a local file to cache OAuth access credentials between R sessions?" Type
1 and follow the browser instructions.
# Save the token save(token,file="./token_file")
After the first time, you can use the following code to load and authorize the token
# Load the token load("./token_file") # Validate the token ValidateToken(token)
Create a query list
- Indicate the date range you are interested in accessing data from using the
metricsthat you are interested in the form
ga:then the name of the dimension/metric. A list of dimensions and metrics availabe can be found on the Google Analytics Website
table.idcan be found by navigating to your Google Analytics page. On the top there is an "Admin" tab. Click that and navigate to "View Settings" on the right. Under "Basic Settings" a "View ID" should be listed. Add this after the
- Optionally, you can chose to sort your data using the
sortargument. Here, we are sorting by the metric
bounces. You can also filter -- here we are filtering by referrals. You can also limit the maximum number of results using the
#Customize this before running query.init <- Init(start.date = "2015-01-01", end.date = "2016-06-01", dimensions = c("ga:source","ga:medium",...), metrics = c("ga:sessions","ga:bounces",...), max.results = 10000, sort = "-ga:bounces", filters="ga:medium==referral" table.id = "ga:123456")
QueryBuilder to build the query list and
GetReportData to pull the data into a data frame.
query <- QueryBuilder(query.init) data <- GetReportData(query, token)