diff --git a/CHANGELOG.md b/CHANGELOG.md index 318959bc80..3b3022e892 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ useful summary for people upgrading their application, not a replication of the commit log. +# Unreleased + +* Extend track click script ([PR #2263](https://github.com/alphagov/govuk_publishing_components/pull/2263)) + # 25.2.3 * Fix final issues with tracking on super nav header ([PR #2256](https://github.com/alphagov/govuk_publishing_components/pull/2256)) diff --git a/app/assets/javascripts/govuk_publishing_components/analytics/track-click.js b/app/assets/javascripts/govuk_publishing_components/analytics/track-click.js index 12d4ba7a53..553a4e5b89 100644 --- a/app/assets/javascripts/govuk_publishing_components/analytics/track-click.js +++ b/app/assets/javascripts/govuk_publishing_components/analytics/track-click.js @@ -19,9 +19,11 @@ window.GOVUK.Modules = window.GOVUK.Modules || {}; GemTrackClick.prototype.handleClick = function (target) { var options = { transport: 'beacon' } + var linkText // if clicked element hasn't got the right attributes, look for a parent that matches if (!target.hasAttribute('data-track-category') && !target.hasAttribute('data-track-action')) { + linkText = target.textContent || target.innerText target = target.closest('[data-track-category][data-track-action]') } @@ -34,9 +36,7 @@ window.GOVUK.Modules = window.GOVUK.Modules || {}; var dimensionIndex = target.getAttribute('data-track-dimension-index') var extraOptions = target.getAttribute('data-track-options') - if (label) { - options.label = label - } + options.label = label ? label : linkText if (value) { options.value = value diff --git a/spec/javascripts/govuk_publishing_components/analytics/track-click.spec.js b/spec/javascripts/govuk_publishing_components/analytics/track-click.spec.js index 26f8ae845c..273a886342 100644 --- a/spec/javascripts/govuk_publishing_components/analytics/track-click.spec.js +++ b/spec/javascripts/govuk_publishing_components/analytics/track-click.spec.js @@ -124,6 +124,28 @@ describe('A click tracker', function () { expect(GOVUK.analytics.trackEvent).toHaveBeenCalledWith('cat2', 'action2', { label: 'label2', transport: 'beacon' }) }) + it('tracks all links in a trackable container and uses the link text as the label if no label is specified', function () { + element = $( + '