/
GoogleAnalytics.java
93 lines (79 loc) · 3.36 KB
/
GoogleAnalytics.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/**
* Retrieve data from the Google Analytics API. May need some tweaking for your particular system.
* @author Kirk Hess
*/
public class GoogleAnalytics {
// Credentials for Client Login Authorization.
private static final String CLIENT_USERNAME = "xxx";
private static final String CLIENT_PASS = "xxxx";
private static final String[] eventDates; //define as needed
// Table ID constant
private static final String TABLE_ID = "ga:123456789";
public static void main(String args[]) {
try {
// Service Object to work with the Google Analytics Data Export API.
AnalyticsService analyticsService = new AnalyticsService("gaExportAPI_acctSample_v2.0");
// Client Login Authorization.
analyticsService.setUserCredentials(CLIENT_USERNAME, CLIENT_PASS);
// Get data from the Account Feed.
getAccountFeed(analyticsService);
// Access the Data Feed if the Table Id has been set.
if (!TABLE_ID.isEmpty()) {
// Get profile data from the Data Feed.
getDataFeed(analyticsService);
}
} catch (AuthenticationException e) {
System.err.println("Authentication failed : " + e.getMessage());
return;
} 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());
return;
}
}
/**
* @param {AnalyticsService} Google Analytics service object that
* is authorized through Client Login.
*/
private static void getAccountFeed(AnalyticsService analyticsService)
throws IOException, MalformedURLException, ServiceException {
// Construct query from a string.
URL queryUrl = new URL(
"https://www.google.com/analytics/feeds/accounts/default?max-results=50");
// Make request to the API.
AccountFeed accountFeed = analyticsService.getFeed(queryUrl, AccountFeed.class);
// Output the data to the screen.
System.out.println("-------- Account Feed Results --------");
for (AccountEntry entry : accountFeed.getEntries()) {
System.out.println(
"\nAccount Name = " + entry.getProperty("ga:accountName") +
"\nProfile Name = " + entry.getTitle().getPlainText() +
"\nProfile Id = " + entry.getProperty("ga:profileId") +
"\nTable Id = " + entry.getTableId().getValue());
}
}
/**
* @param {AnalyticsService} Google Analytics service object that
* is authorized through Client Login.
*/
private static void getDataFeed(AnalyticsService analyticsService, String[] eventDates)
throws IOException, MalformedURLException, ServiceException {
StringBuffer skippedRecords = new StringBuffer();
int skipCount = 0;
// Create a query using the DataQuery Object.
for(String eventDate : eventDates){
DataQuery query = new DataQuery(new URL(
"https://www.google.com/analytics/feeds/data"));
query.setStartDate(eventDate);
query.setEndDate(eventDate);
query.setDimensions("ga:eventlabel,ga:eventcategory,ga:pagePath");
query.setMetrics("ga:totalevents,ga:uniqueevents");
query.setSort("-ga:totalevents");
query.setMaxResults(500);
query.setIds(TABLE_ID);
// Make a request to the API.
DataFeed dataFeed = analyticsService.getFeed(query.getUrl(), DataFeed.class);
}
}