Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,10 @@ For details on how feature flags are managed in Publisher, see the [feature flag

For instructions on how to add a new type of promotion to Completed Transactions, see the ["adding a promotion" documentation](docs/adding-a-promotion-to-a-completed-transaction.md).

### GA4 tracking

For details on the tracking that is applied to Publisher, see the [GA4 tracking documentation](docs/ga4-tracking.md).

## Further documentation

- [Fact Checking](docs/fact-checking.md)
Expand Down
28 changes: 28 additions & 0 deletions docs/ga4-tracking.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# GA4 tracking

Tracking is added to Publisher via GA4. The full documentation is avaialable in [GA4 Publishing Implementation guide](https://docs.google.com/document/d/19RaqZttDTZXsgX4k87wAAOhsol1STM3iTPPPKNlXRl0/edit?tab=t.0#heading=h.eo3rk3rxuz4n).

Via GA4 we are tracking:
- Page views
- Navigation
- Form interactions

Most of the code exists in the govuk_publishing_components gem. On publisher we are setting up various parameters on elements to make use of this. Our approach is to add these dynamically on page load via JavaScript set-up modules.

## Testing locally

Tracking can be tested locally with these steps:
- ensure that the "Ga4 form tracking" feature is set to "On" via FlipFlop
- run `document.querySelector('form').addEventListener('submit',function(e){e.preventDefault()})` in the browser console. This will disable form submissions so the full set of data from user interactions can be observed without leaving the page and saving any changes.
- run `window.GOVUK.analyticsGa4.showDebug = true` in the browser console. The console will then display the tracking data for all user interactions with the form as they occur.

Running `window.dataLayer` in the browser console after performing a save will display the data that has been collected during user interactions with the page. In this work we are interested in the "event_data" events. There should be one of these for each user event with the form since the page was loaded.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might be irrelevant but did you mention having to prevent a form from submitting at some point, in order to see the tracking fully? Just wondered if it would be worth adding that info here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's kind of an alternative to setting "Preserve log" in the console, and probably a better one because it means you can observe what data gets sent without actually saving. I've put that instead and also update the reference to the dataLayer later in a way that makes more sense in that context.

## Automated tests

There are:
- JavaScript Unit Tests for each set-up module:
- spec/javascripts/spec/ga4-form-setup.spec.js
- spec/javascripts/spec/ga4-index-section-setup.spec.js
- Rails integration tests for each page that uses the set-up modules:
- test/integration/ga4_tracking_test.rb
Loading