Working With Account APIs for Google Tag Manager and Google Analytics
Working With Account APIs for Google Tag Manager and Google Analytics
In this post, we look to give an overview of the process of integrating the GA and GTM management accounts API into your application.
Join the DZone community and get the full member experience.
Join For FreeHow to Transform Your Business in the Digital Age: Learn how organizations are re-architecting their integration strategy with data-driven app integration for true digital transformation.
Agenda
The agenda of this post is to give a brief of the working process for the 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 will do the job for us. The script will drive the process of getting access tokens from the authorization server and, based on the access token, will hit the requested API to get the corresponding data which will be filtered to get the respective data out.
OAuth2.0
Google's APIs extensively use OAuth2.0 to validate any user trying to access any of their APIs. Please follow the document An Introduction to OAuth2.0 to understand the process for working with OAuth2.0
Now, once you have an understanding of how to handle OAuth requests, please follow the respective steps to understand the working process of the application.
Requirements
- Python should be installed on your machine.
- cURL – it is a command line tool which can be used to make POST or GET requests 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 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 has the respective client credentials. Please check the screenshot for more reference.
The downloaded JSON file will contain the following parameters:
- client_id
- project_id
- auth_uri
- token_uri
- auth_provider_x509_cert_url
- client_secret
- redirect_uris
Now, we just need the parameters client_id, client_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_id, redirect_uri) from the JSON file which you have downloaded and the scope from the API documentation for the respective Google GA or GTM API.
Step 5
The next step is to get the "refresh_token" because we will need this to generate the "access_token" every hour because the access token expires after an hour. In here, we will 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.
Step 6
Once you have the "refresh_token" you need the refresh token to generate the access token which will give access to the application in order to hit the required API 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 will get the refresh_token as the output from the previous POST request.
So, once you have the respective access token, you can access any of the required GTM or GA APIs 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 after every hour. So, you will have to implement it in such a way that the implementation code will request the refresh_token every hour and will replace the access_token object.
Request GTM Accounts API to Get Accounts Data
To request the GTM accounts API, you will have to send a GET request to the corresponding API:
https://content.googleapis.com/tagmanager/v1/accounts
If you want to read more about the API, here's the documentation.
Request a GA Accounts API to Get Accounts Data
To request the GA Analytics Accounts API, you will 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 on GitHub.
Build and deploy API integrations 7x faster. Try the Cloud Elements 100% RESTful platform for 30 days free. Access your trial here.
Opinions expressed by DZone contributors are their own.
{{ parent.title || parent.header.title}}
{{ parent.tldr }}
{{ parent.linkDescription }}
{{ parent.urlSource.name }}