1

我想从 Reporting API V4 Java 类中提取多个指标。我从“main”函数将维度和指标的“ga:”名称传递给下面的函数:

private static ReportRequest createComplexRequest(java.lang.String startDate,java.lang.String endDate,ArrayList<java.lang.String> metrics,ArrayList<java.lang.String> dimensions, String pageToken){

DateRange dateRange = new DateRange();
dateRange.setStartDate(startDate);
dateRange.setEndDate(endDate);

//Create the Metric object ArrayList
int metricsSize=metrics.size();
Metric[] metricsArray = new Metric[metricsSize];

for(int i=0;i<metricsSize;i++){
    Metric metric = new Metric()
    .setExpression("ga:"+metrics.get(i))
    .setAlias(metrics.get(i));     
    metricsArray[i]=metric;
}

int dimensionsSize=dimensions.size();
Dimension[] dimensionsArray = new Dimension[dimensionsSize];   

for(int i=0;i<dimensionsSize;i++){
    Dimension dimension = new Dimension()
    .setName("ga:"+dimensions.get(i));     
    dimensionsArray[i]=dimension;
}    

ReportRequest request = new ReportRequest()
    .setViewId(VIEW_ID)
    .setDateRanges(Arrays.asList(dateRange))
    .setDimensions(Arrays.asList(dimensionsArray))        
    .setMetrics(Arrays.asList(metricsArray))
    .setPageToken(pageToken)
    .setPageSize(10000);

    return request;    
}

例如,我正在尝试提取“会话”和“反弹”指标。当我试图解决这种情况时,我意识到 metricHeaders.size() 是 2(与我的指标一样多),但 row.getMetrics() 只返回一个(“会话”指标)。以下是我的意思。我故意创建了以下有问题的打印函数来揭示问题:

private static void printResponse(GetReportsResponse response) {
for (Report report: response.getReports()) {
  ColumnHeader header = report.getColumnHeader();
  List<String> dimensionHeaders = header.getDimensions();
  List<MetricHeaderEntry> metricHeaders = header.getMetricHeader().getMetricHeaderEntries();
  List<ReportRow> rows = report.getData().getRows();


  if (rows == null) {
     System.out.println("No data found for " + VIEW_ID);
     return;
  }

  for (ReportRow row: rows) {
    List<String> dimensions = row.getDimensions();
    List<DateRangeValues> metrics = row.getMetrics();
    System.out.println("Metrics size:"+metrics.size());
    for (int i = 0; i < dimensionHeaders.size() && i < dimensions.size(); i++) {
      System.out.println(dimensionHeaders.get(i) + ": " + dimensions.get(i));
    }  
    for(int l = 0;l<metricHeaders.size();l++)
    {            
       System.out.println(metricHeaders.get(l).getName());
       DateRangeValues values = metrics.get(l); 
    }
   }
 }
}

上面的函数崩溃了,因为 metricHeaders.size() 是 2,但是指标是 List,只有一个指标,“会话”指标!这就是为什么我得到:

java.lang.IndexOutOfBoundsException: Index: 1, Size: 1

你知道为什么会这样吗???请帮忙!!

4

1 回答 1

1

GA 开发人员指南中的示例 printResponse() 方法存在一些错误。

请尝试以下代码:

private static void printResponseCustom(GetReportsResponse response) {
   for (Report report: response.getReports()) {
     ColumnHeader header = report.getColumnHeader();
     List<String> dimensionHeaders = header.getDimensions();
     List<MetricHeaderEntry> metricHeaders = header.getMetricHeader().getMetricHeaderEntries();
     List<ReportRow> rows = report.getData().getRows();


     if (rows == null) {
        System.out.println("No data found for " + VIEW_ID);
        return;
     }

     for (ReportRow row: rows) {
       List<String> dimensions = row.getDimensions();
       List<DateRangeValues> metrics = row.getMetrics();

       System.out.println("Metrics size:"+metrics.size());
       System.out.println("Metrics headers:"+metricHeaders.size());        
       for (int i = 0; i < dimensionHeaders.size() && i < dimensions.size(); i++) {
         System.out.println(dimensionHeaders.get(i) + ": " + dimensions.get(i));
       }

       int k = 0;
       for (int j = 0; j < metricHeaders.size() && j < metrics.size(); 
           DateRangeValues values = metrics.get(j);
           for (String value : values.getValues()) {
               MetricHeaderEntry entry = metricHeaders.get(k);
               System.out.println(entry.getName() + ": " + value);
               k++;
           }       
       }
     }
   }
 }
于 2016-06-22T11:15:25.700 回答