Skip to content

Commit

Permalink
Write documentation
Browse files Browse the repository at this point in the history
- document new analytics init, code structure for analytics modules, integration of new analytics into existing Modules
  • Loading branch information
andysellick committed Sep 8, 2022
1 parent 49711a3 commit 75ad14c
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions docs/analytics-gtm/analytics.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,42 @@ Events happen when a user interacts with certain things, for example clicking on

Search data is gathered when users perform a search.

## Cookie consent

The analytics code is only loaded if users consent to cookies. This is managed by the `init-ga4.js` script.

If the page loads and cookie consent has already been given, the analytics code is initialised. This includes sending a page view and creating any event listeners for analytics code such as link tracking.

If the page loads and cookie consent has not been given, an event listener is created for the `cookie-consent` event, which is dispatched by the [cookie banner component](https://github.com/alphagov/govuk_publishing_components/pull/2041/commits/777a381d2ccb67f0a7e78ebf659be806d8d6442d). If triggered, the event listener will initialise the analytics code as described above. This allows analytics to begin on the page where the user consents to cookies.

## Code structure

It is important that no analytics code runs until cookie consent is given. Code to be initialised as part of cookie consent should be attached to the `window.GOVUK.analyticsGA4.analyticsModules` object and include an `init` function, using the structure shown below.

```JavaScript
window.GOVUK = window.GOVUK || {}
window.GOVUK.analyticsGA4 = window.GOVUK.analyticsGA4 || {}
window.GOVUK.analyticsGA4.analyticsModules = window.GOVUK.analyticsGA4.analyticsModules || {};

(function (analyticsModules) {
'use strict'

var ExampleCode = {
init: function () {
// do analytics stuff, like send a page view
}
}

analyticsModules.ExampleCode = ExampleCode
})(window.GOVUK.analyticsGA4.analyticsModules)
```

When cookie consent is given, `init-ga4.js` looks through the `analyticsModules` object for anything with an `init` function, and executes them if found. This means that analytics code will not be executed unless consent is given, and gives a standard way to add more analytics code without additional initialisation.

### Code structure for Modules

Where analytics code is required as a [GOV.UK JavaScript Module](https://github.com/alphagov/govuk_publishing_components/blob/main/docs/javascript-modules.md), the code structure for the [existing model for deferred loading](https://github.com/alphagov/govuk_publishing_components/blob/main/docs/javascript-modules.md#modules-and-cookie-consent) should be used.

## Data schemas

All of the data sent to GTM is based on a common schema.
Expand Down

0 comments on commit 75ad14c

Please sign in to comment.