public static void main(String[] args) {
//------------------------------------------------------
// Configure GA API
//------------------------------------------------------
AnalyticsService as = new AnalyticsService("gaExportAPI_acctSample_v1.0");
String baseUrl = "https://www.google.com/analytics/feeds/data";
DataQuery query;
//------------------------------------------------------
// Client Login Authentication
//------------------------------------------------------
try {
as.setUserCredentials(CLIENT_USERNAME, CLIENT_PASS);
} catch (AuthenticationException e) {
System.err.println("Error : " + e.getMessage());
return;
}
//------------------------------------------------------
// GA Data Feed
//------------------------------------------------------
// first build the query
try {
query = new DataQuery(new URL(baseUrl));
} catch (MalformedURLException e) {
System.err.println("Malformed URL: " + baseUrl);
return;
}
query.setIds("ga:" + PROFILE_ID);
query.setDimensions("ga:source,ga:medium");
query.setMetrics("ga:visits,ga:bounces");
query.setSort("-ga:visits");
query.setFilters("ga:medium==referral");
query.setMaxResults(100);
query.setStartDate("2008-10-01");
query.setEndDate("2008-10-31");
URL url = query.getUrl();
System.out.println("URL: " + url.toString());
// Send our request to the Analytics API and wait for the results to come back
DataFeed feed;
try {
feed = as.getFeed(url, DataFeed.class);
} catch (IOException e) {
System.err.println("Network error trying to retrieve feed: " + e.getMessage());
return;
} catch (ServiceException e) {
System.err.println("Analytics API responded with an error message: " + e.getMessage());