亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

java連接google analytics

系統(tǒng) 2253 0

?

?

?? ?google Analytics 是google的網(wǎng)站分析的工具,分析的很詳細(xì),google本身提供一套展示框架。



?當(dāng)然,我們可以自己制作客戶端去連接google的服務(wù)器,然后取得我們所需要的數(shù)據(jù),網(wǎng)上流傳有flex air版的google analytics客戶端,我下了一個,好像不能用,很多鏈接也打不開。

?看到google 提供analytics的java支持,本來想看看有沒flex 的。就玩了一下下。。。

?

主程序:

?

            /* Copyright (c) 2008 Google Inc.
 *
 * 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 util.SimpleCommandLineParser;
import com.google.gdata.data.analytics.AccountEntry;
import com.google.gdata.data.analytics.AccountFeed;
import com.google.gdata.data.analytics.DataEntry;
import com.google.gdata.data.analytics.DataFeed;
import com.google.gdata.util.ServiceException;

import java.io.IOException;
import java.net.URL;
import java.net.MalformedURLException;

/**
 * Demonstrates how to use the Google Data API's Java client library to access
 * Google Analytics data.
 *
 * 
 */
public class AnalyticsClient {

  private AnalyticsClient() {}

  public static final String ACCOUNTS_URL
      = "https://www.google.com/analytics/feeds/accounts/default";

  public static final String DATA_URL = "https://www.google.com/analytics/feeds/data";

  /**
   * Returns a data feed containing the accounts that the user logged in to the
   * given AnalyticsService has access to.
   *
   * @param myService The AnalyticsService to request accounts from
   * @return An AccountFeed containing an entry for each profile the logged-in
   *     user has access to
   * @throws IOException If an error occurs while trying to communicate with
   *     the Analytics server
   * @throws ServiceException If the API cannot fulfill the user request for
   *     any reason
   */
  public static AccountFeed getAvailableAccounts(AnalyticsService myService)
      throws IOException, ServiceException {

    URL feedUrl = new URL(ACCOUNTS_URL);
    return myService.getFeed(feedUrl, AccountFeed.class);
  }

  /**
   * Displays the accounts in the given account feed.
   */
  public static void printAccounts(AccountFeed accountFeed) {
    System.out.println(accountFeed.getTitle().getPlainText());
    for (AccountEntry entry : accountFeed.getEntries()) {
      System.out.println(
          "\t" + entry.getTitle().getPlainText() + ": "
          + entry.getTableId().getValue());
    }
    System.out.println();
  }

  /**
   * Gets a very basic data query request for the given table.
   *
   * @param tableId The ID of the table to request data from
   * @return A basic query for browser, visits, and bounce information from
   *     the given table
   * @throws MalformedURLException If the URL used to request data is malformed
   */
  public static DataQuery getBasicQuery(String tableId) throws MalformedURLException {
    // Set up the request (we could alternately construct a URL manually with all query parameters
    // set)
    DataQuery query = new DataQuery(new URL(DATA_URL));
    query.setIds(tableId);
    query.setStartDate("2010-01-01");
    query.setEndDate("2010-10-31");
    query.setDimensions("ga:browser");
    query.setMetrics("ga:visits,ga:bounces");

    return query;
  }

  /**
   * Prints the contents of a data feed.
   *
   * @param title A header to print before the results
   * @param dataFeed The data feed containing data to print. Assumed to contain
   *     ga:browser, ga:visits, and ga:bounces information.
   */
  public static void printData(String title, DataFeed dataFeed) {
    System.out.println(title);
    for (DataEntry entry : dataFeed.getEntries()) {
      System.out.println("\tBrowser: " + entry.stringValueOf("ga:browser"));
      System.out.println("\t\tVisits: " + entry.stringValueOf("ga:visits"));
      System.out.println("\t\tBounces: " + entry.stringValueOf("ga:bounces"));
      System.out.println("\t\tBounce rate: "
          + entry.longValueOf("ga:bounces") / (double) entry.longValueOf("ga:visits"));
    }
    System.out.println();
  }

  /**
   * Runs through all the examples using the given GoogleService instance.
   *
   * @param myService An unauthenticated AnalyticsService object
   * @throws ServiceException If the service is unable to handle the request
   * @throws IOException If there is an error communicating with the server
   */
  public static void run(AnalyticsService myService, String username, String password)
      throws ServiceException, IOException {

    // Authenticate using ClientLogin
    myService.setUserCredentials(username, password);

    // Print a list of all accessible accounts
    AccountFeed accountFeed = getAvailableAccounts(myService);
    printAccounts(accountFeed);

    if (accountFeed.getEntries().isEmpty()) {
      return;
    }

    // Each entry in the account feed represents an individual profile
    AccountEntry profile = accountFeed.getEntries().get(0);
    String tableId = profile.getTableId().getValue();

    // Print the results of a basic request
    DataQuery basicQuery = getBasicQuery(tableId);
    DataFeed basicData = myService.getFeed(basicQuery, DataFeed.class);
    printData("BASIC RESULTS", basicData);

    // Ask Analytics to return the data sorted in descending order of visits
    DataQuery sortedQuery = getBasicQuery(tableId);
    sortedQuery.setSort("-ga:visits");
    DataFeed sortedData = myService.getFeed(sortedQuery, DataFeed.class);
    printData("SORTED RESULTS", sortedData);

    // Ask Analytics to filter out browsers that contain the word "Explorer"
    DataQuery filteredQuery = getBasicQuery(tableId);
    filteredQuery.setFilters("ga:browser!@Explorer");
    DataFeed filteredData = myService.getFeed(filteredQuery, DataFeed.class);
    printData("FILTERED RESULTS", filteredData);
    
    // Ask Analytics to filter out browsers that contain the word "Explorer"
    DataQuery regionQuery = getBasicQuery(tableId);
    filteredQuery.setFilters("ga:region!@China");
    DataFeed regionData = myService.getFeed(regionQuery, DataFeed.class);
    printData("REGION RESULTS", regionData);
  }

  /**
   * Uses the command line arguments to authenticate the GoogleService and build
   * the basic feed URI, then invokes all the other methods to demonstrate how
   * to interface with the Analytics service.
   *
   * @param args See the usage method.
   */
  public static void main(String[] args) {
	  String[] str1=new String[2];
	  str1[0]="--user=qq123zhz";
	  str1[1]="--pwd=112343";
    // Set username, password and feed URI from command-line arguments.
    SimpleCommandLineParser parser = new SimpleCommandLineParser(str1);
    String userName = parser.getValue("username", "user", "u");
    String userPassword = parser.getValue("password", "pwd", "p");
    boolean help = parser.containsKey("help", "h");
    if (help || (userName == null)) {
      usage();
      System.exit(1);
    }

    AnalyticsService myService = new AnalyticsService("exampleCo-exampleApp-1");

    try {
      run(myService, userName, userPassword);
    } catch (ServiceException se) {
      se.printStackTrace();
    } catch (IOException ioe) {
      ioe.printStackTrace();
    }
  }

  /**
   * Prints the command line usage of this sample application.
   */
  private static void usage() {
    System.err.println("Usage: AnalyticsClient --username <username> --password <password>");
    System.err.println();
    System.err.println("Fetches and displays various pieces of "
        + "information from the Google Analytics "
        + "Data Export API.");
  }
}

          

?

??? ACCOUNTS_URL = "https://www.google.com/analytics/feeds/accounts/default";

?? DATA_URL = "https://www.google.com/analytics/feeds/data";
這兩個地址分別是賬號驗證和ga數(shù)據(jù)存儲的位置。
?
str1[0] = "--user=qq123zhz";
str1[1] = "--pwd=112343";
分別是用戶名和密碼,自己輸入自己的google analytics帳戶和密碼,以上的密碼被我改過了。

??query.setStartDate("2010-01-01");
?? ?query.setEndDate("2010-10-31");
?? ?query.setDimensions("ga:browser");
?? ?query.setMetrics("ga:visits,ga:bounces");
?設(shè)置查詢的一些參數(shù),起始結(jié)束時間,瀏覽器種類,查詢訪問量,還有一些其他的參數(shù)。。。

?


java連接google analytics


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 色综合色 | 国产高清狼人香蕉在线观看 | 亚洲综合精品成人 | 五月婷婷之综合激情 | 女18毛片| 亚洲欧美网址 | 国产l精品国产亚洲区在线观看 | 欧美国产片 | 男人影院在线观看 | 日本一区二区三区在线播放 | 欧美又粗又硬又大久久久 | 手机看片自拍日韩日韩高清 | 国产免费福利 | 中文字幕一区二区三区四区五区人 | 伊人国产在线观看 | 94久久国产乱子伦精品免费 | 真人一级毛片免费观看视频 | 久久99精品国产麻豆宅宅 | 久热re这里只有精品视频 | 老司机午夜精品视频 | 精品久久久在线观看 | 成人爽a毛片在线视频网站 成人爽视频 | 香蕉超级碰碰碰97视频蜜芽 | 午夜影院在线 | 国产99视频精品草莓免视看 | 亚洲精品三区 | 欧美色视频日本片免费高清 | 中文字幕一区久久久久 | 亚洲视频一区在线观看 | 97高清国语自产拍中国大陆 | 欧美一级爆毛片 | 亚洲精品国产一区二区图片欧美 | 国产精品久久久久久久免费大片 | 一级有奶水毛片免费看 | 亚洲欧美日韩在线中文一 | 免费中文字幕 | 欧美性天天影院欧美狂野 | 天天躁狠狠躁 | 久久国产精品麻豆映画 | 成人短视频网站 | 插久久|