0

嗨,我受 Google Analytics 和我的 GWT 应用程序链接的困扰好几天了。‎ 在我的应用程序中,我需要从 Google Analytics 接收数据处理(例如访问者数量)。‎ 可能示例和库很旧(我认为)

我的问题,我如何解决接管数据?我附上我的代码,看看我是否刺伤了什么。谢谢你的回答!

private static void getAccounts(String accountsFeedUri) {
    System.out.println("Most fogja az acc bejegyzeseit lekerdezni");// debug

    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();
            System.out.println("Megy a lekérdezés");// debug
            AccountEntry targetEntry = entries[0];
            queryData(targetEntry.getTableId().getValue());
            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
            // caught.printStackTrace();
            System.out.println(caught.getError());
            System.out
                    .println("Nem sikerült lekérdezni az account bejegyzéseit.");// 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 static 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("Elhalt");// debug

    }
}

private static void startLekerd() {
    service = AnalyticsService.newInstance("statmodv1");

    try {
        getAccounts(ANAL_SCOPE);// google auth

    } catch (Exception e) {
        System.out.println("Mégse vagy bejelentkezve");// debug

    }

}



private static void login2() { // Use the implementation of Auth intended *
                                // to be used in the GWT client // app.
    final Auth AUTH = Auth.get();

    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 onFailure(Throwable reason) { // TODO Auto-generated
                                                    // method stub
            Window.alert("Error:\n" + reason.getMessage());
        }

        @Override
        public void onSuccess(String token) {
            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);
            }
            startLekerd();
        }

    }

    );
}
4

0 回答 0