From 924c5b976e7e2e6f8fa1f8c15d7ae29492dac5d9 Mon Sep 17 00:00:00 2001 From: Andy Sellick Date: Thu, 12 Aug 2021 10:50:20 +0100 Subject: [PATCH] Modify track click script - link tracking without a label explicitly given should use the link href, not the text of the link --- CHANGELOG.md | 4 ++++ .../analytics/track-click.js | 10 ++++++---- .../analytics/track-click.spec.js | 14 +++++++------- 3 files changed, 17 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0f1405fa2c..0cee6fc376 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 + +* Fix track click link tracking ([PR #2265](https://github.com/alphagov/govuk_publishing_components/pull/2265)) + # 25.3.0 * Extend track click script ([PR #2263](https://github.com/alphagov/govuk_publishing_components/pull/2263)) 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 d1a8470e15..a03cb07a7f 100644 --- a/app/assets/javascripts/govuk_publishing_components/analytics/track-click.js +++ b/app/assets/javascripts/govuk_publishing_components/analytics/track-click.js @@ -24,24 +24,26 @@ window.GOVUK.Modules = window.GOVUK.Modules || {}; GemTrackClick.prototype.handleClick = function (target) { var options = { transport: 'beacon' } - var linkText + var linkUrl // 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 + linkUrl = target.getAttribute('href') target = target.closest('[data-track-category][data-track-action]') } if (target) { var category = target.getAttribute('data-track-category') var action = target.getAttribute('data-track-action') - var label = target.getAttribute('data-track-label') + var label = target.getAttribute('data-track-label') || linkUrl var value = target.getAttribute('data-track-value') var dimension = target.getAttribute('data-track-dimension') var dimensionIndex = target.getAttribute('data-track-dimension-index') var extraOptions = target.getAttribute('data-track-options') - options.label = label || linkText + if (label) { + options.label = label + } 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 dba8fb6534..312be7d14d 100644 --- a/spec/javascripts/govuk_publishing_components/analytics/track-click.spec.js +++ b/spec/javascripts/govuk_publishing_components/analytics/track-click.spec.js @@ -124,11 +124,11 @@ 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 () { + it('tracks all links in a trackable container and uses the link URL as the label if no label is specified', function () { element = $( '
' + - 'Link 1' + - 'Link 1' + + '' + @@ -140,7 +140,7 @@ describe('A click tracker', function () { new GOVUK.Modules.GemTrackClick().start(element) element.find('a.first')[0].click() - expect(GOVUK.analytics.trackEvent).toHaveBeenCalledWith('cat1', 'action1', { label: 'Link 1', transport: 'beacon' }) + expect(GOVUK.analytics.trackEvent).toHaveBeenCalledWith('cat1', 'action1', { label: '#link1', transport: 'beacon' }) element.find('a.second')[0].click() expect(GOVUK.analytics.trackEvent).toHaveBeenCalledWith('cat2', 'action2', { label: 'label2', transport: 'beacon' }) @@ -149,8 +149,8 @@ describe('A click tracker', function () { it('tracks only clicks on links when configured', function () { element = $( '
' + - 'Link 1' + - 'Link 1' + + '' + @@ -166,7 +166,7 @@ describe('A click tracker', function () { expect(GOVUK.analytics.trackEvent).not.toHaveBeenCalled() element.find('a.first')[0].click() - expect(GOVUK.analytics.trackEvent).toHaveBeenCalledWith('cat1', 'action1', { label: 'Link 1', transport: 'beacon' }) + expect(GOVUK.analytics.trackEvent).toHaveBeenCalledWith('cat1', 'action1', { label: '#link1', transport: 'beacon' }) element.find('a.second')[0].click() expect(GOVUK.analytics.trackEvent).toHaveBeenCalledWith('cat2', 'action2', { label: 'label2', transport: 'beacon' })