Skip to content
Ric Lister edited this page May 25, 2013 · 2 revisions

The google analytics source uses google's core reporting API to display hourly time-series data for GA metrics. This source is unusual in that it requires a dashiki server plugin (written in ruby) to handle authentication.

Authentication

The source provies a method Dash.Source.Googleanalytics.auth to handle oAuth authentication. You will need to set up the Analytics API, create a project and a service account, and provide a private/public key pair.

  • go to https://code.google.com/apis/console for your project
  • go to Services and turn Analytics API 'on'
  • go to API Access and create a project (e.g. called 'Dashboard')
  • create a Service account and download the private key to e.g. ./keys/google in your dashiki config installation
  • copy the public_key associated with the file
  • copy the Email address, go to Team and add view permissions for this email

Authentication is then done once per dashboard refresh using the .pre() callback as shown in the example below.

Metric options

  • type: 'googleanalytics'
  • ids: project id from ga dashboard, e.g. 'ga:123456789'
  • target: metric name(s), e.g. 'ga:visits'; see API Explorer for metrics
  • filters: GA filter expressions to narrow results, e.g. 'ga:eventCategory==foo;ga:eventAction==bar'

The source plugin will automatically add dimesions in order to receive and parse all data components for hourly data. Currently GA API does not support higher resolution or real-time data.

Example

Dash.stats()
  .pre(function() {
    Dash.Source.Googleanalytics.auth({
      public_key:    '2hds78j89dfs54sadf7sdfh',
      private_key:   'keys/google/2hds78j89dfs54sadf7sdfh-privatekey.p12',
      email_address: '[email protected]',
      password:      'notasecret'
    })
  })
  .add({
    type:    'googleanalytics',
    ids:     'ga:123456789',     // project id from ga dashboard
    title:   'Visits',
    target:  'ga:visits',        // see http://ga-dev-tools.appspot.com/explorer/ for metrics
    filters: 'ga:eventCategory==foo'
  })

Auth session persistence

The dashiki server plugin for google analytics provides two endpoints, as demonstrated in the example above:

  • /_google_/auth: POST to this endpoint once per dashboard refresh with params public_key, private_key, email_address, password; if there is no existing access token, one will be obtained; if an expired token exists, it will be refreshed; tokens are persisted and shared between server processes by saving them to the server-side sqlite db sessions.db (it is safe to delete this file, the server will just fetch a new access token)
  • /_google_/get: GET to this endpoint with params public_key, ids, target, start-date, end-date, metrics, filters, dimensions; authentication tokens will be loaded from sessions.db, and the endpoint will return json data
Clone this wiki locally