2011-08-11

Google Analytics API

To retrieve the report data:
DataQuery q = new DataQuery(new URL("https://www.google.com/analytics/feeds/data"));
q.setMaxResults(8192);
q.setStartDate("2007-01-01");
q.setEndDate("2012-12-31");
q.setDimensions("ga:pageTitle,ga:pagePath");
q.setMetrics("ga:pageviews,ga:entrances,ga:avgtimeonpage");
q.setSort("-ga:pageviews");
q.setIds("ga:" + profileId);
DataFeed dataFeed = client.getFeed(q, DataFeed.class);
for (DataEntry entry : dataFeed.getEntries()) {
  // Do something with entry
}


The profileId could be listed by:
ManagementFeed f = client.getFeed(new URL("https://www.google.com/analytics/feeds/datasources/ga/accounts/~all/webproperties/~all/profiles"), ManagementFeed.class);
for (ManagementEntry entry : f.getEntries()) {
  System.out.println(entry.getProperty("ga:profileId"));
}


A list of Dimensions and Metrics could be found at http://code.google.com/apis/analytics/docs/gdata/gdataReferenceDimensionsMetrics.html.

No comments: