Home » Web Dev » Working with the Account APIs for Google Tag Manager and Google Analytics

About Soumyajit Basu

Soumyajit Basu
Soumyajit is a QA/DevOps engineer by profession and a technology enthusiast by passion. He loves communicating about technology and is an author at his own blog platform as well as in Dzone and Web Code Geeks.

Working with the Account APIs for Google Tag Manager and Google Analytics

Agenda

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.

Oauth 2.0

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.

Requirements

  1. Python should be installed on your machine.
  2. 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

Step-1

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.

Step-2

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

Step-3

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

  • client_id
  • project_id
  • auth_uri
  • token_uri
  • auth_provider_x509_cert_url
  • client_secret
  • redirect_uris

We would be just be needing the parameters client_idclient_secret and the redirect_uris to get the job done.

Step-4

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.

https://accounts.google.com/o/oauth2/auth?client_id=<client_id>&redirect_uri=<redirect_uri>&scope=<api_scope>&response_type=  <code>

You can get the following query parameters client_idredirect_uri from the JSON file which you have downloaded and scope from the API documentation for the respective Google GA or GTM API.

Step-5

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_idclient_secretredirect_uri from the JSON file which you have downloaded.

Step-6

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_idclient_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:

https://content.googleapis.com/tagmanager/v1/accounts

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:

https://http://www.googleapis.com/analytics/v3/management/accounts

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.

(0 rating, 0 votes)
You need to be a registered member to rate this.
Start the discussion Views Tweet it!
Do you want to know how to develop your skillset to become a Web Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. Building web apps with Node.js
2. HTML5 Programming Cookbook
3. CSS Programming Cookbook
4. AngularJS Programming Cookbook
5. jQuery Programming Cookbook
6. Bootstrap Programming Cookbook
and many more ....
I agree to the Terms and Privacy Policy

Leave a Reply

avatar

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  Subscribe  
Notify of