0
votes

I'm using the 'Hello Analytics' code example found at [https://developers.google.com/analytics/devguides/reporting/core/v3/quickstart/web-js][1] to allow me to gain access to my Google Analytics account and log the JSON response to the console. I can traverse the JSON and see how to request specific data I want from the Core Reporting API... Its pretty awesome.

I'm more interested in the realtime data, however. For this particular project I need something faster than the core reporting API...

Questions: Is the Realtime Reporting API something that is accessible via javascript and if so, would it (generally speaking) also work in this same fashion? Meaning Is it just a matter of changing EITHER of the following to get the same type of response from the REALTIME api?

// Set authorized scope.
var SCOPES = ['https://www.googleapis.com/auth/analytics.readonly'];

or

// Load the Google Analytics client library.
gapi.client.load('analytics', 'v3').then(function() {

I see a good number of resources / examples for the core reporting API, but not much on the realtime API

2

2 Answers

3
votes

The Core Reporting API and the Real time API work very much the same way, with the same authentication scopes even.

Core Reporting API call

gapi.client.analytics.data.ga.get(...)

Core Reporting API method reference.

Real time API call

gapi.client.analytics.data.realtime.get(...)

Realtime API method reference.

The Realtime API has is its own set of dimensions and metrics that begin with rt:... where the Core reporting API dimensions and metrics begin with ga:....

0
votes

I use both of them in my Google Apps Script. These identical functions may help to understand the similarity.

in Core Reporting API Function below gives users, sessions, avgSessionDuration and uniqueEvents for given dimensions with given filters.

function countClicks(analyticsID,startDate,endDate){
  try {
    var results = Analytics.Data.Ga.get(
      'ga:'+analyticsID,
      startDate,
      endDate,
      'ga:users,ga:sessions,ga:avgSessionDuration,ga:uniqueEvents',
      {'dimensions': 'ga:eventLabel','filters': 'ga:sessionDuration>1;ga:eventCategory=@'+SUBJECT}).rows;
    return results;
  } catch(e){
    Logger.log(e);
    logTable.appendRow([e["stack"],e.message,new Date()]);
  }
}

in Realtime API This gives me number of active users at given dimensions

function countClicks(analyticsID){
  try {
    var results = Analytics.Data.Realtime.get(
      'ga:'+analyticsID,
      'rt:activeUsers',
      {'dimensions':'rt:country,rt:city,rt:eventCategory,rt:eventAction,rt:eventLabel,rt:operatingSystem,rt:deviceCategory'}).rows;
    return results;
  } catch(e){
    Logger.log(e);
    logTable.appendRow([e["stack"],e.message,new Date()]);
  }
}

So, yes. It almost works as the same fashion.

There are more metric and dimension options in Core Reporting API acording to Realtime API