如何从 NODE JS 访问 Google 分析

How to access Google analytics from NODE JS

我试过 gooleanalytics

var GA = require('googleanalytics'),
    util = require('util'),
    config = {
        "user": "myusername",
        "password": "mypassword"
    },
    ga = new GA.GA(config);

ga.login(function(err, token) {
    var options = {
        'ids': 'ga:<profileid>',
        'start-date': '2010-09-01',
        'end-date': '2010-09-30',
        'dimensions': 'ga:pagePath',
        'metrics': 'ga:pageviews',
        'sort': '-ga:pagePath'
    };

    ga.get(options, function(err, entries) {
       util.debug(JSON.stringify(entries));
    });
});

但是出现错误

events.js:165 throw err; ^

Error: Uncaught, unspecified "error" event. (https://developers.google.com/accounts/docs/AuthForInstalledApps ) at GA.emit (events.js:163:17)

Important: ClientLogin has been officially deprecated since April 20, 2012 and is now no longer available. Requests to ClientLogin will fail with a HTTP 404 response. We encourage you to migrate to OAuth 2.0 as soon as possible.

 config = {
        "user": "myusername",
        "password": "mypassword"
    },

您正在尝试使用登录名和密码向 Google 进行身份验证。这称为客户端登录,并于 2015 年针对所有 API 关闭。您需要使用 Oauth2 或服务帐户向 google 进行身份验证。您正在使用的代码和库在开发人员更新它以支持新的登录方法之前将无法使用,因为它已经三年没有开发了我认为您可能需要考虑使用官方 Google apis node.js client library example analyticsReporting/batchGet.js

更新

With a user and password 我为此追踪了 github 项目,看起来开发人员知道这个问题但选择不更新项目。

ncb000gt commented on Apr 24, 2015

No idea sorry. I haven't used this module in quite a long time so I'm not really sure what the deal is.

大约是它关闭的确切日期。这意味着他在事情发生后第一时间得到通知。

ncb000gt commented on May 28, 2015

I'd absolutely entertain a PR for this if anyone wants to take a stab at it. Otherwise, an official api may be better.

看来作者正在寻求帮助,如果有人想修复它。