Google-api-nodejs-client: Analytics Realtime batch

Created on 18 Apr 2018  路  9Comments  路  Source: googleapis/google-api-nodejs-client

Hello,

Not an issue just some help needed.

How can batch requests for the realtime api?

I tried to use analyticsreporting.reports.batchGet but I get this error message:

message: 'Unknown dimensions(s): rt:campaign, unknown metric(s): rt:activeUsers\nFor details see https://developers.google.com/analytics/devguides/reporting/core/dimsmets.',

Thanks for the help!

All 9 comments

Greetings! I love it when someone asks for a sample I already wrote 馃槢
https://github.com/google/google-api-nodejs-client/blob/master/samples/analyticsReporting/batchGet.js

Let me know if you have any questions!

Hey Justin,

I used that sample to write my code here and it really works great :-)

The problem here is when I try to retrieve the realtime (rt:* not ga:*) metrics and/or dimensions, the api returns this error:

message: 'Unknown dimensions(s): rt:campaign, unknown metric(s): rt:activeUsers\nFor details see https://developers.google.com/analytics/devguides/reporting/core/dimsmets.'

This is the request I'm using:

"resource": {
    "reportRequests": [
        {
            "metrics": [
                {
                    "expression": "rt:activeUsers"
                }
            ],
            "viewId": "xxxx",
            "dimensions": [
                {
                    "name": "rt:campaign"
                }
            ]
        },
        {
            "metrics": [
                {
                    "expression": "rt:activeUsers"
                }
            ],
            "viewId": "xxxx",
            "dimensions": [
                {
                    "name": "rt:source"
                }
            ]
        },
        {
            "metrics": [
                {
                    "expression": "rt:activeUsers"
                }
            ],
            "viewId": "xxxx",
            "dimensions": [
                {
                    "name": "rt:medium"
                }
            ]
        },
        {
            "metrics": [
                {
                    "expression": "rt:activeUsers"
                }
            ],
            "viewId": "xxxx",
            "dimensions": [
                {
                    "name": "rt:keyword"
                }
            ]
        }
    ]
}

So... my question here is: am I able to batch realtime requests?

Thanks again for the awesome support.

:-)

So good news / bad news. It looks like there is an API specifically for realtime GA data:
https://developers.google.com/analytics/devguides/reporting/realtime/v3/reference/data/realtime/get

I was able to get this to work pretty easily:

const res = await analytics.data.realtime.get({
    ids: 'ga:65704806',
    metrics: 'rt:activeUsers'
  });

Now the bad news. It doesn't look like there's a top level batch API supported. There is the ability to generically do batch with Google APIs, but that isn't supported just yet.

The generalized batch support is being tracked over in #740.

I know it's not the answer you'd hope for, but we will get to batch eventually :) . Hope this helps!

@JustinBeckwith Could you please provide a full example of the above?

I am a bit lost when it comes to authenticating.

Thank you.

@phaistonian,

Here's what I have:

import config from './config.js'
import { google } from 'googleapis'

const scopes = ['https://www.googleapis.com/auth/analytics.readonly']
const jwtClient = new google.auth.JWT(config.clientEmail, null, config.privateKey, scopes)

async function doTheThings() {
  await jwtClient.authorize()
  let response = await google.analytics('v3').data.realtime.get({
    'auth': jwtClient,
    'ids': 'ga:' + config.viewId,
    'metrics': 'rt:activeUsers'
  })
  console.log(response)
}

doTheThings().catch(console.error)

Hey @karlwilbur.

Tried the block your provided and I am getting errors, probably because the viewId and privateKey values I am passing are not the expected ones.

Could you please point me where I could fetch those values from?

I would assume privateKey is provided by the console and the viewId from Analytics - correct?

Thank you.

@phaistonian, Yep. You'll need those values defined in a config object, or refactor to suit your needs.

We are beyond the scope of this issue. I've create a Gist and we can pick up the conversation there.
https://gist.github.com/karlwilbur/35be3803a20a2fb615806a0175518e42

Thank you for taking the time to compile this.

@phaistonian,

Here's what I have:

import config from './config.js'
import { google } from 'googleapis'

const scopes = ['https://www.googleapis.com/auth/analytics.readonly']
const jwtClient = new google.auth.JWT(config.clientEmail, null, config.privateKey, scopes)

async function doTheThings() {
  await jwtClient.authorize()
  let response = await google.analytics('v3').data.realtime.get({
    'auth': jwtClient,
    'ids': 'ga:' + config.viewId,
    'metrics': 'rt:activeUsers'
  })
  console.log(response)
}

doTheThings().catch(console.error)

thanks god !
I find v3 api for realtime batch for 2 day finally!!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

hainguyents13 picture hainguyents13  路  3Comments

nanu-c picture nanu-c  路  3Comments

Chethandsagar picture Chethandsagar  路  4Comments

streamnsight picture streamnsight  路  4Comments

leecheve picture leecheve  路  3Comments