Humanlytics

View Original

Google Analytics APIs for Absolute Beginners

Chapter 1 — What the hack is an API and why is it important

Google Analytics APIs for Absolute Beginners

“What’s the point of using the Google Analytics API? Doesn’t the Google Analytics user interface already do everything I need?”

That’s definitely a question worth asking.

But, if you’re an experienced Google Analytics expert, you’ve probably had one of the following experiences:

  • Your channels and sources are not assigned correctly, and Google Analytics does not have an easy way to fix that

  • You need more flexibility in analyzing URL requests of your users, but you can’t in Google Analytics

  • You’ve wanted to automate your reporting so that you don’t have to parse reports daily

Almost all of those weaknesses, plus more, can be made up by using the Google Analytics API.

The Google Analytics API makes processing and analyzing Google Analytics data significantly more flexible than the traditional Google Analytics interface.

It also makes building automation products and artificial intelligence engines on top of Google Analytics data possible.

While the Google Analytics API remains an incredibly powerful product, it is not completely intuitive. Additionally, there are not a lot of guides out there to help non-technical users understand every single detail about the API, or how to use it in your business.

This guide is designed to fill that void and make the Google Analytics API truly accessible for people without intensive data science and programming backgrounds.

To help, we’ll progress slowly, taking things step by step.

We’ll explain all the necessary details to make sure that you gain a comprehensive understanding of the Google Analytics API.

This guide will be divided into multiple posts, focusing on one topic per post to leave no stone unturned about the Google Analytics API.

This guide will include limited code samples written in Python 3 just to illustrate some of the key concepts of the API. We’d recommend that you take some sort of Python 3 or programming tutorial if you want to gain a full understanding of this guide.

It’s not required though — don’t worry, we’re not giving you homework halfway through this blog post!

On the other side, if you are already a programming expert and experienced data scientist just looking for some documentation to get started programming using the API, you may have a better time with the Google Analytics Reporting API Documentation for a faster and more technical learning experience.

Alright, if you are ready, let’s get started!

Today, we are going to focus on the concept of API and why is it important for your data analytics journey.

What the Heck is an API?

We promise you this diagram will make sense in a bit

If you work with programmers or technical people for your job, you’ve probably noticed that they use the word “API” nearly as much as you use phrases like “LOL”.

That said, just like you say “LOL” over and over again without necessarily realizing it, programmers frequently use the word API without explaining what it means.

See this content in the original post

Here’s the definition:

API: noun An acronym meaning “application programming interface”

That really didn’t help, did it? Here’s a quick real-world example:

Using this analogy gave us a good excuse to end up getting ice cream after writing this

Let’s say you walk into an ice cream store and order two scoops of vanilla and strawberry ice cream on a waffle cone.

To do this, you walk to the counter and tell the worker your order. This triggers a series of actions from the worker:

  • First, they take out the ice cream scoop

  • Then they scoop your vanilla ice cream and places it on the cone

  • Then they scoop the strawberry ice cream and places it above the vanilla

  • After that, the worker hands you the cone

  • After you take the cone, the worker asks you for the appropriate amount of money

As the worker is completing these actions, they may ask you for additional information to help fulfill your request and return your result. (“what type of waffle cone would you like?”, “do you want any toppings?”, “would you like anything else?”)

Now, change the image in your head from a worker to a computer program, and you’ve got a basic idea of what an API is!

Essentially, an API is a structured protocol for a computer program to interact with users and fulfill specific requests from those users.

APIs can vary based on the format in which the request is sent to the computer. The most popular APIs are SOAP (Simple Object Access Protocol) and REST (Representational State Transfer.

Based on the format of the request sent to the computer, an API can be said to be of different protocols. The most popular ones being SOAP (Simple Object Access Protocol), and REST (REpresentational State Transfer).

In SOAP, requests are submitted and received via a file format called XML (illustrated below).

Between these two protocols — REST is far more popular than SOAP (we use REST at Humanlytics). Google Analytics uses REST API to manage all of its requests and interactions.

Don’t count SOAP out though! Google Adwords uses a series of SOAP APIs to manage all of its requests. We’ll come back to it later and give you a better introduction to it later.

Just a tiny bit more about REST APIs

Let’s talk a little bit more about REST API. Understanding it is vital to being able to use the Google Analytics API to perform various actions from data acquisition to changing our account settings. Just like everyday requests — computer requests vary in type and method.

Some of those requests are aimed at reading something from a server like getting your most recent Google Analytics data.

Some requests are for creating an item in the server, like adding a user or an account to Google Analytics.

Some requests are for modifying existing items on your server, as your conversion goals.

Finally, some requests are for deleting an item on your server, like an outdated customer segment.

Within the REST API protocol, these four major tasks are accomplished with one or more specific request types:

  • GET (read): This method is used when trying to get data from the server. Additional data can be passed to supplement this request through a URL, e.g. http://www.getmedata.com?requestParameter1=data

  • POST (advanced read, create): POST is perhaps the most helpful method of all the four: it can be used for nearly anything. Frequently — it’s better than using GET, as POST allows you to attach a file with a request that specifies in detail what our request is (which is necessary with Google Analytics API). It can also be used to create new entries in the database, or initiate new processes.

  • PUT (update): This method is used primarily for updating data records in your server, such as your account information, your conversion goals, etc.

  • DELETE (delete): this method is used primarily for deleting records, and we won’t use it a lot in the Google Analytics API.

For Google Analytics, we will use primarily the POST and PUT requests to get data and manage our accounts, more on that later.

Why Use APIs?

After all those words on what an API is, it’s important to understand why an API is crucial for data analytics.

Using an API in data analytics gives you advantages in three key areas:

1) Automaticity (yes, this is a real word)

2) Flexibility

3) Potential

The first advantage is automaticity, which is the ability to perform multiple tasks automatically with little to no human supervision.

If you work with Google Analytics long enough, you will inevitably have the experience of manually copy-pasting data from Google Analytics to an Excel sheet for reporting or analysis.

With the Google Analytics API, you can set up an automated data pipeline (with some light programming) to pipe the data directly to where ever you want it to go, saving a lot of time and sanity down the road.

The second advantage is the flexibility to conduct customized analysis on Google Analytics data.

Even though the existing Google Analytics tool already has a lot of functionalities for you to play with, it lacks many data cleaning utilities, such as automated attribution.

The Google Analytics API can allow you to build those utilities on top of Google Analytics data to make your analytics journey a lot more powerful and customized.

Finally, saving our favorite for the last, the Google Analytics API has the potential to enable many cool features atop Google Analytics Data.

One of those is everyone’s favorite “Next Big Thing™” — artificial intelligence.

Because Google Analytics already provides a very robust and structured dataset, we can use the API to combine the data of multiple accounts and companies, allowing us to develop machine learning algorithms atop those data sets. This gives us access to a massive knowledge base of business insights and actions, and essentially automate insight generation in Google Analytics.

BTW: that’s what we do at Humanlytics, and we’re super hyped about what we can accomplish with it!

What APIs does Google Analytics offer?

Finally, let’s touch briefly on the APIs offered by Google Analytics.

Google Analytics offers two primary types of APIs:

  • Google Analytics Reporting API (currently in version 4)

  • Google Analytics Management API (currently in version 3)

The Google Analytics Reporting API is used for getting all sorts of data from a user’s Google Analytics account, while the Google Analytics Management API is designed to accomplish various account administrative actions such as adding an account, changing conversion goals and funnels, and etc.

For the sake of this guide, we are going to focus primarily on Google Analytics Reporting API while touching on the features of the Google Analytics Management API only when needed to help our explanation.

It might surprise you to learn that Google Analytics Reporting API contains only one API endpoint using the POST method (batchGet).

Even with only one endpoint, the Reporting API can help you obtain data from simple web statistics like session length to extremely segmented data, like average time spent on-page.

How does any of this work though?

That’s what we will talk about next week when we will continue our discussion and explore the ingenious way the Google Analytics Reporting API is designed, and understand how we can actually get data from the API system!

This article was produced by Humanlytics. Looking for more content just like this? Check us out on Twitter and Medium, and join our Analytics for Humans Facebook community to discuss more ideas and topics like this!