Google APIs

- February 20, 2017

In the world of digital marketing, there is an insatiable thirst for data and reporting. In many cases this means logging into a marketing tool, finding the correct reports, exporting them into your spreadsheet and then finally manipulating the data.

After a long enough period, this habitual work becomes tiresome and expensive and it becomes worthwhile to invest development resources to automate the work. It is important to note that “premature optimization is the root of all evil” and that automated solutions can be expensive if they are misused. Automating a report that will only takes 5 minutes to put together once a week for a six month period is a poor use of everyone’s time.

If there is a legitimate need for continuous reporting that relies on a platform like Google Analytics for data, then frequently you can leverage an API to programmatically extract the information you need.

What You Need

Once you find an API that you want to leverage, like the Google Analytics API, then you need to select a programming language to interact with the API. Google is a big proponent of the “R” language which is an open statistical language that is versatile and easy to use for people who have minimal programming knowledge.

R uses packages to help simplify the amount of programming needed to accomplish a task.

So in order to access the Google Analytics API, you need to:

Downloading R:

The R programming language comes with an IDE (Integrated Development Environment). It is a fantastic, simple to use language that doesn’t require any mucking about in the terminal like other similar languages.

If you live in the United States you can go to https://cran.cnr.berkeley.edu/ and select the correct version for your operating system:

If you already know a little bit about programming and you want a more feature rich IDE than the default, I recommend installing Rstudio Open Source Edition.

https://www.rstudio.com/products/RStudio/#Desktop

One great example of this is RGoogleAnalytics package for R.
https://github.com/Tatvic/RGoogleAnalytics

We love this API because it comes with instructions that anyone can follow to start collecting data from the GA API.
https://github.com/LucyMcGowan/Tutorials/blob/master/googleanalytics.Rmd

Tell Google You will be Using Their API using the API Console

Now you have the program language you need to access the API, let’s make sure that Google knows that you are going to use their API by enabling it. This is done through https://console.developers.google.com.

Google Developers Console

Run a search for the “Google Analytics Reporting API”

Google Analytics Reporting API

Create a project and enable the API for your account.

Enable API

To enable the API you just need to press the enable link on the specific API page.

Enable

Now press the button to “Create credentials”, in this case we will create an Oauth client ID.

Create Creditials

Specify the application type as “Other”

Specify Other

You will now be given your client ID and secret in a popup. Do not share this, keep it private and safe!

Creating Your First R Script

Once you have downloaded and opened R, you will see something that looks like the screenshot below.

Open R

Create a new script by selecting File > New Script

Create New Script

Now you can paste in a script like the below to see your data:

#install the package from CRAN
install.packages(“RGoogleAnalytics”)
install.packages(“httr”)

#Update the client.id and client.secret from the file you downloaded
library(RGoogleAnalytics)
client.id <- “insert your client id here ”
client.secret <- “insert your secret id here”

token

save(token,file=”./token_file”)

#Customize this before running

query.init end.date = “2016-06-01”,
dimensions = c(“ga:sourcbpe”,”ga:medium”),
metrics = c(“ga:sessions”,”ga:bounces”),
max.results = 10000,
sort = “-ga:bounces”,
table.id = “insert your view ID here in format ga:########”)

query data

data

Now you should be seeing the data in R. You can now use this program in conjunction with your task scheduler to automate reports, push information to databases, and run complex statistical calculations!

MoreVisibility and Your API Projects:

If you are interested in reporting more deeply on your data using custom dashboards or integrating your CRM with Google Analytics, then please reach out to us. We have a depth of expertise in Google Analytics and Google Data Studio and can automate dashboards that surface your critical business intelligence easily.

Comments are closed at this time.

© 2017 MoreVisibility. All rights reserved