Google Analytics(分析) Data Export

    技术2022-06-08  40

    import com.google.gdata.client.analytics.AnalyticsService;import com.google.gdata.client.analytics.DataQuery;import sample.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.AuthenticationException;import com.google.gdata.util.ServiceException;

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

    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";

     

      private static void myTest() throws Exception{     AnalyticsService myService = new AnalyticsService("exampleCo-exampleApp-1");     myService.setUserCredentials("google 邮箱名称", "邮箱密码");     URL feedUrl = new URL(ACCOUNTS_URL);     AccountFeed accountFeed = myService.getFeed(feedUrl, AccountFeed.class);     for (AccountEntry entry : accountFeed.getEntries()) {        String tableId = entry.getTableId().getValue();        System.out.println("/t" + entry.getTitle().getPlainText() + ": " + entry.getTableId().getValue());        DataQuery query = new DataQuery(new URL(DATA_URL));        query.setIds(tableId);        query.setStartDate("2011-04-30");        query.setEndDate("2011-05-30");        //query.setDimensions("ga:browser");        query.setMetrics("ga:visits,ga:pageviews");        DataFeed dataFeed = myService.getFeed(query, DataFeed.class);        for (DataEntry dentry : dataFeed.getEntries()) {          // System.out.println("/tBrowser: " + dentry.stringValueOf("ga:browser"));            System.out.println("/t/tVisits: " + dentry.stringValueOf("ga:visits"));            System.out.println("/t/tPageviews: " + dentry.stringValueOf("ga:pageviews"));         //System.out.println("/t/tBounce rate: "  + dentry.longValueOf("ga:bounces") / (double) dentry.longValueOf  ("ga:visits"));        }     }    }

     

     public static void main(String[] args) {     AnalyticsService myService = new AnalyticsService("exampleCo-exampleApp-1");     try {        myTest();     } catch (Exception e) {        e.printStackTrace();     }

     }

    }

     


    最新回复(0)