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..5c5b0ca77e 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]') } @@ -36,6 +38,8 @@ window.GOVUK.Modules = window.GOVUK.Modules || {}; if (label) { options.label = label + } else { + options.label = linkText } if (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 = $( + '