Search code examples
javagoogle-analyticsgoogle-analytics-api

Google Anlytics in Java: AnalyticsService "application name"


Java newbie here. I'm trying to implement a simple Java application that collects data from GoogleAnalytics. Supposing my package name is "gDataExample", my username for accessing GA is "[email protected]", my password is "password", and my table id is "ga:my_table_id" my code until now is the following.

public class AnalyticsMain {

  // Credentials for Client Login Authorization.
  private static final String CLIENT_USERNAME = "[email protected]";
  private static final String CLIENT_PASS = "password";

  // Table ID constant
  private static final String TABLE_ID = "ga:my_table_id";

  public static void main(String args[]) {
    try {
      // Service Object to work with the Google Analytics Data Export API.
      AnalyticsService analyticsService = new AnalyticsService("gDataExample");

      // Client Login Authorization.
      analyticsService.setUserCredentials(CLIENT_USERNAME, CLIENT_PASS);

      // Get data from the Account Feed.
     // getAccountFeed(analyticsService);

    } catch (AuthenticationException e) {
      System.err.println("Authentication failed : " + e.getMessage());
      return;
    }
}

However when I run it I get the following error:

Exception in thread "main" java.lang.NoSuchMethodError: com.google.common.collect.ImmutableSet.copyOf([Ljava/lang/Object;)Lcom/google/common/collect/ImmutableSet;
    at com.google.gdata.wireformats.AltFormat$Builder.setAcceptableTypes(AltFormat.java:399)
    at com.google.gdata.wireformats.AltFormat$Builder.setAcceptableXmlTypes(AltFormat.java:387)
    at com.google.gdata.wireformats.AltFormat.<clinit>(AltFormat.java:49)
    at com.google.gdata.client.Service.<clinit>(Service.java:558)
    at AnalyticsMain.main(AnalyticsMain.java:34)

Where line 34 is

AnalyticsService analyticsService = new AnalyticsService("gDataExample");

So here's my question: what is the "name of my application" that has to be given as a parameter to 'new AnalyticsService()' ?


Solution

  • As far as I know you cant use client login (setUserCredentials) with Google Analytics. You need / should use Oauth2 to connect to the Google analytics API

    Google Oauth2

    Hello Analtyics java Tutorial