无法从Google Analytics Reporting API V4(Java)

时间:2016-06-16 14:15:21

标签: google-analytics google-analytics-v4 google-reporting-api

我想从Reporting API V4 Java类中提取多个Metric。我将“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但是metrics只有一个指标的List,“sessions”指标!!这就是我得到的原因:

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

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

1 个答案:

答案 0 :(得分: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++;
           }       
       }
     }
   }
 }