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.

OAUTH2.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.

SelectProject

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.

APIKeyCredentials

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.

Credentials

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.

ClientID.png

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

For more information on the implementation please check the codebase from GitHub

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

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