My favorites | Sign in
Google
             
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
// Copyright 2009 Google Inc. All Rights Reserved.

/* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import com.google.gdata.client.analytics.AnalyticsService;
import com.google.gdata.client.analytics.DataQuery;
import com.google.gdata.data.analytics.Aggregates;
import com.google.gdata.data.analytics.DataEntry;
import com.google.gdata.data.analytics.DataFeed;
import com.google.gdata.data.analytics.DataSource;
import com.google.gdata.data.analytics.Dimension;
import com.google.gdata.data.analytics.Metric;
import com.google.gdata.util.AuthenticationException;
import com.google.gdata.util.ServiceException;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

/**
* Sample program demonstrating how to make a data request to the GA Data Export
* using client login authentication as well as accessing important data in the feed.
*/
public class DataFeedExample {

private static final String CLIENT_USERNAME = "INSERT_LOGIN_EMAIL_HERE";
private static final String CLIENT_PASS = "INSERT_PASSWORD_HERE";
private static final String PROFILE_ID = "INSERT_PROFILE_ID_HERE";

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());
return;
}

outputFeedData(feed);
outputFeedDataSources(feed);
outputFeedAggregates(feed);
outputEntryRowData(feed);

String tableData = DataFeedExample.getFeedTable(feed);
System.out.println(tableData);
}

//------------------------------------------------------
// Format Feed Related Data
//------------------------------------------------------
/**
* Output the information specific to the feed.
* @param {DataFeed} feed Parameter passed
* back from the feed handler.
*/
public static void outputFeedData(DataFeed feed) {
System.out.println(
"\nFeed Title = " + feed.getTitle().getPlainText() +
"\nFeed ID = " + feed.getId() +
"\nTotal Results = " + feed.getTotalResults() +
"\nSart Index = " + feed.getStartIndex() +
"\nItems Per Page = " + feed.getItemsPerPage() +
"\nStart Date = " + feed.getStartDate().getValue() +
"\nEnd Date = " + feed.getEndDate().getValue());
}

/**
* Output information about the data sources in the feed.
* Note: the GA Export API currently has exactly one data source.
* @param {DataFeed} feed Parameter passed
* back from the feed handler.
*/
public static void outputFeedDataSources(DataFeed feed) {
DataSource gaDataSource = feed.getDataSources().get(0);
System.out.println(
"\nTable Name = " + gaDataSource.getTableName().getValue() +
"\nTable ID = " + gaDataSource.getTableId().getValue() +
"\nWeb Property Id = " + gaDataSource.getProperty("ga:webPropertyId") +
"\nProfile Id = " + gaDataSource.getProperty("ga:profileId") +
"\nAccount Name = " + gaDataSource.getProperty("ga:accountName"));
}

/**
* Output all the metric names and values of the aggregate data.
* The aggregate metrics represent values across all of the entries selected
* by the query and not just the rows returned.
* @param {DataFeed} feed Parameter passed
* back from the feed handler.
*/
public static void outputFeedAggregates(DataFeed feed) {
Aggregates aggregates = feed.getAggregates();
List<Metric> aggregateMetrics = aggregates.getMetrics();
for (Metric metric : aggregateMetrics) {
System.out.println(
"\nMetric Name = " + metric.getName() +
"\nMetric Value = " + metric.getValue() +
"\nMetric Type = " + metric.getType() +
"\nMetric CI = " + metric.getConfidenceInterval().toString());
}
}

/**
* Output all the important information from the first entry in the data feed.
* @param {DataFeed} feed Parameter passed
* back from the feed handler.
*/
public static void outputEntryRowData(DataFeed feed) {
List<DataEntry> entries = feed.getEntries();
if (entries.size() == 0) {
System.out.println("No entries found");
return;
}
DataEntry singleEntry = entries.get(0);

// properties specific to all the entries returned in the feed
System.out.println("Entry ID = " + singleEntry.getId());
System.out.println("Entry Title = " + singleEntry.getTitle().getPlainText());

// iterate through all the dimensions
List<Dimension> dimensions = singleEntry.getDimensions();
for (Dimension dimension : dimensions) {
System.out.println("Dimension Name = " + dimension.getName());
System.out.println("Dimension Value = " + dimension.getValue());
}

// iterate through all the metrics
List<Metric> metrics = singleEntry.getMetrics();
for (Metric metric : metrics) {
System.out.println("Metric Name = " + metric.getName());
System.out.println("Metric Value = " + metric.getValue());
System.out.println("Metric Type = " + metric.getType());
System.out.println("Metric CI = " + metric.getConfidenceInterval().toString());
}
}

/**
* Get the data feed values in the feed as a string.
* @param {DataFeed} feed Parameter passed
* back from the feed handler.
* @return {String} This returns the contents of the feed.
*/
public static String getFeedTable(DataFeed feed) {
List<DataEntry> entries = feed.getEntries();
if (entries.size() == 0) {
return "No entries found";
}
DataEntry singleEntry = entries.get(0);
List<Dimension> dimensions = singleEntry.getDimensions();
List<Metric> metrics = singleEntry.getMetrics();
List<String> feedDataNames = new ArrayList<String>();
String feedDataValues = "";

// put all the dimension and metric names into an array
for (Dimension dimension : dimensions) {
feedDataNames.add(dimension.getName());
}
for (Metric metric : metrics) {
feedDataNames.add(metric.getName());
}

// put the values of the dimension and metric names into the table
for (DataEntry entry : entries) {
for (String dataName : feedDataNames) {
feedDataValues += "\n" + dataName + "\t= " + entry.stringValueOf(dataName);
}
feedDataValues += "\n";
}
return feedDataValues;
}
}
Show details Hide details

Change log

r3 by api.nickm on Apr 16, 2009   Diff
Small update to DataFeedExample
Go to: 
Project members, sign in to write a code review

Older revisions

r2 by api.nickm on Apr 14, 2009   Diff
Initial release of the Analytics API
Java Sample Code.
All revisions of this file

File info

Size: 8602 bytes, 225 lines

File properties

svn:executable
*