Skip to main content
Last updated: 26 Oct 2023

Analytics on GOV.UK

GOV.UK uses Google Analytics to track user journeys through the site. The tracking data is available to anyone with Google Analytics Suite access for GOV.UK. You can request access to this by following the steps in the GDS wiki.

GOV.UK analytics code

The GOV.UK analytics codebase is a collection of JavaScript modules spread through a few different projects:

Tracking overview

User behaviours and journeys can be tracked in a variety of ways, the default method of tracking with Google Analytics is to record pageviews - This is data relating to a page the user has just requested (eg. URL, user-agent, referrer). Pageviews are typically recorded as the user visits the page.

Where a page offers the user multiple navigation choices it’s often desirable to track events, a typical example of this is recording when a user chooses an option in a group of radio buttons. Event data is usually comprised of a category, a label and a value.

Both pageviews and events can be augmented with custom dimensions, additional fragments of data describing the behaviour of the user, eg. which variant of an multivariate test they are assigned. The custom dimensions are documented on Confluence.

Cross domain tracking

Work has been done to implement cross domain tracking between GOV.UK and some government services. Tracking data for cross domain journeys is sent to a separate GA property from the existing GOV.UK GA property, and services can also maintain their own separate tracking.

Tracking a user journey across different services with different domains requires additional configuration so that the destination domain (eg. tax.service.gov.uk) can receive analytics data for the portion of the user journey taking place on the source domain (eg. GOV.UK).

To some extent the referrer value in pageview data will contain some evidence of where the user has arrived from, often this isn’t enough information or may be misleading due to authentication redirects.

The javascript function

GOVUK.analytics.addLinkedTrackerDomain('UA-43888888-1', 'someServiceTracker', ['some.service.gov.uk'])

registers a linked (cross domain) tracker and sends a pageview to the tracker. The current cross domain configuration on GOV.UK contains only those domains that have turned on cross domain tracking. More information is included in static’s analytics documentation.

Some cross domain tracking existed prior to this work to link 19 services to GOV.UK. It exists on some transaction start pages such as Change your driving test appointment, where the start button contains data attributes, such as:

<a href="https://some.service.gov.uk"
   data-module="cross-domain-tracking"
   data-tracking-code="UA-43888888-1"
   data-tracking-name="someServiceTracker">Start</a>

Cross domain event tracking

Once the linked tracker is registered, it’s also possible to track events using the registered tracker name:

GOVUK.analytics.trackEvent('Radio button chosen', 'selected-service-option', { 'trackerName': 'someServiceTracker' })

Cross domain tracking and GOV.UK javascript module loading

The GOVUK.Modules library will attempt to initialise and start a module for every data-module data attribute it encounters. In the case of cross domain tracking this can be undesirable if more than one element contains the cross-domain-tracking data-module because the underlying ga.js library from Google will not accept multiple calls to register a linked tracker and will throw javascript errors. See this static commit for how we mitigate against this for multiple govspeak buttons with cross domain tracking enabled.

Cross domain tracking for the GOV.UK Account

The GOV.UK Account shares the account home and management pages between www.gov.uk and account.gov.uk. We send tracking data to the main GOV.UK GA property from both domains and use cross domain tracking to see the whole user journey.

User journeys between these domains involve both users clicking links or buttons and server side redirects, so the standard cross domain tracking won’t suffice on its own. To handle these journeys we append a (cookie_consent) parameter recording if they have engaged with the cookie banner (see ADR). If the user has consented to tracking, we also send the user’s Google Analytics client ID (_ga). We listen for these when a user lands on a page. To achieve this, we have:

  1. A JavaScript module in govuk_publishing_components that adds the tracking parameters to a link or form action. Any links or forms that may result in a user being redirected to sign in must have the data-module="explicit-cross-domain-links" attribute so this module can add the event listener.

  2. A check in the the GA setup JavaScript that looks for the params in the URL when the page loads and updates the cookie consent status if needed. This prevents the user accepting cookies on one domain and then being asked to accept again when they’re sent to sign in.

  3. A redirect_with_analytics method to use on the server side that redirects the user and passes on the tracking parameters. This must be used when redirecting the user to or from the account.gov.uk domain.

Developing and debugging Google Analytics tracking

Google provide an extension Google Analytics Debugger which logs all GA suite interactions to the console. This is useful for testing what is being sent to GA and when. Other GA debuggers are also available.

Google Analytics Debugger console output