我正在尝试使用JAVA将Google Analytics(分析)数据报告给数据库,但是即使该URL存在,某些地址也返回null。
示例:/目录/ companyX /美国/ zh -----找到了。
/ catalog / companyX / US / zh / research ------他找不到它。
我尝试使用过滤器“ ga:pagePath = @ / research”,但始终返回null。有什么问题吗?因为他在Google Analytics(分析)网站上找到了这些URL。
public GetReportsResponse getReport(AnalyticsReporting service) throws IOException {
// Create the DateRange object.
DateRange dateRange = new DateRange();
dateRange.setStartDate("2019-10-06");
dateRange.setEndDate("2019-10-06");
// Create the Metrics object.
Metric timeOnPage = new Metric().setExpression("ga:timeOnPage");
Metric totalEvents = new Metric().setExpression("ga:totalEvents");
Metric avgSessionDuration = new Metric().setExpression("ga:avgSessionDuration");
Metric hits = new Metric().setExpression("ga:hits");
// Create the Dimensions object.
Dimension browser = new Dimension().setName("ga:browser");
Dimension device = new Dimension().setName("ga:deviceCategory");
Dimension country = new Dimension().setName("ga:country");
Dimension page = new Dimension().setName("ga:pagePath");
Dimension eventCategory = new Dimension().setName("ga:eventCategory");
Dimension eventAction = new Dimension().setName("ga:eventAction");
Dimension eventLabel = new Dimension().setName("ga:eventLabel");
Dimension date = new Dimension().setName("ga:date");
Dimension pageTitle = new Dimension().setName("ga:pageTitle");
ReportRequest request6 = new ReportRequest().setViewId(VIEW_ID).setDateRanges(Arrays.asList(dateRange))
.setPageSize(100000)
.setMetrics(Arrays.asList(totalEvents, hits, avgSessionDuration, timeOnPage))
.setDimensions(Arrays.asList(date, country, page, browser, device, eventCategory, eventAction,
eventLabel, pageTitle));
ArrayList<ReportRequest> requests = new ArrayList<ReportRequest>();
requests.add(request6);
// 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;
}
public 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) {
TableGA tableGA = new TableGA();
Acessos acessos = new Acessos();
List<String> dimensions = row.getDimensions();
List<DateRangeValues> metrics = row.getMetrics();
if (row.getDimensions() != null) {
for (int i = 0; i < dimensionHeaders.size() && i < dimensions.size(); i++) {
switch (dimensionHeaders.get(i)) {
case "ga:browser":
tableGA.setBrowser(dimensions.get(i));
break;
case "ga:deviceCategory":
tableGA.setDevice(dimensions.get(i));
break;
case "ga:country":
tableGA.setCountry(dimensions.get(i));
break;
case "ga:pagePath":
tableGA.setPage(dimensions.get(i));
break;
case "ga:eventCategory":
tableGA.setEventCategory(dimensions.get(i));
break;
case "ga:eventAction":
tableGA.setEventAction(dimensions.get(i));
break;
case "ga:eventLabel":
tableGA.setEventLabel(dimensions.get(i));
break;
case "ga:date":
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date convertedCurrentDate = null;
String year = dimensions.get(i).substring(0, 4);
String month = dimensions.get(i).substring(4, 6);
String day = dimensions.get(i).substring(6, 8);
String date = year + "-" + month + "-" + day;
try {
convertedCurrentDate = simpleDateFormat.parse(date);
} catch (ParseException e) {
e.printStackTrace();
}
tableGA.setActualDate(convertedCurrentDate);
break;
case "ga:pageTitle":
tableGA.setPageTitle(dimensions.get(i));
break;
}
}
}
for (int j = 0; j < metrics.size(); j++) {
DateRangeValues values = metrics.get(j);
for (int k = 0; k < values.getValues().size() && k < metricHeaders.size(); k++) {
switch (metricHeaders.get(k).getName()) {
case "ga:timeOnPage":
tableGA.setTimeOnPage(Double.parseDouble(values.getValues().get(k)));
break;
case "ga:totalEvents":
tableGA.setTotalEvents(Integer.parseInt(values.getValues().get(k)));
break;
case "ga:avgSessionDuration":
tableGA.setAvgSessionDuration(Double.parseDouble(values.getValues().get(k)));
break;
case "ga:hits":
tableGA.setHits(Integer.parseInt(values.getValues().get(k)));
break;
}
}
}
gaService.save(tableGA);
System.out.println(tableGA.getPage());
gaService.save(acessos);
}
}
}