Obtaining website analytics from google using java

-

A lot of the websites out there use google analytics to track what users are doing on their website. Google has a very extensive dashboard to find a lot of information about your users. For one of our customers we needed a custom dashboard with statistics coming from different systems. From google analytics we needed information about the number of page views, sessions and visitors. In this blog post I am going to describe the steps to take to obtain these statistics from google analytics API using the java client.

There are three steps that you need to do before you have the data:

  1. Configure google analytics to allow the java client to connect.
  2. Create a java project (we use spring boot) and configure it to connect.
  3. Create the query and obtain the statistics from the results.

Configure Google analytics

Of course you first have to have a site that uses google analytics, I use my personal blog that is sleeping. It still attracts some visitors, so let us use this. If you have such a website, go to the developers console.

https://console.developers.google.com

Create a new project (use drop down top left screen) and after that click the library button in the menu on the left.

Push the enable button. A notification should be visible to add credentials to connect to the API. Use this button or the button in the menu on the left to get to the credentials screen.

Create credentials for a service account key, the next screen should be visible. Add the project viewer role.

Finally chose the p12 key. Json is recommended, but for some reason I could not find a way to make it work with the json key. When everything is ready copy the email address, you will need it in the next steps. Format of the email address that I mean is this: analytics@website-statistics-144211.iam.gserviceaccount.com.

When creating the credential a p12 file is downloaded to your machine.

Now we are almost done. We now have credentials to connect to the google analytics API, but we have to explicitly give the email address access in the google analytics maintenance screens. So had over to that website.

https://analytics.google.com/analytics/web

First choose the project you want obtain the analytics for and note the web property id (format of UA-123456-1). In the screen go to the maintenance tab and push user management.

The result should be the following image in which we have entered the email address as created in the previous steps.

That is it, now we can move on to creating the java application.

Create the java project and configure to connect

First we create a new spring-boot project, but if you prefer a plain java project that is fine as well. I would at least use something like gradle or maven. I still prefer maven. I used the spring boot started project without additional libraries and I manually added the google libraries. The following two libraries work for me:



    com.google.api-client
    google-api-client
    1.22.0



    com.google.apis
    google-api-services-analytics
    v3-rev134-1.22.0

Now we need to create an instance of the class GoogleCredential, the following code block uses a the builder to create the instance.

GoogleCredential credential = new GoogleCredential.Builder()
        .setTransport(httpTransport)
        .setJsonFactory(jsonFactory)
        .setServiceAccountId(SERVICE_ACCOUNT_EMAIL)
        .setServiceAccountScopes(Collections.singleton(AnalyticsScopes.ANALYTICS))
        .setServiceAccountPrivateKeyFromP12File(new File(P12)) // notasecret
        .build();

The jsonFactory and httpTransport are created by the following lines:

JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();

The SERVICE_ACCOUNT_EMAIL is the one we created when creating new credentials and the one we added to the google analytics users. P12 is a constant representing the name of the file with the p12 key in there. The one you should not share with others.

Next step is to use the created credential and connect to the analytics API. To do that we create the Analytics object, again the with builder. This time we provide it with an application name, which does not seem to matter what value you give it.

Analytics analytics = new Analytics.Builder(httpTransport, jsonFactory, credential)
         .setApplicationName(APPLICATION_NAME)
         .build();

Now that we can connect, we need to be more specific what metrics we want for what project. I have multiple websites running with the same account. Therefore I have multiple web property id’s. I already showed you how to obtain the web property id in the format UA-123456-1, the account id in this case would be 123456. Using the Analytics object and these two parameters we can obtain a list of profiles. The following code block obtain the profiles and prints them to the screen.

Profiles profiles = analytics
        .management()
        .profiles()
        .list(ACCOUNT_ID, WEB_PROPERTY_ID).execute();
profiles.getItems().forEach(profile ->
        System.out.println(profile.getId() + " " + profile.getName()));

Finally we can use this profile id together with the analytics object to print analytics data for a certain period.

System.out.println("July");
printMonth(analytics, "2016-07-01", "2016-07-31");
private void printMonth(Analytics analytics, String startDate, String endDate) throws IOException {
    GaData period = analytics.data().ga()
            .get(PROFILE_ID, startDate, endDate, "ga:pageviews,ga:sessions,ga:visitors")
            .execute();
    period.getTotalsForAllResults().entrySet().forEach(entry -> {
        System.out.println(entry.getKey() + " " + entry.getValue());
    });
}

The output for my site would than be:

July
ga:pageviews 7138
ga:sessions 6377
ga:visitors 5494
August
ga:pageviews 7550
ga:sessions 6590
ga:visitors 5665
September
ga:pageviews 5009
ga:sessions 4332
ga:visitors 3774

Hope that this can help others to connect to google as well. There is some documentation out there, but it is hard to find and often outdated. Even the samples that come with the library did not work out of the box for me.