Search code examples
javascriptnode.jsgoogle-analyticsgoogle-oauthgoogle-analytics-api

How to access Google analytics from NODE JS


I have tried with 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));
    });
});

But getting error

events.js:165 throw err; ^

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


Solution

  • 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"
        },
    

    You are trying to authenticate to Google using login and password. This is called client login and was shut down in 2015 for all APIs. You will need to authenticate to google using either Oauth2 or a service account. The code and the library you are using will not work until the developer updates it to support the new login method as it hasnt been developed on in three years i think you may want to consider using the official Google apis node.js client library example analyticsReporting/batchGet.js

    Update

    With a user and password I tracked down the github project for this and it looks like the developer knows about the issue but chooses not to update the project.

    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.

    Which was around the exact date that it was shut down. Meaning he was informed as soon as it happened.

    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.

    It appears the author is looking for assistance if someone wants to fix it.