Google Analytics 多个日期范围 - NodeJS
Google Analytics Multiple Date Ranges - NodeJS
如何使用 nodeJS 客户端库获取使用多个日期范围的报告,如下面所列的示例?
取自https://developers.google.com/analytics/devguides/reporting/core/v4/basics#multiple_date_ranges
POST https://analyticsreporting.googleapis.com/v4/reports:batchGet
{
"reportRequests":
[
{
"viewId": "XXXX",
"dateRanges": [
{"startDate": "2014-11-01", "endDate": "2014-11-30"},
{"startDate": "2014-10-01", "endDate": "2014-10-30"}
],
"metrics": [
{"expression": "ga:pageviews"},
{"expression": "ga:sessions"}
],
"dimensions": [{"name": "ga:pageTitle"}]
}
]
}
我试过这个:
"dateRanges": [
{ "startDate": "2018-03-17", "endDate": "2018-03-24" },
{ "startDate": "14daysAgo", "endDate": "7daysAgo" }
]
并出现以下错误:
Missing required parameters: start-date, end-date
非常感谢您的帮助!
更新
我想我用错了函数 analytics.data.ga.get
而不是 analyticsreporting.reports.batchGet
但是当我尝试这个时:
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});
});
我明白了
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.',
我在这里错过了什么?
谢谢!
供日后参考
此处的 reportsRequest
对象需要位于资源对象内部,因为它是 post 主体的一部分,如 JustinBeckwith 在 https://github.com/google/google-api-nodejs-client/issues/1085
中所述
如何使用 nodeJS 客户端库获取使用多个日期范围的报告,如下面所列的示例?
取自https://developers.google.com/analytics/devguides/reporting/core/v4/basics#multiple_date_ranges
POST https://analyticsreporting.googleapis.com/v4/reports:batchGet
{
"reportRequests":
[
{
"viewId": "XXXX",
"dateRanges": [
{"startDate": "2014-11-01", "endDate": "2014-11-30"},
{"startDate": "2014-10-01", "endDate": "2014-10-30"}
],
"metrics": [
{"expression": "ga:pageviews"},
{"expression": "ga:sessions"}
],
"dimensions": [{"name": "ga:pageTitle"}]
}
]
}
我试过这个:
"dateRanges": [
{ "startDate": "2018-03-17", "endDate": "2018-03-24" },
{ "startDate": "14daysAgo", "endDate": "7daysAgo" }
]
并出现以下错误:
Missing required parameters: start-date, end-date
非常感谢您的帮助!
更新
我想我用错了函数 analytics.data.ga.get
而不是 analyticsreporting.reports.batchGet
但是当我尝试这个时:
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});
});
我明白了
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.',
我在这里错过了什么?
谢谢!
供日后参考
此处的 reportsRequest
对象需要位于资源对象内部,因为它是 post 主体的一部分,如 JustinBeckwith 在 https://github.com/google/google-api-nodejs-client/issues/1085