How to use Google Analytics API v4 - Java - Part 2 - Java Application


Posted by khrishan on 2016-08-31

Before now, I haven't properly used Google Analytics and its full capability. Merely putting the JavaScript code into my HTML code was as far as I went.

With my new revamped website, I now have a desire to track where my website viewers are coming from, and how they are accessing my website.

The Google Documentation is extremely detailed, but spread across many sections and to those who aren't as code savy, it can come across as a daunting task trying to find out what information is useful or not. What also doesn't help is the Google Example documentation is wrong! (You’ll see in Part 2)…

So this is my documentation as to how I got the Google Analytics API to work for me:

I am using Java to access the Google Analytics API, there are many other ways of accessing the API (JS, PHP, Python) - I plan to do an JS/PHP version soon)

This section follows Part 1 of this two part series where I try to connect to the Google v4 API. If you haven't seen Part 1, you can see it here...

In this section, I will be going through how I made the Java code work for me. Although I have made the code available to all via GitHub (Click here), I would like to share with you what I did to make it do as I wished.

Contents

  1. Installing the Client Libraries
  2. HelloAnalytics.java Mistakes
  3. Modifications to the HelloAnalytics.java
  4. Making it do what I wanted
  5. References

Installing the Client Libraries

1. In the Google Documentation, its states :

Download the Analytics Reporting API V4 Java Client library, which is bundled as a ZIP file with all the required dependencies.’
What they don’t tell you is that the link provided points to the v3 of the API. So when you are doing this yourself, ensure that you find v4 of the Google Analytics API (or follow the link here).

2. Once you have downloaded the ZIP file and extracted all the files (and put them where you want to), we need to import these JAR files into your Eclipse project. In Eclipse, right click on the project; navigate to Build Path -> Configure Build Path.

3. This will load up a dialog box. From here, click on the ‘Add External JARs…

4. This will take your File Explorer. The JARs needed are all the JARs in the libs folder and google-api-services-analyticsreporting-v4-[version].jar (not the Javadoc or Source one)

5. Click OK -> Apply -> OK. Then the libraries will be added to the project.

HelloAnalytics.java Mistakes

Next, I copied and pasted the code from the example documentation (HelloAnalytics.java). I believe that this code hasn’t been modified since the v3 update (or there has been an update to the v4 API which makes this code redundant). But here are the changes I made to make the sample code work (Changes I have documented on GitHub also).

  • First I changed the import statements so they actually call the correct libraries from the JAR files.

  • Next, I changed the name of the class from ‘HelloAnalyticsReporting’ to ‘Hello Analytics’. #rookieMistake
  • Finally, wherever you have the term ‘Analyticsreporting’ – change this to ‘AnalyticsReporting’.

And there you have it! A class which Google ‘should’ have provided you with!

Modifications to HelloAnalytics.java

Now that I had a class that would compile, now it was time to make it connect to the API.

1. Underneath the class declaration, there are some ‘static/final’ variables. Here are the fields that I modified to connect my application to the API.

  • Application Name
    • Can put anything but I’d recommend putting the Product Name that you would have entered when creating the Service Account (just for consistency).
  • Key File Location
    • One thing I did was to put my .p12 certificate into the project folder. This makes it easier to reference when it comes to the Key File Location, where I put “client_secrets.p12”.
  • Service Account Email
    • Replace with the ‘email address’ that coincided with my service account.
  • View ID
    • Replaced with the view ID I obtained.

Making it do what I wanted

The example is set up so you receive all the hits from the last 7 days and splits this information based on browsers but there are endless possibilities as to what data you can pull and into what format.

The list of tags can be found from Google’s Documentation here

In my example, I wanted to pull all the total session data from the previous day.

To do this, I could get rid of the ‘Browser dimension’ in the code (specifically remove it from the getReport()) method.

Then I changed the date range and set it to the previous days date using the UTC format (YYYY-MM-DD).

Finally, it was about getting the results back in a format I wanted. Currently, the data is being returned in some form of ‘GetReportsResponse’ – which is essentially JSON (See screen shot below)

Google have thankfully provided built in methods to get this data. I get the data from the report, get the totals, get the values and get the first index.

 

And that sums up how I pull information from the Google Analytics API. If you have any more comments, views, tip and/or tricks that you'd like to share.. please feel free to tweet me (@khrishan94) or contact me using the form that is on the bottom of the homepage.