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

Track all links and buttons in Whitehall as per agreed event schema #9651

Merged
merged 6 commits into from
Dec 6, 2024

Conversation

ryanb-gds
Copy link
Contributor

There are a few things going on here:

We are preventing button tracking for elements that are not buttons but have the button role because there is a risk of duplicate events. Most non-button elements with the button role are links, and will therefore be tracked via the link tracker module. The link tracker module will send a link event type for links with the button role. We do not need to send additional events via button tracking for such elements.

There was a hardcoded "method" attribute which we unset, as we don't know whether it's actually a primary click or not. The link tracker from Publishing components does apply this attribute.

The link tracking setup was using the wrong data object, "event" instead of "link", so that's been fixed.

Finally we move the data module initialisation for links and buttons higher up the DOM tree so that tracking is applied globally(ish) 🎉

@ryanb-gds ryanb-gds force-pushed the track-all-links-and-buttons branch from dd23fb0 to cc6728b Compare November 26, 2024 15:18
Copy link
Contributor

@ChrisBAshton ChrisBAshton left a comment

Choose a reason for hiding this comment

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

LGTM 👍 Nitpick suggestions below

@@ -10,15 +10,12 @@ window.GOVUK.analyticsGa4.analyticsModules =
"[data-module~='ga4-button-setup']"
)
moduleElements.forEach(function (moduleElement) {
const buttons = moduleElement.querySelectorAll(
'button'
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: this reduction to one line should be fixed up to the first commit.

And the second commit should explain what you've explained in the PR description:

There was a hardcoded "method" attribute which we unset, as we don't know whether it's actually a primary click or not. The link tracker from Publishing components does apply this attribute.

(NB, that last sentence reads a bit confusingly - I'd suggest rewording along the lines of "the link tracker from Publishing components already applies the attribute, so we don't need to repeat it here")

@@ -16,10 +16,10 @@ window.GOVUK.analyticsGa4.analyticsModules =
event_name: 'navigation',
type: link.role === 'button' ? 'button' : 'generic_link'
}
if (link.dataset.ga4Event) {
Object.assign(event, JSON.parse(link.dataset.ga4Event))
if (link.dataset.ga4Link) {
Copy link
Contributor

Choose a reason for hiding this comment

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

@ryanb-gds ryanb-gds force-pushed the track-all-links-and-buttons branch from cc6728b to fe8d18c Compare November 28, 2024 10:53
We are preventing button tracking for elements that are not buttons but have the button role because there is a risk of duplicate events.

Most non-button elements with the button role are links, and will therefore be tracked via the link tracker module. The link tracker module will send a link event type for links with the button role. We do not need to send additional events via button tracking for such elements.
…click events

There was a hardcoded "method" attribute which we unset, as we don't know whether it's actually a primary click or not. The link tracker from Publishing components already applies this attribute anyway, so no need for us to repeat it.
Link tracking should use the ga4-link data attribute instead of the ga4-event data attribute. See https://github.com/alphagov/govuk_publishing_components/blob/2bf9077e3e091a2151feab56a715035e1883c689/app/assets/javascripts/govuk_publishing_components/analytics-ga4/ga4-link-tracker.js#L9

This will allow us to mix the attributes in to the GA4 link tracking module events provided by GOV.UK Publishing Components
…m submission

It doesn't make sense to track a "navigation" event for such buttons, as there is no form submission to trigger a page navigation
Tabs have their own tracking built in to the component from the govuk_publishing_components gem. No need for us to duplicate it.
@ryanb-gds ryanb-gds force-pushed the track-all-links-and-buttons branch from fe8d18c to 283d512 Compare December 6, 2024 13:47
@ryanb-gds ryanb-gds merged commit 54f6ad0 into main Dec 6, 2024
19 checks passed
@ryanb-gds ryanb-gds deleted the track-all-links-and-buttons branch December 6, 2024 13:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants