Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow custom text for GA4 scroll tracker #3896

Merged
merged 1 commit into from Mar 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -9,6 +9,7 @@

## Unreleased

* Allow custom text for GA4 scroll tracker ([PR #3896](https://github.com/alphagov/govuk_publishing_components/pull/3896))
* Ensure analytics stops firing if a user disables usage cookies on our cookie settings page ([PR #3893](https://github.com/alphagov/govuk_publishing_components/pull/3893))

## 37.5.1
Expand Down
Expand Up @@ -164,7 +164,7 @@ window.GOVUK.Modules = window.GOVUK.Modules || {};
bottom: pos.bottom + document.documentElement.scrollTop,
eventData: {
type: type,
text: heading.textContent.replace(/\s+/g, ' ').trim(),
text: heading.getAttribute('data-ga4-text') || heading.textContent.replace(/\s+/g, ' ').trim(),
index_section: i + 1,
index_section_count: totalHeadings
}
Expand Down
10 changes: 9 additions & 1 deletion docs/analytics-ga4/ga4-scroll-tracker.md
Expand Up @@ -61,6 +61,14 @@ When tracking headings, the following data will be pushed to the dataLayer.
}
```

If you need custom text when tracking a specific heading, a data attribute can be added to the element for this purpose.

```HTML
<h2 data-ga4-text="this text will be recorded in the event">
This text will be ignored
</h2>
```

### Track markers

Sometimes only certain elements on a page should be tracked. This can be done by adding a marker to the required elements and configuring as shown.
Expand All @@ -81,7 +89,7 @@ Sometimes only certain elements on a page should be tracked. This can be done by

Any element that a marker is attached to should be short in both height and text content to be tracked well, because:

- the `text` attribute collects the full text of the element with the marker
- the `text` attribute collects the full text of the element with the marker (or the `data-ga4-text` attribute can be applied, as for heading tracking)
- scroll tracking only happens when the marked element is fully visible to the user

When tracking markers, the following data will be pushed to the dataLayer.
Expand Down
Expand Up @@ -80,7 +80,7 @@ describe('GA4 scroll tracker', function () {
var extremeHeight = window.innerHeight + 1000
var FIXTURE =
'<main style="position: absolute; top: 0px;">' +
'<h1>Heading 1</h1>' +
'<h1 data-ga4-text="Alternative heading 1 text">Heading 1</h1>' +
'<div style="height:' + extremeHeight + 'px">' +
'<h2 style="display: none;">Heading 2</h2>' +
'<h3 style="margin-top: ' + extremeHeight + 'px;">Heading 3</h3>' +
Expand All @@ -104,8 +104,8 @@ describe('GA4 scroll tracker', function () {
})

it('should send a tracking event on initialisation for headings that are already visible', function () {
expected.event_data.text = 'Heading 1'
expected.event_data.section = 'Heading 1'
expected.event_data.text = 'Alternative heading 1 text'
expected.event_data.section = 'Alternative heading 1 text'
expected.event_data.index.index_section = '1'
expect(window.dataLayer[0]).toEqual(expected)
})
Expand All @@ -118,8 +118,8 @@ describe('GA4 scroll tracker', function () {

it('should track headings on scroll and ignore already tracked headings', function () {
expect(window.dataLayer.length).toEqual(1)
expected.event_data.text = 'Heading 1'
expected.event_data.section = 'Heading 1'
expected.event_data.text = 'Alternative heading 1 text'
expected.event_data.section = 'Alternative heading 1 text'
expected.event_data.index.index_section = '1'
expect(window.dataLayer[0]).toEqual(expected)

Expand All @@ -137,8 +137,8 @@ describe('GA4 scroll tracker', function () {

it('should track newly visible headings on scroll and ignore already tracked headings', function () {
expect(window.dataLayer.length).toEqual(1)
expected.event_data.text = 'Heading 1'
expected.event_data.section = 'Heading 1'
expected.event_data.text = 'Alternative heading 1 text'
expected.event_data.section = 'Alternative heading 1 text'
expected.event_data.index.index_section = '1'
expect(window.dataLayer[0]).toEqual(expected)

Expand All @@ -156,8 +156,8 @@ describe('GA4 scroll tracker', function () {

it('should track when the body height changes', function () {
expect(window.dataLayer.length).toEqual(1)
expected.event_data.text = 'Heading 1'
expected.event_data.section = 'Heading 1'
expected.event_data.text = 'Alternative heading 1 text'
expected.event_data.section = 'Alternative heading 1 text'
expected.event_data.index.index_section = '1'
expect(window.dataLayer[0]).toEqual(expected)

Expand All @@ -177,8 +177,8 @@ describe('GA4 scroll tracker', function () {

it('should not track headings wrapped in ignored elements', function () {
expect(window.dataLayer.length).toEqual(1)
expected.event_data.text = 'Heading 1'
expected.event_data.section = 'Heading 1'
expected.event_data.text = 'Alternative heading 1 text'
expected.event_data.section = 'Alternative heading 1 text'
expected.event_data.index.index_section = '1'
expect(window.dataLayer[0]).toEqual(expected)

Expand Down Expand Up @@ -291,7 +291,7 @@ describe('GA4 scroll tracker', function () {
var extremeHeight = window.innerHeight + 1000
var FIXTURE =
'<main style="position: absolute; top: 0px;">' +
'<h1 data-ga4-scroll-marker>Heading 1</h1>' +
'<h1 data-ga4-scroll-marker data-ga4-text="Alternative heading 1 text">Heading 1</h1>' +
'<div style="height:' + extremeHeight + 'px">' +
'<h2 data-ga4-scroll-marker style="display: none;">Heading 2</h2>' +
'<h3 data-ga4-scroll-marker style="margin-top: ' + extremeHeight + 'px;">Heading 3</h3>' +
Expand All @@ -315,8 +315,8 @@ describe('GA4 scroll tracker', function () {
})

it('should send a tracking event on initialisation for headings that are already visible', function () {
expected.event_data.text = 'Heading 1'
expected.event_data.section = 'Heading 1'
expected.event_data.text = 'Alternative heading 1 text'
expected.event_data.section = 'Alternative heading 1 text'
expected.event_data.index.index_section = '1'
expect(window.dataLayer[0]).toEqual(expected)
})
Expand All @@ -329,8 +329,8 @@ describe('GA4 scroll tracker', function () {

it('should track headings on scroll and ignore already tracked headings', function () {
expect(window.dataLayer.length).toEqual(1)
expected.event_data.text = 'Heading 1'
expected.event_data.section = 'Heading 1'
expected.event_data.text = 'Alternative heading 1 text'
expected.event_data.section = 'Alternative heading 1 text'
expected.event_data.index.index_section = '1'
expect(window.dataLayer[0]).toEqual(expected)

Expand All @@ -348,8 +348,8 @@ describe('GA4 scroll tracker', function () {

it('should track newly visible headings on scroll and ignore already tracked headings', function () {
expect(window.dataLayer.length).toEqual(1)
expected.event_data.text = 'Heading 1'
expected.event_data.section = 'Heading 1'
expected.event_data.text = 'Alternative heading 1 text'
expected.event_data.section = 'Alternative heading 1 text'
expected.event_data.index.index_section = '1'
expect(window.dataLayer[0]).toEqual(expected)

Expand All @@ -367,8 +367,8 @@ describe('GA4 scroll tracker', function () {

it('should track when the body height changes', function () {
expect(window.dataLayer.length).toEqual(1)
expected.event_data.text = 'Heading 1'
expected.event_data.section = 'Heading 1'
expected.event_data.text = 'Alternative heading 1 text'
expected.event_data.section = 'Alternative heading 1 text'
expected.event_data.index.index_section = '1'
expect(window.dataLayer[0]).toEqual(expected)

Expand All @@ -388,8 +388,8 @@ describe('GA4 scroll tracker', function () {

it('should not track headings wrapped in ignored elements', function () {
expect(window.dataLayer.length).toEqual(1)
expected.event_data.text = 'Heading 1'
expected.event_data.section = 'Heading 1'
expected.event_data.text = 'Alternative heading 1 text'
expected.event_data.section = 'Alternative heading 1 text'
expected.event_data.index.index_section = '1'
expect(window.dataLayer[0]).toEqual(expected)

Expand Down