-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2864 from alphagov/ga4-schema-rework
GA4 analytics schema rework
- Loading branch information
Showing
9 changed files
with
314 additions
and
279 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
app/assets/javascripts/govuk_publishing_components/analytics-ga4.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
app/assets/javascripts/govuk_publishing_components/analytics-ga4/gtm-schemas.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
;(function (global) { | ||
'use strict' | ||
|
||
var GOVUK = global.GOVUK || {} | ||
|
||
var Schemas = function () { | ||
this.null = 'n/a' | ||
} | ||
|
||
Schemas.prototype.eventSchema = function () { | ||
return { | ||
event: this.null, | ||
|
||
event_data: { | ||
event_name: this.null, | ||
type: this.null, | ||
url: this.null, | ||
text: this.null, | ||
index: this.null, | ||
index_total: this.null, | ||
section: this.null, | ||
action: this.null, | ||
external: this.null | ||
} | ||
} | ||
} | ||
|
||
GOVUK.analyticsGA4 = GOVUK.analyticsGA4 || {} | ||
GOVUK.analyticsGA4.Schemas = Schemas | ||
|
||
global.GOVUK = GOVUK | ||
})(window) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# Google Analytics 4 and Google Tag Manager | ||
|
||
This document describes the use of Google Tag Manager (GTM) with Google Analytics 4 (GA4) on GOV.UK Publishing. | ||
|
||
No analytics code is initialised and no data is gathered without user consent. | ||
|
||
## General approach | ||
|
||
We pass three types of data to GA4. | ||
|
||
- page views | ||
- events | ||
- search data | ||
|
||
Page views happen when a page loads. | ||
|
||
Events happen when a user interacts with certain things, for example clicking on an accordion, tab, or link. | ||
|
||
Search data is gathered when users perform a search. | ||
|
||
## Data schemas | ||
|
||
All of the data sent to GTM is based on a common schema. | ||
|
||
``` | ||
{ | ||
event: '', | ||
page: {}, | ||
event_data: {}, | ||
search_results: {} | ||
} | ||
``` | ||
|
||
`event` must have a specific value to activate the right trigger in GTM. | ||
|
||
`page` is defined in the [gtm-page-views script](https://github.com/alphagov/govuk_publishing_components/blob/main/app/assets/javascripts/govuk_publishing_components/analytics-ga4/gtm-page-views.js). | ||
|
||
`event_data` is defined in the [gtm-schemas script](https://github.com/alphagov/govuk_publishing_components/blob/main/app/assets/javascripts/govuk_publishing_components/analytics-ga4/gtm-schemas.js) and used in the [gtm-click-tracking script](https://github.com/alphagov/govuk_publishing_components/blob/main/app/assets/javascripts/govuk_publishing_components/analytics-ga4/gtm-click-tracking.js). | ||
|
||
`search_results` has not been implemented yet. | ||
|
||
## How the dataLayer works | ||
|
||
GTM's dataLayer has two elements - an array and an object. `window.dataLayer = []` is executed when the page loads. | ||
|
||
GOV.UK JavaScript (JS) pushes objects to the dataLayer array when certain things happen e.g. when the page loads, or a user clicks on a certain type of link. Once that happens GTM takes over. It reads the latest object in the array and passes the data found into the dataLayer object. Importantly, it only adds to the object, so data can persist from previous array pushes. | ||
|
||
For example: | ||
|
||
- an event happens and JS pushes `{ a: 1 }` to the dataLayer array | ||
- GTM adds this to the dataLayer object, which is now `{ a: 1 }` | ||
- another event happens and JS pushes `{ b: 1 }` to the array | ||
- GTM adds this to the dataLayer object, which is now `{ a: 1, b: 1 }` | ||
|
||
If data shouldn't persist it can be erased in a following push, for example by sending `{ a: 1, b: false }`, but often this overall behaviour is desirable - for example, page view data will persist in events that happen on that page, providing more context for analysts. | ||
|
||
If the data given to GTM contains a recognised `event` attribute value, GTM then sends that data on to GA4. | ||
|
||
The dataLayer is recreated when a user navigates to another page, so no data in the dataLayer will persist between pages. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.