How to Connect to Google Analytics API

You want to be able to use Google Analytics API but you are struggling to use Google APIs?

Can’t find any good beginner documentation on the subject?

I understand exactly how you feel…

This is why I made this absolute beginner guide to help you connect to your Google Analytics API.

Let’s start, I will show you the easy steps to connect to the Google Analytics API (or any google API).

How to connect to the Analytics API

If this is your first time using an API, don’t worry, I will make it as easy as possible to help beginners understand how to use the Google Analytics API.

What is an API?

An API, or Application Programming Interface, is a way of communication between various software components. Basically, someone creates a software, and gives you access to parts of it via an API. In our situation, Google gives us access to some functionalities of Google Analytics via their API so you can use them from another platform.

Step 1: Create a Project in Google API

The first step to connect to the Google Analytics API is to create your project.

There are 3 simple steps to follow to do that.

1. Sign-in to Google’s developers console

2. Create a new project

Click Select a project > New Project

Here, I will call my project google-analytics-api

Step 2: Activate the Google Analytics Reporting API

Now we will search for the Google Analytics Reporting API and activate it.

1.Go to your Dashboard and click “Enable APIs and Services”

2. Search for “Google Analytics Reporting API”

3. Click on the API to create

4. Click on “Enable”

enable api

Step 3: Get your API Key

Congratulations, you have enabled your Google Analytics API. You will now learn how to download an API key and create your credentials within the developer console.

What is an API key? 

The API Key is the equivalent of your password when you log-in Google Analytics. A single API key gives you access to all of the Google API services. This is the equivalent of using the same Gmail address to log into Google Analytics and Google AdWords.

Depending on the API that you are using, you will have three different types of keys: a simple text key like “jTnfdas-ANDSOASD“, a user OAuth credentials and a service account key.

How Can I Get an API KEY?

Now that we have enabled the Analytics API, you will get your API Key by creating your credentials. Basically, you want to create your and your client.secret.

Remember, this is your “password”. you will need it to connect to the API will ask you for your and client.secret.

1. Create credentials

Go in the “credential” tab on the left, click on “Credentials in APIs & Services”.

Create Credentials – Google Analytics

2. Select OAuth client ID

Click on the blue box “create credentials” and select “OAuth Client ID”.

auth client id
OAuth Client ID

3. Set product name on consent screen

Now it is time to give a name to your project. This will help you know that you try to connect to the right API.

To do this, click on “configure consent screen”. Here I will call this Knime Credentials since it is part of a previous project on How to Make Google Analytics Queries from KNIME.

  1. Give product a name
  2. The rest is optional, leave it blank if you want
  3. Click Save.
Give a name to your consent screen.

4. Select Your Application Type

Now we choose the application type that we will be using.

  1. Select “other” and name it.
  2. Click “Create”
Select Application Type

Splendid! There is your and client.secret .

Step 3: Save your and client.secret

Splendid! you have generated your and your client.secret. Keep it somewhere close, you’ll need it to make your first API call from KNIME or RStudio and import Google Analytics Data.

copy client id and client secret

This is the end of this tutorial.

To learn more read how to connect to the Google Search Console API or the Google AdWords API.