Skip to content


Folders and files

Last commit message
Last commit date

Latest commit



25 Commits

Repository files navigation

Maintainer Wanted

This project is looking for maintainer(s).

For now only pull requests of external contributors are being reviewed, accepted and welcomed. No more bug fixes or new features will be implemented.

If you are interested in giving this project some ❤️, please comment on this issue: #9


Google Analytics API access, with automated concurrency limiting and optional request caching.


npm install ga-api --save


  • Concurrent requests limiting (GA allows 10 by default, and we retry on 403s)
  • Caching of requests (optional and configurable)
  • Loosely based on this.
Note: you'll need to setup a google service account here: in order to use this library, and have access to google analytics reporting.


Before using the library, setup the google service account as described in the link, then grab the following information:

  • clientId: "[CLIENTID]"
  • email: "[CLIENTID]" - you must add a service email here, for the correct view.
  • key: "[PATH/TO/PRIVATE.KEY.FILENAME].pem", as described here
  • ids: The view ID(s), eg: "ga:[VIEWID]"

You can find the client ID and Email in the google developers console here (after creating it):

alt text

And the view ID in the google analytics console here:

alt text

So you end up with something like:

var options = {
  clientId: "",
  email: "",
  key: "google-service-private-key.pem",
  ids: "ga:12345678"


gaApi(args, callBack, settings)


  • args - all the authentication settings plus query parameters
  • callBack - a function that can take an error and a result argument
  • settings - an optional settings object where you can override the default settings


  • startDate - (YYYY-MM-DD), eg: 2015-05-21
  • endDate - (YYYY-MM-DD), eg: 2015-05-28
  • metrics - any metric to include, eg: "ga:session"
  • filters - any filtering you wnat, eg: "ga:pagePath=~/Home"
  • dimensions - any dimensions you like, eg: "ga:source"
  • maxResults - maximum number of results to return, eg: 100
  • startIndex - offset to start from to allow paging, eg: 1, 101
  • sort - what to sort by, eg: "ga:source"

The args must also include the Authentication information from above, ie: clientId, email, key and ids.


This function receives an error object and the resulting data from your query, i.e:

function(error, result) {
    if(error) throw error;
    //	Do something with result here


The settings can optionally override some deafult settings, these include:

  • cache - time in ms for caching requests, default is 0, ie: no cache
  • cacheDir - directory to save the cache - uses system temp dir by default (process.env.TMPDIR)
  • concurrentLimit - maximum concurrent requests - default is 10 (which is what Google set by default)
  • concurrentDelay - how long to delay if we get a 403 error, default is 1000ms
  • concurrentMaxRetry - how many times to retry after a 403 error - default is 3


var options = {...authentication info from above...},
	gaApi = require('ga-api');

gaApi(_.extend({}, options, {
	startDate: "2015-06-03",
	endDate: "2015-06-10",
	dimensions: "ga:affiliation,ga:date",
	metrics: "ga:revenuePerTransaction"
}), function(err, data) {

The resulting data will look something like this:

{ kind: 'analytics#gaData',
  id: ',ga:date&metrics=ga:revenuePerTransaction&sort=ga:affiliation&start-date=2015-06-03&end-date=2015-06-10',
   { 'start-date': '2015-06-03',
     'end-date': '2015-06-10',
     ids: 'ga:XXXXXXXX',
     dimensions: 'ga:affiliation,ga:date',
     metrics: [ 'ga:revenuePerTransaction' ],
     sort: [ 'ga:affiliation' ],
     'start-index': 1,
     'max-results': 1000 },
  itemsPerPage: 1000,
  totalResults: 32,
  selfLink: ',ga:date&metrics=ga:revenuePerTransaction&sort=ga:affiliation&start-date=2015-06-03&end-date=2015-06-10',
   { profileId: 'XXXXXXXX',
     accountId: 'XXXXXXXX',
     webPropertyId: 'UA-XXXXXXXX-2',
     internalWebPropertyId: 'XXXXXXXX',
     profileName: 'Web data',
     tableId: 'ga:XXXXXXXX' },
  containsSampledData: false,
   [ { name: 'ga:affiliation',
       columnType: 'DIMENSION',
       dataType: 'STRING' },
     { name: 'ga:date', columnType: 'DIMENSION', dataType: 'STRING' },
     { name: 'ga:revenuePerTransaction',
       columnType: 'METRIC',
       dataType: 'CURRENCY' } ],
  totalsForAllResults: { 'ga:revenuePerTransaction': '123.456' },
   [ [ 'SOURCE1', '20150603', '12.34' ],
     [ 'SOURCE1', '20150604', '56.78' ],
     [ 'SOURCE2', '20150603', '90.12' ],
     [ 'SOURCE2', '20150604', '34.56' ],
     [ 'SOURCE3', '20150603', '78.90' ],
     [ 'SOURCE3', '20150605', '12.34' ],

So essentially the "rows" attribute of the data object will have what you want.

Note: See this page:

for examples of how to use dimensions and metrics and create a report, and:

for examples of dimensions and metrics


Google Analytics API access



Security policy





No releases published


No packages published

Contributors 4