Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1187 from alphagov/add-tracking-details-component
Allow tracking of the details component
- Loading branch information
Showing
6 changed files
with
159 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
app/assets/javascripts/govuk_publishing_components/components/details.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* eslint-env jquery */ | ||
// = require govuk/components/details/details.js | ||
window.GOVUK = window.GOVUK || {} | ||
window.GOVUK.Modules = window.GOVUK.Modules || {}; | ||
|
||
(function (Modules) { | ||
'use strict' | ||
|
||
Modules.GovukDetails = function () { | ||
this.start = function (element) { | ||
var customTrackLabel = element[0].getAttribute('data-track-label') | ||
|
||
// If a custom label has been provided, we can simply call the tracking module | ||
if (customTrackLabel) { | ||
var trackDetails = new window.GOVUK.Modules.TrackClick() | ||
trackDetails.start(element) | ||
} else { | ||
// If no custom label is set, we use the open/close status as the label | ||
var detailsComponent = element[0] | ||
var detailsClick = detailsComponent.querySelector('[data-details-track-click]') | ||
var that = this | ||
|
||
$(detailsClick).click(function (e) { | ||
that.trackDefault(detailsComponent) | ||
}) | ||
} | ||
} | ||
|
||
this.trackDefault = function (element) { | ||
if (window.GOVUK.analytics && window.GOVUK.analytics.trackEvent) { | ||
var componentStatus = (element.getAttribute('open') == null) ? 'open' : 'closed' | ||
var trackCategory = element.getAttribute('data-track-category') | ||
var trackAction = element.getAttribute('data-track-action') | ||
var trackOptions = element.getAttribute('data-track-options') | ||
|
||
if (typeof trackOptions !== 'object' || trackOptions === null) { | ||
trackOptions = {} | ||
} | ||
|
||
trackOptions['label'] = componentStatus | ||
|
||
if (trackAction && trackCategory) { | ||
window.GOVUK.analytics.trackEvent(trackCategory, trackAction, trackOptions) | ||
} | ||
} | ||
} | ||
} | ||
})(window.GOVUK.Modules) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* eslint-env jasmine, jquery */ | ||
/* global GOVUK */ | ||
|
||
describe('Details component', function () { | ||
var FIXTURE | ||
|
||
GOVUK.analytics = { | ||
trackEvent: function () {} | ||
} | ||
|
||
var callback = jasmine.createSpy() | ||
GOVUK.Modules.TrackClick = function () { | ||
this.start = function () { | ||
callback() | ||
} | ||
} | ||
|
||
function loadDetailsComponent () { | ||
var details = new GOVUK.Modules.GovukDetails() | ||
details.start($('.gem-c-details')) | ||
} | ||
|
||
beforeEach(function () { | ||
spyOn(GOVUK.analytics, 'trackEvent') | ||
|
||
FIXTURE = | ||
'<details class="gem-c-details govuk-details govuk-!-margin-bottom-3" data-track-category="track-category" data-track-action="track-action" data-track-label="track-label" data-module="govuk-details">' + | ||
'<summary class="govuk-details__summary" data-details-track-click="">' + | ||
'<span>Toggle text</span>' + | ||
'</summary>' + | ||
'</details>' | ||
|
||
window.setFixtures(FIXTURE) | ||
}) | ||
|
||
afterEach(function () { | ||
GOVUK.analytics.trackEvent.calls.reset() | ||
}) | ||
|
||
it('uses built in tracking module when provided with a track-label', function () { | ||
loadDetailsComponent() | ||
|
||
$('.govuk-details__summary').click() | ||
|
||
expect(GOVUK.analytics.trackEvent.calls.count()).toEqual(0) | ||
expect(callback).toHaveBeenCalled() | ||
}) | ||
|
||
it('does not fire an event if track category and track action are not present', function () { | ||
$('.gem-c-details').attr('data-track-action', null) | ||
$('.gem-c-details').attr('data-track-category', null) | ||
$('.gem-c-details').attr('data-track-label', null) | ||
|
||
loadDetailsComponent() | ||
|
||
$('.govuk-details__summary').click() | ||
|
||
expect(GOVUK.analytics.trackEvent.calls.count()).toEqual(0) | ||
}) | ||
|
||
it('tracks open state by default if no track label provided', function () { | ||
$('.gem-c-details').attr('data-track-label', null) | ||
loadDetailsComponent() | ||
|
||
$('.govuk-details__summary').click() | ||
|
||
expect(GOVUK.analytics.trackEvent).toHaveBeenCalledWith('track-category', 'track-action', { label: 'open' }) | ||
}) | ||
|
||
it('tracks closed state by default if no track label provided', function () { | ||
$('.gem-c-details').attr('data-track-label', null) | ||
$('.gem-c-details').attr('open', true) | ||
loadDetailsComponent() | ||
|
||
$('.govuk-details__summary').click() | ||
|
||
expect(GOVUK.analytics.trackEvent).toHaveBeenCalledWith('track-category', 'track-action', { label: 'closed' }) | ||
}) | ||
}) |