Monday 14 April 2014

Getting up and running with Google Analytics Core Reporting API 3.0

Update: Google have recently improved their documentation so it should be straight-forward to get set up:  Core Reporting API - Developer Guide


Back in late 2013 I struggled to get anything to work with Google's Java client code for version 3 of their analytics API.

The sample code and documentation seemed to be somewhat lacking compared to what was then available for version 2.

A week or so ago I decided that it might be worthwhile to have another crack at making something work.  After much trial and error I now have some code which will successfully send requests to and receive responses from Google's Analytics service.

Before you get too excited, I'll just add the caveat that this particular setup involves the application triggering a browser to open so that a Google account associated with the Analytics account can be prompted to authorise the access to the data.

If this code isn't useful to you, then so be it but I may find myself Googling for this information in the future so here goes.

Dependencies
- at least one includes the dreaded alpha in the version, so expect it to require updating in the future:
  • com.google.apis:google-api-services-analytics:v3-rev83-1.17.0-rc
  • com.google.api-client:google-api-client-jackson2:1.17.0-rc
  • com.google.api.client:google-api-client-auth-oauth2:1.2.3-alpha
  • com.google.oauth-client:google-oauth-client-java7:1.16.0-rc
  • com.google.oauth-client:google-oauth-client-jetty:1.17.0-rc
Sample code for authentication
Specify your own CLIENT_ID, CLIENT_SECRET and APPLICATION_NAME values.

        HttpTransport transport = new NetHttpTransport();

        JsonFactory jsonFactory = new JacksonFactory();

        GoogleClientSecrets secrets = new GoogleClientSecrets().set(
                "client_id", CLIENT_ID).set("client_secret", CLIENT_SECRET);

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

        DataStoreFactory dataStoreFactory = new MemoryDataStoreFactory();

        GoogleAuthorizationCodeFlow.Builder builder = 
                new GoogleAuthorizationCodeFlow.Builder(httpTransport, jsonFactory, 
                        CLIENT_ID,
                        CLIENT_SECRET,
                        Collections.singleton(AnalyticsScopes.ANALYTICS_READONLY));

        GoogleAuthorizationCodeFlow flow = builder.setDataStoreFactory(dataStoreFactory).build();

        final Credential credential = new AuthorizationCodeInstalledApp(flow, 
                new LocalServerReceiver()).authorize("user");

        Analytics analytics = new Analytics.Builder(transport, jsonFactory, credential)
                .setApplicationName(APPLICATION_NAME).setHttpRequestInitializer(
                        new HttpRequestInitializer() {
                    @Override
                    public void initialize(HttpRequest httpRequest) throws IOException {
                        credential.initialize(httpRequest);
                        httpRequest.setConnectTimeout(30000);
                        httpRequest.setReadTimeout(60000);
                    }
                }).build();

Sample code for request on API
Specify your own profileId value.

        Analytics.Data data = analytics.data();

        Analytics.Data.Ga ga = data.ga();

        // External search
        Analytics.Data.Ga.Get get = ga.get("ga:" + profileId,
                "2013-01-01",              // Start date.
                "2013-12-31",              // End date.
                "ga:pageviews")  // Metrics.
                .setDimensions("ga:keyword").setSort("-ga:pageviews");

        GaData results = get.execute();

Do what you like with the results object - probably some iterating :)


What next
Because my intended use of the API will ultimately involve some server side application, I will need to come up with a different approach to authentication.

Let me know if you found this post useful.

No comments:

Post a Comment