Google has lots of good documentation on starting out with Google APIs (which is used for Google Analytics) for certain languages like Python, Java, and C#, but completely sketchy for Node.js. Googling there are many examples but most are for v3. And the implementations get confusing if you aren’t familiar with oAuth or JSON Web Tokens.

Here is a really simplified chunk of starter code for pulling the number of Goal 2 completions by source/medium, ordered in descending order. The assumption is that you have already set up the service account and downloaded the JSON file with the private key.

var prettyjson = require('prettyjson'); // Un-uglify JSON output
var {google} = require('googleapis');
var key = require('<SERVICE\_ACCOUNT\_JSON\_FILE>'); // Downloaded JSON file

var viewID = '<VIEW\_ID>'; // Google Analytics view ID
var analytics = google.analyticsreporting('v4'); // Used for pulling report
var jwtClient = new google.auth.JWT(key.client\_email, // For authenticating and permissions

jwtClient.authorize(function (err, tokens) {
  if (err) {
  } else {
    console.log('Yup, we got authorized!');

// Set up what we data we want to pull from Google Analytics
metrics\_columns = \[{
  expression: 'ga:goal2Completions'

dimensions\_rows = \[{
  name: 'ga:sourceMedium'

date\_filters = \[{
  startDate: 'today',
  endDate: 'today',

sort = \[{
  fieldName: 'ga:goal2Completions',
  sortOrder: "DESCENDING"

var req = {
  reportRequests: \[{
    viewId: viewID,
    dateRanges: date\_filters,
    metrics: metrics\_columns,
    dimensions: dimensions\_rows,
    orderBys: sort

// Options for prettifying JSON output
var pretty\_json\_options = {
  noColor: false

// Pull report and output the data
    auth: jwtClient,
    resource: req
  function (err, response) {
    if (err) {
      console.log('Failed to get Report');
    // is where the good stuff is located
    console.log('Success - got something back from the Googlez');
    console.log(prettyjson.render(, pretty\_json\_options));