How to Activate the Google Analytics API
Here's how to use Google's excellent API for their Analytics service, so that you can fetch, collect, and process data as you wish.
Google Analytics is one of the best — if not the best — services for getting statistics on how often a website is visited, how long visitors stay, which pages they visit, what type of device they visit from, and where they come from.
One disadvantage is that you have to log in to Google Analytics to access the data. Some may want to save it for archiving, or simply perform their analysis on the data that the service collects, and then it helps if you can access their API.
Activate Google Analytics API
It's time to get started with the Google Analytics API so that we can access the data that Analytics has collected about our visitors using our code.
To make the guide easy to follow despite its length, I will divide it into shorter sections where we address the different steps;
- Create a Project on Google Cloud Platform
- Create a Service Account within your project
- Create a JSON Key for authentication
- Grant access to Google Analytics
- Activate the Analytics API
We will go through each of these steps in detail below so that you can easily follow the activation without missing any important details.
Step 1: Create a project
The first step is to create a project and get a so-called Client ID. Start by going to Google Cloud Platform where you can manage so-called Service Accounts. If you haven't used the service before, you'll be presented with a pop-up box after logging in asking you to select a country and approve the service's policies ("Terms of Service").
As soon as you get past the initial settings, click the CREATE PROJECT button to create a project. You now need to enter a name for your project, and you can name it whatever you want. For simplicity's sake, however, you should name it something that has to do with the purpose, such as "My API project" or similar. Click CREATE to create your new project.
Step 2: Create a Service Account
When your new project on the Google Cloud Platform is created, you will be sent to the project's home page, which in our case is empty. What you need to do now is click on CREATE SERVICE ACCOUNT to create an account within your project.
Under the Service Account Details heading, enter the following information:
- The name of your account ("Service Account Name")
- The email address for the account ("Service Account ID")
Service Account ID is a type of email address for the new account and is an address you will use later. Choose something relatively easy to remember and recognize, so that you avoid misunderstandings in the future.
When you're done, click CREATE so that the new account is created within your project on the Google Cloud Platform.
Step 3: Create a JSON Key
Now we need to create a way to authenticate ourselves, and we do that by creating a key in JSON format that you get to download to your computer. This key is important to save, so remember where you save it.
The view you now see has the title Service Account Permissions. Here you can manage permission roles of various types, which is not something we need at this point. Just click CONTINUE to proceed. Once the page has finished loading, you should scroll down to the Create Key heading. Here you create your JSON key which will be saved to your computer and which you will later upload to your web server.
Click the CREATE KEY button to continue. You now get to choose what type of key you want to create: JSON or P12. Here we choose JSON as it is the more modern option than P12, which only exists for backward compatibility reasons.
When you've chosen the right Key Type, click the CREATE button to create your key. A dialog box appears concerning the download of your JSON key - save it in a place you remember so that you can easily find the key again.
Your key is now created, you have downloaded it in JSON format, and you can also see KEY ID under your Service Account. Also save this information, for example in Notepad.
Example of KEY ID: ea513f700c94d2143c2787020a3bf38f
Click DONE to complete the creation of your new key and return to your Service Account.
Step 4: Grant access to Google Analytics
It is time to provide authorization to your service account on Google Analytics. The information we need is visible on the page we are currently on, that is, our Service Account;
- Email: account@project.iam.gserviceaccount.com
- Key ID: 38523e3be0b1db8adbe22b0da35ba1ee
- Unique: 9849382432014
Here, the email address you need is the one that shows both which account and which project you are using. Copy it, store it in Notepad if you like, and then log in to your Google Analytics account.
Click Administrator and then go to the property you want to grant access to.
Note: Here you may want to grant access to all properties or just a few; that's up to you, but you should only grant access at the level that feels right. If you are the only one using the service, this doesn't matter much, but if there are several of you, you may want to discuss this before granting access to an API.
Click Property User Management and click the blue button labeled + to add a new user. Here's what you need to do:
- Enter the email address from your Service Account
- Set the permission to Read and Analyze
- Click Add to complete.
Now, your service account within your Google Cloud Platform project has access to your Analytics account and can authenticate itself to fetch data from the Google Analytics API.
There is just one step left before it works!
Step 5: Activate Analytics API
To activate the API within Google Analytics, visit the following addresses and activate the API functionality:
- analytics.googleapis.com
- analyticsdata.googleapis.com (required for Analytics v4)
On the page you now land on, it just says Google Analytics API with a brief description of how the API provides access to your Analytics data. The only thing you need to do on the page is to click the ENABLE button to enable API access to your account.
When the page has finished loading, you can confirm that it went well by checking what is under Activation Status; it should say ENABLED.
All done!
Now you're all set, and your Analytics account is fully ready to receive requests from scripts that you can write in PHP — more about this will come in a separate article.
Just remember to save your JSON key so that the PHP script that you will later write can authenticate itself against your Service Account.
Details
- Title: Tutorial: Activate the Google Analytics API
- Published:
- Author: Andrew Young
- Categories: API Guides