代码之家  ›  专栏  ›  技术社区  ›  Marrone

Google Analytics多个日期范围-NodeJS

  •  0
  • Marrone  · 技术社区  · 6 年前

    如何使用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.',
    

    我错过了什么?

    谢谢

    1 回复  |  直到 6 年前
        1
  •  0
  •   Marrone    6 年前

    供日后参考

    这个 reportsRequest 这里的对象需要位于资源对象内部,因为它是JustinBeckwith在 https://github.com/google/google-api-nodejs-client/issues/1085