Analytics on the Admin app - alphagov/notifications-manuals GitHub Wiki

Analytics on the Admin app

Introduction

Our Google Analytics (GA) implementation is wrapped in an abstraction layer, called a 'toolkit', to make our use consistent with other projects. The code was taken from GOVUK but is also used on other GDS projects such as Digital Marketplace.

GOVUK.Analytics is defined in app/assets/javascripts/analytics/analytics.js. The code to set up the tracker is defined in app/assets/javascripts/analytics/init.js.

The analytics toolkit

Our version of the toolkit provides methods for tracking pageviews and events. It currently includes:

  • a Google Analytics universal tracker wrapper
  • code to asynchronously load universal analytics
  • a generic Analytics wrapper that allows a single tracker to be configured

GOVUK.Analytics

The tracker is set up by creating a new instance of GOVUK.Analytics. The convention is to set it as window.GOVUK.analytics.

Loading the Google Analytics library

The Google Analytics library needs to be loaded before the tracker is set up. This is done by running:

window.GOVUK.Analytics.load();

Configuring the tracker

The tracker is configured by the options sent in when the instance is created.

Configuration options

Argument Description
trackingId (required) Unique identifier for your GA account
cookieDomain (required) Domain to associate the GA cookies with
anonymizeIp (required) Anonymise user's IP addresses
allowAdFeatures (required) Disable beacons used for advertising features
transport (required) Transport mechanism used to register hits
expires (required) Expiry date of GA cookies

Once instantiated, the GOVUK.analytics object can be used to track virtual pageviews and send custom events.

Virtual pageviews

Page tracking allows you to measure the number of views you had of a particular page on your web site.

See: https://developers.google.com/analytics/devguides/collection/analyticsjs/field-reference#page

Argument Description
page (optional) Path section of URL for page
title (optional) Title of the page, if different to document.title

Note: Any UUIDs in path will be stripped before sending.

// Track current page
GOVUK.analytics.trackPageview();

// Track a custom path
GOVUK.analytics.trackPageview('/path');

// Use a custom title for the page
GOVUK.analytics.trackPageview('/path', 'Custom title');

Custom events

Event tracking allows you to measure how users interact with the content of your website. For example, you might want to measure how many times a button was pressed, or how many times a particular item was used.

See: https://developers.google.com/analytics/devguides/collection/analyticsjs/events

Argument Description
category (required) Typically the object that was interacted with, eg "JavaScript error"
action (required) The type of interaction, eg a Javascript error message
options.label (required) The options argument, containing a label property to describe the event
// Track a custom event
GOVUK.analytics.trackEvent('category', 'action', { 'label': 'missing field'});