4

Below code calls the Google Analytics Reporting API using Google's nodejs client version 0.7 here. It returns a socket hang up error on some executions but not always. Would this be errors on Google's servers' end? Is there an easy way to debug? BTW I'm making several calls consecutively, not sure if it's caused by rate limits.

gapi = require "googleapis"
authClient = new gapi.auth.JWT(
  config.ga.clientEmail,
  config.ga.privateKeyPath,
  null,
  [config.ga.scopeUri]
)

authPromise = new Promise (resolve, reject) ->
  authClient.authorize (err, token) ->
      resolve token
    return
  return

authPromise.then ->
  gapi.discover('analytics', 'v3')
    .withAuthClient(authClient)
    .execute (err, client) ->
      ...
Ray Shan
  • 1,646
  • 3
  • 17
  • 25

1 Answers1

1

The error surfaced after successfully getting the client then running: client.analytics.data.ga.get(queryObj).execute (err, result) -> ....

Ryan Seys, a contributor on the API client, suggested here that .discover should be called once and the resulting client should reused. I was calling .discover hundreds of times consecutively and creating a bunch of new clients. Server probably didn't like that. By storing and reusing the client the issue went away. Working code for prosperity:

gapi = require "googleapis"
authClient = new gapi.auth.JWT(
  config.ga.clientEmail,
  config.ga.privateKeyPath,
  null,
  [config.ga.scopeUri]
)

authPromise = new Promise (resolve, reject) ->
  authClient.authorize (err, token) ->
    gapi.discover('analytics', 'v3').withAuthClient(authClient).execute (err, client) ->
      resolve client
      return
    return
  return

authPromise.then (client) ->
  client.analytics.data.ga.get(queryObj).execute (err, result) ->
Ray Shan
  • 1,646
  • 3
  • 17
  • 25