Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Analytics authorization in java

I'm looking for the simplest way of programmatically logging in into Analytics and get data. Google documentation writes and gives examples for Oauth 2.0 which involves a user manually logging into with his google account, and then being redirected to my site with authorization. But this is not what I want to achieve - I'm building an automatic tool that needs to have user/pass or any other authorization key to be hard-coded and then log in without any user involvement (this is a periodic reporting tool).

I already found something about API KEY, but I can't find any example how to do that, or how to to that with Google java libraries.

I would be very grateful for pointing me into right direction. Also this may be valuable clue for others how to do it the simplest way - and I think logging should be simple.

like image 799
Marcin Avatar asked Jan 31 '12 18:01

Marcin


2 Answers

I had the same problem and I took me about 1h to find this in the v3 documentation:

Service Accounts

Useful for automated/offline/scheduled access to Google Analytics data for your own account. For example, to build a live dashboard of your own Google Analytics data and share it with other users.

There are a few steps you need to follow to configure service accounts to work with Google Analytics:

  1. Register a project in the APIs Console.
  2. In the Google APIs Console, under the API Access pane, create a client ID with the Application Type set to Service Account.
  3. Sign-in to Google Analytics and navigate to the Admin section.
  4. Select the account for which you want the application to have access to.
  5. Add the email address, from the Client ID created in the APIs Console from step #2, as a user of the selected Google Analytics account.
  6. Follow the instructions for Service Accounts to access Google Analytics data.

Read more here: https://developers.google.com/analytics/devguides/reporting/core/v3/gdataAuthorization

Puh, I guess the API is so big Google is having trouble documenting it an easy way =)

Then I looked at the code in the plus-serviceaccount-cmdline-sample and analytics-cmdline-sample. This is a very basic version implemented in a Playframework2 java app that prints to System.out as the examples above:

private static final HttpTransport HTTP_TRANSPORT = new NetHttpTransport();
private static final JsonFactory JSON_FACTORY = new JacksonFactory();

public static Result index() {
   GoogleCredential credential = null;
   try {
        credential = new GoogleCredential.Builder().setTransport(HTTP_TRANSPORT)
              .setJsonFactory(JSON_FACTORY)
              .setServiceAccountId("[email protected]")
              .setServiceAccountScopes(Arrays.asList(AnalyticsScopes.ANALYTICS_READONLY))
              .setServiceAccountPrivateKeyFromP12File(new File("/your/path/to/privatekey/privatekey.p12"))                          
              .build();
     } catch (GeneralSecurityException e) {
         e.printStackTrace();
     } catch (IOException e) {
         e.printStackTrace();  
     }

     // Set up and return Google Analytics API client.
     Analytics analytics = new Analytics.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential).setApplicationName(
          "Google-Analytics-Hello-Analytics-API-Sample").build();

     String profileId = "";
     try {
         profileId = getFirstProfileId(analytics);
     } catch (IOException e) {
        e.printStackTrace(); 
     }

     GaData gaData = null;
     try {
        gaData = executeDataQuery(analytics, profileId);
     } catch (IOException e) {
        e.printStackTrace();
     }
     printGaData(gaData);

     return ok(index.render("Your new application is ready."));
}

private static String getFirstProfileId(Analytics analytics) throws IOException {
    String profileId = null;

    // Query accounts collection.
    Accounts accounts = analytics.management().accounts().list().execute();

    if (accounts.getItems().isEmpty()) {
        System.err.println("No accounts found");
    } else {
        String firstAccountId = accounts.getItems().get(0).getId();

        // Query webproperties collection.
        Webproperties webproperties =
                analytics.management().webproperties().list(firstAccountId).execute();

        if (webproperties.getItems().isEmpty()) {
            System.err.println("No Webproperties found");
        } else {
            String firstWebpropertyId = webproperties.getItems().get(0).getId();

            // Query profiles collection.
            Profiles profiles =
                    analytics.management().profiles().list(firstAccountId, firstWebpropertyId).execute();

            if (profiles.getItems().isEmpty()) {
                System.err.println("No profiles found");
            } else {
                profileId = profiles.getItems().get(0).getId();
            }
        }
    }
    return profileId;
}

/**
 * Returns the top 25 organic search keywords and traffic source by visits. The Core Reporting API
 * is used to retrieve this data.
 *
 * @param analytics the analytics service object used to access the API.
 * @param profileId the profile ID from which to retrieve data.
 * @return the response from the API.
 * @throws IOException tf an API error occured.
 */
private static GaData executeDataQuery(Analytics analytics, String profileId) throws IOException {
    return analytics.data().ga().get("ga:" + profileId, // Table Id. ga: + profile id.
            "2012-01-01", // Start date.
            "2012-01-14", // End date.
            "ga:visits") // Metrics.
            .setDimensions("ga:source,ga:keyword")
            .setSort("-ga:visits,ga:source")
            .setFilters("ga:medium==organic")
            .setMaxResults(25)
            .execute();
}

/**
 * Prints the output from the Core Reporting API. The profile name is printed along with each
 * column name and all the data in the rows.
 *
 * @param results data returned from the Core Reporting API.
 */
private static void printGaData(GaData results) {
    System.out.println("printing results for profile: " + results.getProfileInfo().getProfileName());

    if (results.getRows() == null || results.getRows().isEmpty()) {
        System.out.println("No results Found.");
    } else {

        // Print column headers.
        for (GaData.ColumnHeaders header : results.getColumnHeaders()) {
            System.out.printf("%30s", header.getName());
        }
        System.out.println();

        // Print actual data.
        for (List<String> row : results.getRows()) {
            for (String column : row) {
                System.out.printf("%30s", column);
            }
            System.out.println();
        }

        System.out.println();
    }
}

Hope this helps!

like image 122
jakob Avatar answered Sep 30 '22 05:09

jakob


I solved it finally with the 2.4 version of Core Reporting - there's autorization with your gmail user/pass, just as simple as it should be - I wonder why there's no example how to do this in new 3.0 version.

Core reporting 2.4: http://code.google.com/intl/pl-PL/apis/analytics/docs/gdata/v2/gdataJava.html

like image 24
Marcin Avatar answered Sep 30 '22 05:09

Marcin