gpt4 book ai didi

java - 使用 java 在 google Analytics api 中指标返回始终为 null

转载 作者:太空宇宙 更新时间:2023-11-04 12:26:32 24 4
gpt4 key购买 nike

嗨,我正在为我的java使用谷歌分析报告API v4。我正在尝试获取维度和相应的维度指标。但我只获得维度值指标值始终返回 null。下面是我的代码。

public static void main(String[] args) {
try {
AnalyticsReporting service = initializeAnalyticsReporting();

GetReportsResponse response = getReport(service);
printResponse(response);
} catch (Exception e) {
e.printStackTrace();
}
}
private static AnalyticsReporting initializeAnalyticsReporting() throws GeneralSecurityException, IOException {

HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
GoogleCredential credential = new GoogleCredential.Builder()
.setTransport(httpTransport)
.setJsonFactory(JSON_FACTORY)
.setServiceAccountId(SERVICE_ACCOUNT_EMAIL)
.setServiceAccountPrivateKeyFromP12File(new File(KEY_FILE_LOCATION))
.setServiceAccountScopes(AnalyticsReportingScopes.all())
.build();

// Construct the Analytics Reporting service object.
return new AnalyticsReporting.Builder(httpTransport, JSON_FACTORY, credential)
.setApplicationName(APPLICATION_NAME).build();
}
private static GetReportsResponse getReport(AnalyticsReporting service) throws IOException {
// Create the DateRange object.

DateRange dateRange = new DateRange();
dateRange.setStartDate("2016-07-11");
dateRange.setEndDate("today");

Dimension sessionDurationBucket= new Dimension().setName("ga:sessionDurationBucket");

Metric sessions = new Metric().setExpression("ga:sessions").setAlias("sessions");






ReportRequest request = new ReportRequest().setViewId(VIEW_ID).setDateRanges(Arrays.asList(dateRange)).setDimensions(Arrays.asList(sessionDurationBucket)).setMetrics(Arrays.asList(sessions));

ArrayList<ReportRequest> requests = new ArrayList<ReportRequest>();
requests.add(request);

// Create the GetReportsRequest object.
GetReportsRequest getReport = new GetReportsRequest()
.setReportRequests(requests);

// Call the batchGet method.
GetReportsResponse response = service.reports().batchGet(getReport).execute();

// Return the response.
return response;
}
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();
for (int i = 0; i < dimensionHeaders.size() && i < dimensions.size(); i++) {
System.out.println(dimensionHeaders.get(i) + ": " + dimensions.get(i));
}

for (int j = 0; j < metrics.size(); j++) {
System.out.print("Date Range (" + j + "): ");
DateRangeValues values = metrics.get(j);
for (int k = 0; k < values.size() && k < metricHeaders.size(); k++) {
System.out.println(metricHeaders.get(k).getName() + ": " + values.get(k));
}
}
}
}
}
}


Sample output:
ga:sessionDurationBucket: 64
Date Range (0): sessions: null
ga:sessionDurationBucket: 708
Date Range (0): sessions: null
ga:sessionDurationBucket: 86
Date Range (0): sessions: null
ga:sessionDurationBucket: 891
Date Range (0): sessions: null

最佳答案

循环访问指标值时,您的代码中存在一个小错误:

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

for (int j = 0; j < metrics.size(); j++) {
System.out.print("Date Range (" + j + "): ");
DateRangeValues values = metrics.get(j);
for (int k = 0; k < values.getValues().size() && k < metricHeaders.size(); k++) {
System.out.println(metricHeaders.get(k).getName() + ": " + values.getValues().get(k));
}
}
}

对于每个DateRangeValue,您需要调用.getValues()来获取实际值。请参阅Sample documentationReference docs了解详情。

关于java - 使用 java 在 google Analytics api 中指标返回始终为 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38343486/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com