protected Analytics.Data.Ga.Get getQuery(Analytics analytics) { Analytics.Data dataApi = analytics.data(); Analytics.Data.Ga.Get query; try { String metrics = environmentSubstitute(meta.getMetrics()); if (Utils.isEmpty(metrics)) { logError(BaseMessages.getString(PKG, "GoogleAnalytics.Error.NoMetricsSpecified.Message")); return null; } query = dataApi .ga() .get( meta.isUseCustomTableId() ? environmentSubstitute(meta.getGaCustomTableId()) : meta.getGaProfileTableId(), // ids environmentSubstitute(meta.getStartDate()), // start date environmentSubstitute(meta.getEndDate()), // end date metrics // metrics ); String dimensions = environmentSubstitute(meta.getDimensions()); if (!Utils.isEmpty(dimensions)) { query.setDimensions(dimensions); } if (meta.isUseSegment()) { if (meta.isUseCustomSegment()) { query.setSegment(environmentSubstitute(meta.getCustomSegment())); } else { query.setSegment(meta.getSegmentId()); } } if (!Utils.isEmpty(meta.getSamplingLevel())) { query.setSamplingLevel(environmentSubstitute(meta.getSamplingLevel())); } if (!Utils.isEmpty(meta.getFilters()) && !Utils.isEmpty(environmentSubstitute(meta.getFilters()))) { query.setFilters(environmentSubstitute(meta.getFilters())); } if (!Utils.isEmpty(meta.getSort())) { query.setSort(environmentSubstitute(meta.getSort())); } return query; } catch (IOException ioe) { return null; } }
/** * Returns the top 25 source paths with most total conversions. The MCF API is used to retrieve * this data. * * @param analytics The analytics service object used to access the API. * @param tableId The table ID from which to retrieve data. * @return The response from the API. * @throws IOException If an API error occurred. */ private static McfData executePathQuery(Analytics analytics, String tableId) throws IOException { return analytics .data() .mcf() .get( tableId, "2012-01-01", // Start date. "2012-03-31", // End date. "mcf:totalConversions") // Metrics. .setDimensions("mcf:sourcePath") .setSort("-mcf:totalConversions") .setMaxResults(25) .execute(); }
/** * @param analytics * @param profileId * @param startDate format = YYYY-mm-DD * @param endDate format = YYYY-mm-DD * @return * @throws IOException */ private static GaData getData( Analytics analytics, String profileId, String startDate, String endDate) throws IOException { GaData sessionData = analytics .data() .ga() .get( "ga:" + profileId, // Table Id. ga: + profile id. startDate, // Start date endDate, // End date. "ga:pageviews,ga:sessions") .setDimensions("ga:date") .setSort("-ga:date") // Metrics. .execute(); return sessionData; }