Esempio n. 1
0
  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;
    }
  }
  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;
  }