+ "\nProfile Id = " + entry.getProperty("ga:profileId") //配置文件编号
+ "\nTable Id = " + entry.getTableId().getValue()); //配置文件的Table Id
}
}
/**
* 获取指标和维度信息
* @param analyticsService
* @throws IOException
* @throws MalformedURLException
* @throws ServiceException
*/
private static void getDataFeed(AnalyticsService analyticsService)
throws IOException, MalformedURLException, ServiceException {
// Create a query using the DataQuery Object.
DataQuery query = new DataQuery(new URL("https://www.google.com/analytics/feeds/data"));
query.setStartDate("2011-10-01"); //要统计的数据的起始时间
query.setEndDate("2011-10-30"); //要统计的数据的结束时间
query.setDimensions("ga:pageTitle,ga:pagePath"); //要统计的维度信息
query.setMetrics("ga:pageviews,ga:bounces,ga:visits,ga:visitors"); //要统计的指标信息
query.setSort("-ga:pageviews");
query.setMaxResults(10);
query.setIds(TABLE_ID);
// Make a request to the API.
DataFeed dataFeed = analyticsService.getFeed(query.getUrl(),
DataFeed.class);
// Output data to the screen.
System.out.println("----------- Data Feed Results ----------");
for (DataEntry entry : dataFeed.getEntries()) {
System.out.println("\nPage Title = "
+ entry.stringValueOf("ga:pageTitle") + "\nPage Path = "
+ entry.stringValueOf("ga:pagePath") + "\nPageviews浏览量 = "
+ entry.stringValueOf("ga:pageviews") + "\nga:bounces = "
+ entry.stringValueOf("ga:bounces") + "\nga:visits访问次数 = "
+ entry.stringValueOf("ga:visits") + "\nga:visitors访问人数 = "
+ entry.stringValueOf("ga:visitors"));
}
}
}
最后,使用任意方式(main()或servlet)调用这个class的myTest()方法即可。
注意:
Table ID的数字前加上“ga:”,例如ga:47778978
要取得的维度和指标信息需要在query.setDimensions()和query.setMetrics()中设定一下,见上面例子。