# @govuk-one-login/one-login-analytics

> Reusable GA4 package for GDS One Login

Latest version **1.0.2** (published 2024-04-11) · ISC license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install @govuk-one-login/one-login-analytics
pnpm add @govuk-one-login/one-login-analytics
yarn add @govuk-one-login/one-login-analytics
bun add @govuk-one-login/one-login-analytics
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 1.0.2 |
| Published | 2024-04-11 |
| First published | 2023-12-07 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 34.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | DI Frontend Capability Team |
| Maintainers | joanneyoung01, andyholme-gds, di-fabs, pcornishgds, wilsondrrn, merlinc |
| Keywords | one-login, gds, ga4, google analytics 4, onelogin, one login |

## Links

- npm: https://www.npmjs.com/package/@govuk-one-login/one-login-analytics
- Repository: https://github.com/govuk-one-login/di-fec-ga4
- Homepage: https://github.com/govuk-one-login/di-fec-ga4#readme
- Issues: https://github.com/govuk-one-login/di-fec-ga4/issues
- npm.io page: https://npm.io/package/@govuk-one-login/one-login-analytics

## Alternatives

- [cli-color](https://npm.io/package/cli-color.md) — 3.4M weekly downloads
- [log](https://npm.io/package/log.md) — 1.3M weekly downloads
- [logstash-client](https://npm.io/package/logstash-client.md) — 4.5K weekly downloads
- [@nocobase/plugin-logger](https://npm.io/package/@nocobase/plugin-logger.md) — 2.0K weekly downloads
- [child-process-debug](https://npm.io/package/child-process-debug.md) — 695 weekly downloads

## Recent versions

- 1.0.2 (latest) — 2024-04-11
- 0.0.10 — 2024-04-04
- 0.0.9 — 2024-02-15
- 0.0.8 — 2024-02-14
- 0.0.7 — 2024-02-12
- 0.0.6 — 2024-02-02
- 0.0.5 — 2024-01-30
- 0.0.4 — 2024-01-09
- 0.0.3 — 2023-12-20
- 0.0.2 — 2023-12-12
- 0.0.1 — 2023-12-07

## README

<!-- Improved compatibility of back to top link: See: https://github.com/othneildrew/Best-README-Template/pull/73 -->

<a name="readme-top"></a>

<!-- PROJECT LOGO -->
<br />
<div align="center">
  
<h3 align="center">GOV UK One Login GA4 Implementation</h3>
  <p align="center">
    This package enables GOV UK LOGIN frontend Node.js applications to use Google Tag Manager and Google Analytics 4.
    <br />
    <a href="https://govukverify.atlassian.net/wiki/spaces/DIFC/pages/3801317710/Analytics+package+One-login-ga4"><strong>Explore the docs »</strong></a>
    <br />
    <br />
    <a href="https://github.com/govuk-one-login/di-fec-ga4-demo">View Demo</a>
    ·
    <a href="https://github.com/govuk-one-login/di-fec-ga4/issues">Report Bug</a>
    ·
    <a href="https://github.com/govuk-one-login/di-fec-ga4/issues">Request Feature</a>
  </p>
</div>

<!-- TABLE OF CONTENTS -->
<details>
  <summary>Table of Contents</summary>
  <ol>
    <li>
      <a href="#about-the-project">About The Project</a>
    </li>
    <li>
      <a href="#getting-started">Getting Started</a>
      <ul>
        <li><a href="#installation">Installation</a></li>
      </ul>
    </li>
  </ol>
</details>

<!-- ABOUT THE PROJECT -->

## About The Project

The GDS One Login GA4 (Google Analytics 4) node package is a shared, reusable solution created to facilitate the upgrade from GAU to GA4 across the One Login programme as GAU is being retired mid 2024.

The purpose of this package is to make it as easy as possible for the various pods that make up the One Login journey to upgrade their analytics while having as minimal an impact as possible on the dev teams time and effort.

The package is owned by the DI Frontend Capability team, part of the development of this tool involves ongoing discovery with the pods responsible for maintaining the frontend repositories that make up the One Login journey. As more information is collated, the package and documentation will be updated. As such, it is considered a WIP and the pods will be notified when a stable release is ready.

<p align="right">(<a href="#readme-top">back to top</a>)</p>

<!-- GETTING STARTED -->

## Getting Started

### Installation

1. Install NPM package
   ```sh
   npm install @govuk-one-login/one-login-analytics
   ```
2. Configure your node application's startup file (example: app.js or index.js) and add a new virtual directory:

   ```js
   app.use(
     "/ga4-assets",
     express.static(
       path.join(
         __dirname,
         "../node_modules/@govuk-one-login/one-login-analytics/lib",
       ),
     ),
   );
   ```

   [!WARNING] Check if the path to your node module folder is the correct one. [!WARNING]

3. Set a variable “ga4ContainerId” with the value of your google tag manager id (format: GTM-XXXXXXX) and be sure it’s accessible to your base nunjucks template (example: src/views/common/layout/base.njk).
   You can also set the variable uaContainerId with the value of your google tag container id (format: GTM-XXXXXXX).

[!NOTE] Different methods exist if you want to set this variable. Some projects use a middleware, some will prefer to use another method. [!NOTE]

4. Add this block of code into your base nunjucks template:

   ```js
    <script src="/ga4-assets/analytics.js"></script>
    <script>
    window.DI.appInit({ga4ContainerId: "{{ga4ContainerId}}", uaContainerId: '{{ uaContainerId }}'})
    </script>
   ```

   window.DI.appInit can take another parameter: an object of settings. That can be used if you want to disable some options. This is the property of this settings object:

   - disableGa4Tracking (boolean): disable GA4 trackers
   - disableUaTracking (boolean): disable Universal Analytics tracker
   - cookieDomain (string): specify the domain the analytics consent cookie should be raised against (default is "account.gov.uk")

Example of call:

```js
window.DI.appInit(
  {
    ga4ContainerId: "{{ga4ContainerId}}",
    uaContainerId: "{{ uaContainerId }}",
  },
  {
    disableGa4Tracking: true,
    disableUaTracking: true,
    cookieDomain: "{{ cookieDomain }}",
  },
);
```

[!NOTE] window.DI.appInit is a function loaded from analytics.js. That will create a new instance of our analytics library and store into window.DI.analyticsGa4 [!NOTE]

### Analytics Cookie Consent

The Cookie class is responsible for managing cookies consent about analytics. It provides methods and fields to handle cookie-related operations:

- Set the cookie when the visitor decides to accept or reject any analytics tracking
- Hide the cookie banner that displays a message when the visitor has decided if he rejects or accepts the analytics tracking
- Show the element that displays a message when consent is not given
- Show the element that displays a message when consent is given
- Hide the cookie banner when the visitor wants to hide the accepted or rejected message

[!NOTE]
Tips:
1/ You can get analytics cookie consent status (true or false) by calling the function hasConsentForAnalytics:

```js
window.DI.analyticsGa4.cookie.hasConsentForAnalytics();
```

2/ You can revoke analytics cookie consent by calling the function setBannerCookieConsent:

```js
window.DI.analyticsGa4.cookie.setBannerCookieConsent(
  false,
  youranalyticsdomain,
);
```

[!NOTE]

### Page View Tracker

Page view tracking allows us to see which pages are most visited, where your visitors are coming from, etc.
It can be called by using the method trackOnPageLoad of the object pageViewTracker stored into the analytics library (analyticsGa4)

It takes as a unique parameter an object define by :

- statusCode (number): Status code of the page.
- englishPageTitle (string): English version of the page title.
- taxonomy_level1 (string): Taxonomies are hierarchical tool that allows us to filter data for reporting and insights purposes.
- taxonomy_level2 (string): Taxonomies are hierarchical tool that allows us to filter data for reporting and insights purposes.
- content_id (string): Content ID is a unique ID for each front end display on a given page.
- logged_in_status (boolean): Whether a user is logged in or logged out.
- dynamic (boolean): This parameter indicates whether the page has multiple versions and uses the same URL.

Example:

```js
window.DI.analyticsGa4.pageViewTracker.trackOnPageLoad({
  statusCode: 200,
  englishPageTitle: "english version of the page title",
  taxonomy_level1: "test tax1",
  taxonomy_level2: "test tax2",
  content_id: "<e4a3603d-2d3c-4ff1-9b80-d72c1e6b7a58>",
  logged_in_status: true,
  dynamic: true,
});
```

### Navigation Tracker

Navigation tracking allows us to see exactly how often each navigation link is used. It's triggered by a listener on the click event.

We are tracking different types of link:

- Generic Inbound Links: When a user clicks on a link and it is an inbound link which is defined as any links that point to a domain that does match the domain of the current page
- Generic Inbound Button: When a user clicks on a button and it is an inbound link which is defined as any links that point to a domain that does match the domain of the current page
- Generic Outbound Links: When a user clicks on a link and it is an outbound link, which is defined as any links that point to a domain that does not match the domain of the current page.
- Header Menu Bar: When a user clicks on a link in the header menu
- Footer links: When a user clicks on a link within the footer

[!NOTE] All links are automatically tracked. But if you need to track a button, your element needs to have a specific attributes "data-nav" and "data-link"(e.g: <button data-nav=true data-link="/next-page-url">Next</button>) [!NOTE]

### Form Response Tracker

Trigger by the submission of any form, this tracker will send to GA4 some data about the form details:

- Type of field
- Label of the field
- Submit Button text
- Value of the field

### Checkbox or Radio Fields Without a Legend

If a checkbox or radio field has been implemented without a legend, please follow these steps to ensure the tracker can retrieve the correct section value:

1. Add a `rel` attribute to the tag used to hold the section title.
2. Set the `rel` attribute value to match the `id` of the field.

**Example:**
(

<h2 rel="consentCheckbox">Section Title</h2>
<div class="govuk-form-group">
  <div class="govuk-checkboxes" data-module="govuk-checkboxes">
    <div class="govuk-checkboxes__item">
      <input id="consentCheckbox" name="consentCheckbox" type="checkbox" />
      <label id="consentCheckbox-label" for="consentCheckbox">
        Checkbox Label
      </label>
    </div>
  </div>
</div>)

### Form Change Tracker

Form Change Tracker is triggered when a user clicks on a link that allows them to change a previous form they had completed and loads the form page correctly. The URL needs to contain an edit parameter equal to true (example: /my-form-page?edit=true).
We are tracking the label of the field and the submit button text.

### Form Error Tracker

Form Error Tracker is triggered when a page loads and when the page displays any form errors.
We are tracking the label of the field and the error message.

### Checkbox or Radio Fields Without a Legend

If a checkbox or radio field has been implemented without a legend, please follow these steps to ensure the tracker can retrieve the correct section value:

1. Add a `rel` attribute to the tag used to hold the section title.
2. Set the `rel` attribute value to match the `id` of the field.

**Example:**
(

<h2 rel="consentCheckbox">Section Title</h2>
<div class="govuk-form-group">
  <div class="govuk-checkboxes" data-module="govuk-checkboxes">
    <div class="govuk-checkboxes__item">
      <input id="consentCheckbox" name="consentCheckbox" type="checkbox" />
      <label id="consentCheckbox-label" for="consentCheckbox">
        Checkbox Label
      </label>
    </div>
  </div>
</div>)

### Universal Analytics compability

More information:
https://govukverify.atlassian.net/wiki/spaces/DIFC/pages/3843227661/Universal+Analytics+compatibility

<p align="right">(<a href="#readme-top">back to top</a>)</p>

---
_Source: https://npm.io/package/@govuk-one-login/one-login-analytics · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
