-
Notifications
You must be signed in to change notification settings - Fork 194
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
Conversation
dd23fb0
to
cc6728b
Compare
There was a problem hiding this 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' |
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Worth linking to https://github.com/alphagov/govuk_publishing_components/blob/2bf9077e3e091a2151feab56a715035e1883c689/app/assets/javascripts/govuk_publishing_components/analytics-ga4/ga4-link-tracker.js#L9 in the commit message to more clearly show why this is being updated?
cc6728b
to
fe8d18c
Compare
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.
fe8d18c
to
283d512
Compare
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) 🎉