Google-api-nodejs-client: Analytics Report with Multiple Date Ranges

Created on 24 Mar 2018  路  2Comments  路  Source: googleapis/google-api-nodejs-client

Hello,

I'm trying to get a report using multiple date ranges with the following code:

analyticsreporting = google.analyticsreporting({
   version: 'v4',
   auth: oauth2Client
});

analyticsreporting.reports.batchGet({
    "reportRequests": [
        {
            "viewId": req.params.profileId,
            "dateRanges": [
                {
                    "startDate": "2018-03-17",
                    "endDate": "2018-03-24"
                },
                {
                    "startDate": "14daysAgo",
                    "endDate": "7daysAgo"
                }
            ],
            "metrics": [
                {
                    "expression": "ga:users"
                }
            ]
        }
    ]
}, function (err, results) {
    if (err){
        console.log('ERROR: ');
        console.log(err.errors);
        res.status(500).send(err.errors);
    }

    console.log(JSON.stringify(results));
    res.send({results: results});
});        

But I'm getting the following error message:

[ { message: 'Invalid JSON payload received. Unknown name "reportRequests[dateRanges][endDate]": Cannot bind query parameter. Field \'reportRequests[dateRanges][endDate]\' could not be found in request message.\nInvalid JSON payload received. Unknown name "reportRequests[dateRanges][startDate]": Cannot bind query parameter. Field \'reportRequests[dateRanges][startDate]\' could not be found in request message.\nInvalid JSON payload received. Unknown name "reportRequests[viewId]": Cannot bind query parameter. Field \'reportRequests[viewId]\' could not be found in request message.\nInvalid JSON payload received. Unknown name "reportRequests[metrics][expression]": Cannot bind query parameter. Field \'reportRequests[metrics][expression]\' could not be found in request message.',
    domain: 'global',
    reason: 'badRequest' } ]

What am I missing here?

The same payload works when I try the API on https://developers.google.com/analytics/devguides/reporting/core/v4/rest/v4/reports/batchGet

Thanks very much!

Most helpful comment

Greetings! The reportsRequest object here needs to be inside of a resource object, as it's part of the post body. I wrote up a complete working sample in #1086. Hope this helps!

All 2 comments

Greetings! The reportsRequest object here needs to be inside of a resource object, as it's part of the post body. I wrote up a complete working sample in #1086. Hope this helps!

@JustinBeckwith Thanks very much for the help! It worked like a charm :-)

Was this page helpful?
0 / 5 - 0 ratings