简体   繁体   中英

Google Analytics Api: Filter by date

Is possible to filter the response by date?

For example, my report query pulls data for 7daysago . But I want to filter this data for each day. This way I don't have to run GoogleAnalytics query every day. Tried using the ga:date dimension as filter but it did not work.

DimensionFilter filter = new DimensionFilter()
        .setDimensionName("ga:date").setOperator("LESS_THAN")
        .setExpressions(Arrays.asList("today"));

Stacktrace:

com.google.api.client.googleapis.json.GoogleJsonResponseException: 400 Bad Request
{
  "code": 400,
  "errors": [
    {
      "domain": "global",
      "message": "Invalid value at 'report_requests[0].dimension_filter_clauses[0].filters[0].operator' (TYPE_ENUM), \"LESS_THAN\"",
      "reason": "badRequest"
    }
  ],

You can try the core reporting api v4. It offers the dateRange parameter. Let me know if you need some code. For example:

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

where the dateRangesArray could be:

    DateRange dateRange = new DateRange();
    dateRange.setStartDate(startDate);
    dateRange.setEndDate(endDate);
     dateRangessArray[0]=dateRange;   

The startDate and endDate are strings in the date format: yyyy-mm-dd

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM