public AnalyticsResults getResults(AnalyticsQuery analyticsQuery) throws AnalyticsException { logger.info("Entered"); AnalyticsResults results = new AnalyticsResults(); try { String tableId = (new StringBuilder()).append("ga:").append(profileId).toString(); com.google.api.services.analytics.Analytics.Data.Ga.Get get = analytics .data() .ga() .get( tableId, analyticsQuery.getStartDate(), analyticsQuery.getEndDate(), analyticsQuery.getMetrics()); if (!StringUtils.isBlank(analyticsQuery.getDimensions())) { logger.info( (new StringBuilder()) .append("Adding Dimensions:") .append(analyticsQuery.getDimensions()) .toString()); get.setDimensions(analyticsQuery.getDimensions()); } if (!StringUtils.isBlank(analyticsQuery.getSort())) { logger.info( (new StringBuilder()) .append("Adding Sort:") .append(analyticsQuery.getSort()) .toString()); get.setSort(analyticsQuery.getSort()); } if (!StringUtils.isBlank(analyticsQuery.getFilters())) { logger.info( (new StringBuilder()) .append("Adding Filter:") .append(analyticsQuery.getFilters()) .toString()); get.setFilters(analyticsQuery.getFilters()); } if (analyticsQuery.getMaxResults().intValue() > 0) get.setMaxResults(analyticsQuery.getMaxResults()); else { get.setMaxResults(Integer.valueOf(50)); } GaData gaData = get.execute(); if (gaData.getRows() == null || gaData.getRows().isEmpty()) { logger.info("No results Found."); } else { results.setEmpty(false); String header; for (Iterator i$ = gaData.getColumnHeaders().iterator(); i$.hasNext(); results.addHeader(header)) { com.google.api.services.analytics.model.GaData.ColumnHeaders columnHeader = (com.google.api.services.analytics.model.GaData.ColumnHeaders) i$.next(); header = columnHeader.getName(); logger.info((new StringBuilder()).append("Header : ").append(header).toString()); } AnalyticsData analyticsData; for (Iterator i$ = gaData.getRows().iterator(); i$.hasNext(); results.addData(analyticsData)) { List row = (List) i$.next(); analyticsData = new AnalyticsData(); for (int i = 0; i < row.size(); i++) { String data = (String) row.get(i); header = (String) results.getHeaders().get(i); logger.info((new StringBuilder()).append("Header : ").append(header).toString()); logger.info((new StringBuilder()).append("Data : ").append(data).toString()); analyticsData.put(header, data); } } } } catch (Exception expGeneral) { throw new AnalyticsException(expGeneral); } logger.info("Leaving"); return results; }
public static GoogleAnalyticsData getData(String p12CertPath) { GoogleAnalyticsData gaData = null; Analytics analytics = null; try { analytics = getAnalytics(getCredential(p12CertPath)); } catch (GeneralSecurityException e) { logger.error("Security exception while obtaining Google API Certificate:", e); } catch (IOException e) { logger.error("IO exception while obtaining Google API Certificate:", e); } if (analytics == null) { logger.error("Error while getting Google Analytics object"); } String profileId = null; try { profileId = SITEProfileId(analytics); } catch (IOException e) { logger.error("Error while getting Google Analytics Profile ID:", e); } if (profileId == null) { logger.error("Error while getting Google Analytics Profile ID"); } try { GaData data = getData(analytics, profileId, "2010-01-01", "today"); if (data != null) { gaData = new GoogleAnalyticsData(); gaData.setTotalPageViews(Long.parseLong(data.getTotalsForAllResults().get("ga:pageviews"))); gaData.setTotalSessions(Long.parseLong(data.getTotalsForAllResults().get("ga:sessions"))); for (int count = 0; count < 90; count++) { List<String> row = data.getRows().get(count); if (count < 30) { gaData.setPageViews30(gaData.getPageViews30() + Long.parseLong(row.get(1))); gaData.setSessions30(gaData.getSessions30() + Long.parseLong(row.get(2))); } if (count < 60) { gaData.setPageViews60(gaData.getPageViews60() + Long.parseLong(row.get(1))); gaData.setSessions60(gaData.getSessions60() + Long.parseLong(row.get(2))); } if (count < 90) { gaData.setPageViews90(gaData.getPageViews90() + Long.parseLong(row.get(1))); gaData.setSessions90(gaData.getSessions90() + Long.parseLong(row.get(2))); } } } } catch (IOException e) { logger.error("Error while getting Google Analytics Data.", e); } return gaData; }