Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Working With Account APIs for Google Tag Manager and Google Analytics

DZone's Guide to

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.

· Integration Zone ·
Free Resource

SnapLogic is the leading self-service enterprise-grade integration platform. Download the 2018 GartnerMagic Quadrant for Enterprise iPaaS or play around on the platform, risk free, for 30 days.

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

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

SelectProject

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.

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 has the respective client credentials. Please check the screenshot for more reference.

ClientID.png

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.

Download A Buyer's Guide to Application and Data Integration, your one-stop-shop for research, checklists, and explanations for an application and data integration solution.

Topics:
google analytics api ,google tag manager ,integration ,api integration ,api security

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}