Integrate Google Analytics to IBM Integration Bus - Perficient Blogs
Blog
  • Topics
  • Industries
  • Partners

Explore

Topics

Industries

Partners

Integrate Google Analytics to IBM Integration Bus

Google Analytics is web analytics tool which has advanced modes of capturing statistical data of applications. While IIB dominates the middleware world with its robust integration capabilities, this blog provides an approach to utilize the REST services of the Google Analytics API to consume various reports containing various metrics of the native applications. The metrics can be further used to classify the areas of the application which had heavy traffic and pages which had poor traffic.

The following are the broad stages involved in integrating Google Analytics API to IIB with assumption that the user has a Google Analytics Account.

  • Create Service Account.
  • Create OAuth 2.0 Client Credentials.
  • Create a JSON Web Token (JWT).
  • Invoke OAuth 2.0 REST API using JWT.
  • Invoke Analytics REST API using access token.
  • Test using data from Query Explorer.

Create Service Account & OAuth 2.0 Client Credentials

Once the Google Analytics Account is created, login to the following link using the same credentials (https://console.developers.google.com/iam-admin/projects). After creating a project, click on the Service accounts link and furnish the below details as shown in the picture.

Ensure you check both the check boxes as shown so that we generate the P12 private key used to get the access token which will be elaborated later and also enables the clients to access the analytics data. Save the Personal Information Exchange (P12) file to a location (e.g. SampleDemoPrivateKey.p12). Note the password (usually “notasecret”) to be used later. Select the created service account and provide the required Permission to the Service account id as shown.

Since we have enabled the domain wide delegation for the Service Account, an OAuth client for service account is created which can be verified in the following link (https://console.developers.google.com/apis/credentials). If we do not find the OAuth client we can, we can create one by selecting Create Credential à Service Account Key à Choose the Service Account and download the P12 file. Ensure the Client is listed under OAuth 2.0 Client IDs sections.

Create a JWT & invoke Google APIs

Upon obtaining the client ID and private key from the API Console, your application needs to complete the following steps for the entire process of getting authenticated and invoking Analytics API getting automated without any manual or browser intervention,

a) Create a JSON Web Token (JWT) which includes a header, a claim set and a signature. The JWT contains three parts to it as stated above and all 3 parts of the JWT are base 64 encoded URL. The algorithm used for creating JWT, supported by OAuth is RSA SHA-256. Further details on the fields of the JSON are described in the following link (https://developers.google.com/identity/protocols/OAuth2ServiceAccount#creatingjwt). Jjwt-0.6 version is a suggested API for creating the JWT for transmission.

b) The properties that are required for creating the JWT can be stored in the property file. The JWT created can be validated against an online tool that re-converts the JWT into decoded string of header, claim and signature. (https://jwt.io/)

c)After generating the JWT, the https://www.googleapis.com/oauth2/v4/token URL is invoked by passing the parameters as “POST parameters” as shown in the below snippet.

Set OutputRoot.HTTPRequestHeader.”Content-Type” = ‘application/x-www-form-urlencoded’;

SET OutputRoot.BLOB.BLOB = CAST(‘grant_type=urn:ietf:params:oauth:grant-type:jwt-bearer&assertion=’ || JWT AS BLOB CCSID 1208);

d) The response from the Google API for OAuth is a GZIP compressed response which could throw exceptions while parsing the response unless the below settings are set in the HTTPRequest node.

e) The response contains the access token which is valid for an hour and which has to be used to make subsequent calls to the Google Analytics API by setting the access token as a HTTP Header as shown below.

f) Invoke the Google Analytics API by copying the message back to our request which was saved in an environment variable during the start of the flow. The endpoint to access the Google Analytics API is https://analyticsreporting.googleapis.com/v4/reports:batchGet

Leave a Reply

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

Subscribe to the Weekly Blog Digest:

Sign Up