Hi,
I started to use the analyticsreporting module v4 to batch the data.
I write this issue to notify you the difference between real usage and documentation.
Following the sample in your documentation to do a batchGet doesn't work.
analyticsreporting.reports.batchGet( {
"reportRequests":[
{
"viewId":"XXXX",
"dateRanges":[
{
"startDate":"2015-06-15",
"endDate":"2015-06-30"
}],
"metrics":[
{
"expression":"ga:sessions"
}],
"dimensions": [
{
"name":"ga:browser"
}]
}]
} ).execute(handleReportingResults)
When I do the call, I have this response message : "Error: Invalid JSON payload received. Unknown name "reportRequests": Cannot bind query parameter. 'reportRequests' is a message type. Parameters can only be bound to primitive types."
I was investigating and I discover that the object of the request has an empty json property.
I put some debugger on v4.js module and I discover that in the batchGet method.
batchGet: function(params, callback) {
var parameters = {
options: {
url: 'https://analyticsreporting.googleapis.com/v4/reports:batchGet',
method: 'POST'
},
params: params,
requiredParams: [],
pathParams: [],
context: self
};
return createAPIRequest(parameters, callback);
}
there is no reference related to "resource" (At line 82 of apirequest.js) property which is assign to options.json property (At line 164 of apirequest.js).
I have changed the params object of my module without edit v4.js and apirequest.js and it works.
This following snippet shows what I changed.
analytics.reports.batchGet({
"headers": {
"Content-Type": "application/json"
},
"auth": options.auth,
"resource": {
"reportRequests": req // req is an array of objects (like in documentation)
},
}, callback);
Does it make sense ? Is there another way to face this issue ?
Any concers let me know.
Thanks!
Have fun coding :)
Also, the
.execute(handleReportingResults)
method is not defined.
Any concers let me know.
Thanks!
Have fun coding :)
The documentation you're looking at (with the "JavaScript" label on the tab) is for the browser-based JavaScript library. If there were an example for Node.js, then the tab would be labeled "Node.js". That page doesn't have a Node.js example.
Sorry but I thought that the usage was the same of the browser-based.
Anyway, I thought that was good write this issue to notify other devs how to use the batchGet with this version.
Thanks man :)
Is there a node example?
Documentation is hard to follow for normal developers. A few simple examples would really help.
Thanks,
/t
Please fix. I've spent hours looking for this.
Simply putting a {resource: YourRequestObj } will do the trick:
analyticsreporting.reports.batchGet(
{
resource:{
"reportRequests":[
{
"viewId":"XXXX",
"dateRanges":[
{
"startDate":"2015-06-15",
"endDate":"2015-06-30"
}],
"metrics":[
{
"expression":"ga:sessions"
}],
"dimensions": [
{
"name":"ga:browser"
}]
}]
}
} )
can someone please link to this on the analytics samples?
Thanks @derappelt! I literally spent hours just to know that what was missing was the "resource" part of the request. The documentation is hard to follow.
This might help you out:
_(any help with formatting this comment highly appreciated ;) )_
`
const theThing = () => {
const {client_email, private_key} = key;
const jwtClient = new google.auth.JWT(
client_email,
null,
private_key,
scopes,
null,
);
jwtClient.authorize((err, tokens) => {
if (err) {
console.log(err);
return;
}
const reportRequests = {
reportRequests:
[
{
viewId,
dateRanges:
[
{
endDate: '2018-01-17',
startDate: '2017-01-17',
},
],
metrics:
[
{
expression: 'ga:pageViews',
},
],
dimensions:
[
{
name: 'ga:date',
},
],
},
],
};
analyticsreporting.reports.batchGet({
auth: jwtClient,
resource: reportRequests,
}, (error, response) => {
error && console.log('https://www.soundcloud.com/ambient-occlusion');
response && console.log(JSON.stringify(response));
});
});
};
`
Adding my voice to this. I had to look at the source code in order to find this single line in v4.js of analyticsreporting:
* @param {analyticsreporting(v4).GetReportsRequest} params.resource Request body data
https://github.com/google/google-api-nodejs-client/blob/master/src/apis/analyticsreporting/v4.ts#L45
This is not well documented.
I've had this problem and my problem is fixed. Thank you @the0ffh
Thank you guys, I've been spending two hours on this and finally found the solution thanks to you, especially @derappelt 馃憤
Most helpful comment
Simply putting a {resource: YourRequestObj } will do the trick:
analyticsreporting.reports.batchGet( { resource:{ "reportRequests":[ { "viewId":"XXXX", "dateRanges":[ { "startDate":"2015-06-15", "endDate":"2015-06-30" }], "metrics":[ { "expression":"ga:sessions" }], "dimensions": [ { "name":"ga:browser" }] }] } } )