com.google.gdata.util.ServiceException {
AnalyticsService analyticsService = new AnalyticsService(CLIENT_NAME);
analyticsService.setUserCredentials(USERNAME, PASSWORD);
DataQuery dataQuery = new DataQuery(new URL("https://www.google.com/analytics/feeds/data"));
dataQuery.setIds(PROFILE_ID);
dataQuery.setStartDate(GA_START_DATE);
dataQuery.setEndDate(GA_END_DATE);
// Dimensions: Boundaries for the data being requested.
dataQuery.setDimensions("ga:year,ga:month,ga:adwordsCampaignID," +
"ga:adwordsAdGroupID,ga:adwordsCriteriaID");
// Metrics: The data being requested
dataQuery.setMetrics("ga:visits,ga:entrances,ga:bounces,ga:transactions," +
"ga:transactionRevenue,ga:goalCompletionsAll,ga:goalValueAll");
// A descending sort (highest values first) can be requested by placing a negative symbol in
// front of the metric to sort by.
dataQuery.setSort("-ga:transactionRevenue,-ga:entrances");
// This example is specifically comparing Analytics data to keywords set up in AdWords.
// AdWords returns clicks achieved via Content Targetting (think adsense on other websites)
// as having a Criteria ID of 3000000. While important, these aren't relevant to this example.
// Thus, filter them out.
dataQuery.setFilters("ga:adwordsCriteriaID!=3000000");
dataQuery.setMaxResults(MAX_RESULTS);
return analyticsService.getFeed(dataQuery.getUrl(), DataFeed.class);
}