You want to be able to use Google Analytics API or Google Search Console 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
How to Connect to a Google API
To connect to any API provided by Google, you need to take the following steps:
- Create a Project in Google API
Step 1: Create a Project in Google API
The first step to connect to any Google API is to create a project.
There are 3 simple steps to follow to do that.
- Sign-in to Google’s developers console
- Create a new project
- Click Select a project > New Project
Here, I will call my project google-analytics-api, since I want to connect to the Google Analytics API.
Step 2: Activate the API
To activate the API, you need to go to the “Enable APIs and Services” section and search for the relevant API. Here is an example where we search for the Google Analytics Reporting API and activate it.
- Go to your Dashboard and click “Enable APIs and Services”
- Search for “Google Analytics Reporting API”
- Click on the API to create
- Click on “Enable”
You can search for any existing API available using the search bar or the filters on the left. Here’s an example with the Google Search Console API.
Step 3: Get Your API Keys
To download your API keys, you need the API to be activated and then create the credentials that you need in the format that you need 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.
How Can I Get an API KEY?
Depending on the API and the application that you are using to call the API, you will have three different types of keys:
- Simple text key like “jTnfdas-
ANDSOASD “, - User OAuth credentials (step 3A)
- Service account key (step 3B)
Choose 3A or 3B ( whichever you need for your project)
Step 3A: Get your API Keys (via client ID and client secret)
Now that we have enabled the Analytics API, you will get your API Key by creating your credentials. Basically, you want to create your client.id and your client
Remember, this is your “password”. you will need it to connect to the API will ask you for your client.id and client.secret.
1. Create credentials
Go in the “credential” tab on the left, click on “Credentials in APIs & Services”.
2. Select OAuth client ID
Click on the blue box “create credentials” and select “OAuth Client ID”.
3. Set product name on the 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.
- Give the product a name
- The rest is optional, leave it blank if you want
- Click Save.
4. Select Your Application Type
Now we choose the application type that we will be using.
- Select “other” and name it.
- Click “Create”
Splendid! There is your client.id and client.secret .
5. Save your client.id and client.secret
Splendid! you have generated your client.id 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.
Step 3B: Get your API Keys (via Service Account)
If you use an application to read Google Analytics Data, you will need to create a service account key.
A service account is a special type of Google account intended to represent a non-human user that needs to authenticate and be authorized to access data in Google APIs.
Google
1. Create Your Credentials
Again, go in the “credential” tab on the left, click on “Credentials in APIs & Services”.
2. Select Service Account Key
Click on the blue box “create credentials” and select “Service Account Key”.
3. Create New Service Account
Under “service account”, select “new service account” and give a “service account name”.
You can choose whatever you want.
Then, select a role.
Click “select a role”>”App Engine”>”App Engine Admin”.
For the Key type, choose “JSON”.
Note that your Service Account ID will be useful later to connect to Google Analytics API. Store it somewhere safe.
If you already have a project, in the credentials tab you can click on manage service accounts.
Then click on create service account.
4. Give Your Service Account Access to Google Analytics
In the previous step, a JSON file was downloaded.
You will need to open this file to copy the “client_email” that will look something like this.
"spyder-ide@XXXXXXXXXX.iam.gserviceaccount.com"
Then, go to Google Analytics > Admin > View > User Management.
Or in Ga4, it is now Property Access Management.
Click the plus “+” button > “Add user”.
Then add the copied service account email as a user and click “Add”.
This is it!
You now have downloaded your JSON API key to your service account and granted access to Google Analytics.
Other APIs You Might Be Interested In
Looking for the next steps to improve your skills using APIs?
- How to Get Google Search Console API Keys
- How to use Google Analytics Reporting API v4
- How to Connect to Google Search Console API using Python
- Get All Your Search traffic With Google Search Console API (more than 50,000 keywords)
- How to use Reddit API With Python
- How to use Google Search Console API with R
- Request Access to the AdWords API (Simple Guide)
- How to connect to Google Ads API with R
- Guide to set-up credentials in RGoogleAnalytics API
This guide is part of the complete tutorial on the Google Search Console API.
Conclusion
You are now ready to start using Google Analytics API with Python and to use Google Analytics with R.
To learn more read how to connect to the Google Search Console API or the Google AdWords API.
SEO Strategist at Tripadvisor, ex- Seek (Melbourne, Australia). Specialized in technical SEO. Writer in Python, Information Retrieval, SEO and machine learning. Guest author at SearchEngineJournal, SearchEngineLand and OnCrawl.