Working with the Account APIs for Google Tag Manager and Google Analytics
The agenda of this documentation is to give a brief about the working process for GA and GTM management accounts API. The motive of this documentation is to give a very brief preview of how one can easily utilize the Google Analytics and Google Tag Manager account API to get the list of active Accounts with the corresponding Account Id.
Note: I have not utilized Google’s client library to handle the authentication and the API calls. Instead, I have written a custom script which would do the job for us. The script would drive the process of getting access tokens from the authorization server and based on the access token would hit the requested API to get the corresponding data which would be filtered to get the respective data out.
Google APIs extensively use OAuth2.0 to validate any user trying to access any of the APIs. Please follow the document An introduction to OAuth2.0 to understand the working process for OAuth2.0
Now once you have an understanding of how to handle OAuth request please follow the respective steps to understand the work process of the application.
- Python should be installed on your machine.
- cURL – it is a command line tool which can be used to make POST or GET request to an API and get the required result. To install curl you just have to type in
brew install curl
Guideline to Oauth Authorization
Create a project in “Google Cloud Platform“. To create a project click on the ‘+’ icon. Please check the screenshot for more reference.
Or you can select a specific project to which you have access to as well for a particular account.
After selecting a specific project click on the breadcrumb and select the option “APIs&Services” for the specific project which has been selected. Please check the screenshot for more reference.
Once you click on “APIs&Services” you will be redirected to the APIs&Services section
Now click on “Credentials“. Please check the screenshot for more reference.
Once you click on Credentials, you have to select any one of the existing “Oauth2.0 client ID” or you have to create a separate “Oauth2.0 client ID“. Now once you select a specific OAuth Client ID you have to download the JSON file which would have the respective client credentials. Please check the screenshot for more reference.
The downloaded JSON file would contain the following specific parameters
We would be just be needing the parameters client_id, client_secret and the redirect_uris to get the job done.
Make an authorization request through the browser in order to get the respective “authorization code“. To get the authorization code you have to make a request to the authorization server. Use this URL to authorize the user in order to get the authorization code.
You can get the following query parameters client_id, redirect_uri from the JSON file which you have downloaded and scope from the API documentation for the respective Google GA or GTM API.
Next step would be to get the “refresh_token” as because we would need this to generate the “access_token” every after 1 hour, as because the access token expires after one hour. In here we would need cURL to make a post request in order to get the refresh token.
curl \ --request POST \ --data "code=<authorization_code>&client_id=<client_id>&client_secret=<cllient_secret>&redirect_uri=<redirect_uri>&grant_type=authorization_code" \ https://accounts.google.com/o/oauth2/token
You can get the following query parameters client_id, client_secret, redirect_uri from the JSON file which you have downloaded.
Once you have the “refresh_token” you would need the refresh token to generate the access token which would give access to the application to hit the required API in order to get the required data.
curl \ --request POST \ --data 'client_id=<client_id>&client_secret=<client_secret>&refresh_token=<refresh_token>&grant_type=refresh_token' \ https://accounts.google.com/o/oauth2/token
You can get the following query parameters client_id, client_secret from the JSON file which you have downloaded and you would get the refresh_token as an output from the previous POST request.
So once you have the respective access token you can access any of the required GTM or GA API in order to get the list of all account details. While implementing the code it has to be kept in mind that the access token expires every after 1 hour. So, you would have to implement it in such a way that the implementation code would request the refresh_token every after one hour and would replace the access_token object.
Request GTM Accounts API to get Accounts Data
To request the GTM accounts API you would have to send a GET request to the corresponding API:
To read more about the API please follow the required documentation https://developers.google.com/tag-manager/api/v1/reference/accounts/list
Request GA Accounts API to get Accounts Data
To request the GA Analytics Accounts API you would have to send a GET request to the corresponding API:
For more information on the implementation please check the codebase from GitHub
|Published on Web Code Geeks with permission by Soumyajit Basu, partner at our WCG program. See the original article here: Working with the Account APIs for Google Tag Manager and Google Analytics|
Opinions expressed by Web Code Geeks contributors are their own.