Hi,
I suffer for days with Google Analytics and my GWT application linking.
In my app, I need to have the recived data procces from Google Analytics
(e.g. number of visitors).
For this I used the gwt-gdata library.
Probably the examples and the library is old (I think)
My question, how I can
solve to take over the data?
I attach My code to see if I stabbed just something.
Thank you for the answer!
The code:
package com.masa.statmod.client;
import com.google.api.gwt.oauth2.client.Auth;
import com.google.api.gwt.oauth2.client.AuthRequest;
import com.google.appengine.tools.admin.OAuth2Native;
import com.google.gwt.core.client.Callback;
import com.google.gwt.accounts.client.AuthSubStatus;
import com.google.gwt.accounts.client.User;
import com.google.gwt.gdata.client.GData;
import com.google.gwt.gdata.client.GDataSystemPackage;
import com.google.gwt.gdata.client.analytics.AccountEntry;
import com.google.gwt.gdata.client.analytics.AccountFeed;
import com.google.gwt.gdata.client.analytics.AccountFeedCallback;
import com.google.gwt.gdata.client.analytics.AnalyticsService;
import com.google.gwt.gdata.client.analytics.DataEntry;
import com.google.gwt.gdata.client.analytics.DataFeed;
import com.google.gwt.gdata.client.analytics.DataFeedCallback;
import com.google.gwt.gdata.client.analytics.DataQuery;
import com.google.gwt.gdata.client.impl.CallErrorException;
import com.google.gwt.user.client.Window;
public class GaAuth {
// Use the implementation of Auth intended to be used in the GWT client
app.
private static final Auth AUTH = Auth.get();
private AnalyticsService service;
private static final String ANAL_SCOPE =
"https://www.googleapis.com/auth/analytics.readonly";
private static final String GOOGLE_AUTH_URL =
"https://accounts.google.com/o/oauth2/auth";
private static final String CLIENT_SECRET = "xx-xx-xx";
private static final String GOOGLE_CLIENT_ID =
"xxxx.apps.googleusercontent.com";
private static final String CLIENT_USERNAME = "[email protected]";
private static final String CLIENT_PASS = "xx";
private static final String TABLE_ID = "ga:xxxx";
private final String scope =
"https://www.googleapis.com/auth/analytics.readonly";
public final static String GDATA_API_KEY = "--";
void GaAuth() {
login();
if (!GData.isLoaded(GDataSystemPackage.ANALYTICS)) {
System.out.println("Loading the GData Analytics package...");
GData.loadGDataApi(GDATA_API_KEY, new Runnable() {
public void run() {
startLekerd();
}
}, GDataSystemPackage.ANALYTICS);
} else {
startLekerd();
}
}
/**
* Retrieve the Analytics accounts feed using the Analytics service and
the
* accounts feed uri. In GData all get, insert, update and delete
methods
* always receive a callback defining success and failure handlers.
Here,
* the failure handler displays an error message while the success
handler
* obtains the first Account entry and calls queryData to retrieve the
data
* feed for that account.
*
* @param accountsFeedUri
* The uri of the accounts feed
*/
private void getAccounts(String accountsFeedUri) {
System.out.println("Most kéne lekérdeznie az account
bejegyzéseit");//debug
try{
service.getAccountFeed(accountsFeedUri, new AccountFeedCallback() {
@Override
public void onSuccess(AccountFeed result) {
System.out.println("Sikerült lekérdezni az account
bejegyzéseit");//debug
AccountEntry[] entries = result.getEntries();
if (entries.length == 0) {
System.out.println("Nincs ga accod");//debug
} else {
System.out.println("Megy a lekérdezés");//debug
AccountEntry targetEntry = entries[0];
queryData(targetEntry.getTableId().getValue());
}
}
@Override
public void onFailure(CallErrorException caught) {
// TODO Auto-generated method stub
System.out.println("Nem sikerült elérni se a ga-t.");//debug
}
});}catch(Exception e){
System.out.println("Valami az urival lesz az
kurvahétszenség");//debug
}
}
/**
* Retrieves a data feed for an Analytics account using a Query object.
In
* GData, feed URIs can contain querystring parameters. The GData query
* objects aid in building parameterized feed URIs. Upon successfully
* receiving the data feed, the data entries are displayed to the user
via
* the showData method. Query parameters are specified for start and end
* dates, dimensions, metrics, sort field and direction and the IDs of
the
* account tables which should be queried.
*
* @param tableId
* The id of the account table for which to retrieve the
* Analytics data.
*/
private void queryData(String tableId) {
System.out.println("Adatok lekérése");//debug
DataQuery query = DataQuery
.newInstance("https://www.googleapis.com/auth/analytics.readonly");
query.setStartDate("2012-07-01");
query.setEndDate("2013-01-10");
query.setDimensions("ga:date");
query.setMetrics("ga:visits,ga:pageviews");
query.setSort("ga:date");
query.setIds(tableId);
try{
System.out.println("AZ adatok lekérése megtörtént");//debug
service.getDataFeed(query, new DataFeedCallback() {
@Override
public void onSuccess(DataFeed result) {
System.out.println("Ezek szerint a sikerült a
datafeed");//debug
// TODO Auto-generated method stub
result.getEntries();
DataEntry[] bejegyzesek = result.getEntries();
DataEntry entry = bejegyzesek[0];
System.out.println(entry.getStringValueOf("ga:visits"));
}
@Override
public void onFailure(CallErrorException caught) {
System.out.println("Sikertelen lekérdezés, de legalább
sikerült elérni a ga-t.");//debug
}
});
}catch(Exception e){
System.err.println("Itt hal el a kurva anyját ennek a
szarnak");//debug
}}
private void startLekerd() {
service = AnalyticsService
.newInstance("HelloGData_Analytics_YourAccountsDemo_v2.0");
try {
getAccounts("https://www.googleapis.com/auth/analytics.readonly");
} catch (Exception e) {
System.out.println("Mégse vagy bejelentkezve");//debug
}
}
private static void login() {
final AuthRequest req = new AuthRequest(GOOGLE_AUTH_URL,
GOOGLE_CLIENT_ID)
.withScopes(ANAL_SCOPE);
// Calling login() will display a popup to the user the first
time it is
// called. Once the user has granted access to the application,
// subsequent calls to login() will not display the popup, and
will
// immediately result in the callback being given the token to
use.
/*AUTH.login(req, new Callback<String, Throwable>() {
@Override
public void onSuccess(String token) {
Window.alert("Got an OAuth token:\n" + token + "\n"
+ "Token expires in " + AUTH.expiresIn(req) + " ms\n");
}
@Override
public void onFailure(Throwable caught) {
Window.alert("Error:\n" + caught.getMessage());
}
});
}
});
*/
AUTH.login(req,new Callback<String, Throwable>(){
@Override
public void onFailure(Throwable reason) {
// TODO Auto-generated method stub
Window.alert("Error:\n" + reason.getMessage());
}
@Override
public void onSuccess(String token) {
// TODO Auto-generated method stub
Window.alert("Got an OAuth token:\n" + token + "\n"
+ "Token expires in " + AUTH.expiresIn(req) + "
ms\n");
}}
);
}
}
--
You received this message because you are subscribed to the Google Groups
"Google Web Toolkit" group.
To view this discussion on the web visit
https://groups.google.com/d/msg/google-web-toolkit/-/PRhO5my6L_gJ.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/google-web-toolkit?hl=en.