Analytics API Requests

Posted on by in Analytics

To make Google Data API requests you can use the App Engine in Java. It will assist you to create an application through a combined action of Analytics, Accounts, and App Engine to let the application be successfully shared across the internet.

To get access in Google Analytics data, one has to login (also known as authorization). Like this way, the applications acting on behalf of the user, also needs to be authorized to get access to those data. Google accounts are basically linked to different Google services. So, it will be a very dire experience if the applications ask for authorization (username and pass code) every time the user tries to log in. In this context, the best possible solution might be using an authorization token for any particular user. OAuth and AuthSub are two alternatives that are generally used to execute this indirect authorization request to Google Analytics API.

The MVC pattern

The app uses the MVC or Model, View, Controller design to separate the object, display and logical data.

The object data i.e. GoogleData, is referred as Model and includes the token and login information of the user. This part of the pattern is also in charge to hold all the application data.

‘View’ consists of all the data presented before the end user. For example, login.jsp, authorization.jsp, results.jsp are ‘View’ objects which convert the GoogleData objects into HTML as their key function.

‘Controller’ holds the set of rules and logics important to retrieve data. MainServlet is one of the two controllers used to set maximum number of data into the GoogleData and transmits these GoogleData to the ‘View’ appropriate for the user to carry on the procedure. AuthorizationServlet is the other of the two controllers and is used to manage and set rules to get tokens and controls the overall authorization process to manage the Google Analytics API.

 

 

 

 

Collectively, this Model View Controller approach takes apart three basic kinds of data (object, display, logic) and turns the application and the process into a flexible one.

Retrieve data from the Google Analytics API

To retrieve the essential data from a Google Analytics API, the application has to go through some steps handled by the GoogleDataManager:

1. Acquire user ID

The application must have the user ID and thus the user must login first. The GoogleData object holds a URL to login or logout and transmits to the login.jsp as a HTML upon request of the user.

2. Retrieve & store token

The MainServlet captures the ID and retrieve the authorization token, for the purpose of further use, from the Datastore. If the application doesn’t find any token in the Datastore, the user is forwarded to Authorization.jsp. As the next step, the Authorization.jsp makes a link available for the user from where he/she can get the token. There are different URL’s for both AuthSub and OAuth authorization methods and each of these are supported by the application. Now, what is the method you use, doesn’t matter, you can grant access for the application to the Google Analytics data as soon as you are on the Google authorization page. After the access is granted, one will be redirected to the AuthorizationServlet along with a token. The token is converted to a long lived token and then stored within the Datastore next to the user ID to use against different Google Analytics API requests. After storing the token, the application control is redirected to the MainServlet.

3. Make requests

Now, as the long-lived token is available the application can retrieve data from the Google Analytics API upon authorized requests. The system makes sure that the token is valid and not revoked. The status code of the Analytics as well as Management API Query can be used to determine the validity of a long-lived token.

Share

Let us know what you think about this article!