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

Extend track click script #2263

Merged
merged 3 commits into from
Aug 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

# Unreleased

* Extend track click script ([PR #2263](https://github.com/alphagov/govuk_publishing_components/pull/2263))
* Fix cookie banner issue (IE10) ([PR #2231](https://github.com/alphagov/govuk_publishing_components/pull/2231))

# 25.2.3
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,26 @@ window.GOVUK.Modules = window.GOVUK.Modules || {};
GemTrackClick.prototype.start = function ($module) {
this.$module = $module[0]
this.$module.handleClick = this.handleClick.bind(this)
var trackLinksOnly = this.$module.hasAttribute('data-track-links-only')

var that = this
// add a listener to the whole element
this.$module.addEventListener('click', function (e) {
that.$module.handleClick(e.target)
if (!trackLinksOnly) {
that.$module.handleClick(e.target)
} else if (trackLinksOnly && e.target.tagName === 'A') {
that.$module.handleClick(e.target)
}
})
}

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]')
}

Expand All @@ -34,9 +41,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 || linkText

if (value) {
options.value = value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,54 @@ 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 = $(
'<div data-module="gem-track-click" data-track-category="cat1" data-track-action="action1">' +
'<a class="first" href="#">Link 1</a>' +
'<a class="second" href="#" ' +
'data-track-category="cat2"' +
'data-track-action="action2"' +
'data-track-label="label2">' +
'Link 2' +
'</a>' +
'</div>'
)

new GOVUK.Modules.GemTrackClick().start(element)

element.find('a.first')[0].click()
expect(GOVUK.analytics.trackEvent).toHaveBeenCalledWith('cat1', 'action1', { label: 'Link 1', transport: 'beacon' })

element.find('a.second')[0].click()
expect(GOVUK.analytics.trackEvent).toHaveBeenCalledWith('cat2', 'action2', { label: 'label2', transport: 'beacon' })
})

it('tracks only clicks on links when configured', function () {
element = $(
'<div data-module="gem-track-click" data-track-category="cat1" data-track-action="action1" data-track-links-only>' +
'<a class="first" href="#">Link 1</a>' +
'<a class="second" href="#" ' +
'data-track-category="cat2"' +
'data-track-action="action2"' +
'data-track-label="label2">' +
'Link 2' +
'</a>' +
'<span class="nothing"></span>' +
'</div>'
)

new GOVUK.Modules.GemTrackClick().start(element)

element.find('.nothing')[0].click()
expect(GOVUK.analytics.trackEvent).not.toHaveBeenCalled()

element.find('a.first')[0].click()
expect(GOVUK.analytics.trackEvent).toHaveBeenCalledWith('cat1', 'action1', { label: 'Link 1', transport: 'beacon' })

element.find('a.second')[0].click()
expect(GOVUK.analytics.trackEvent).toHaveBeenCalledWith('cat2', 'action2', { label: 'label2', transport: 'beacon' })
})

it('tracks a click correctly when event target is a child element of trackable element', function () {
element = $(
'<div data-module="gem-track-click">' +
Expand Down