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

Allow custom options when tracking events #365

Merged
merged 2 commits into from
Jan 17, 2017
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
18 changes: 6 additions & 12 deletions javascripts/govuk/analytics/google-analytics-universal-tracker.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,7 @@
// Label is optional
if (typeof options.label === 'string') {
evt.eventLabel = options.label
}

// Page is optional
if (typeof options.page === 'string') {
evt.page = options.page
delete options.label
}

// Value is optional, but when used must be an
Expand All @@ -85,21 +81,19 @@
if (options.value || options.value === 0) {
value = parseInt(options.value, 10)
if (typeof value === 'number' && !isNaN(value)) {
evt.eventValue = value
options.eventValue = value
}
delete options.value
}

// Prevents an event from affecting bounce rate
// https://developers.google.com/analytics/devguides/collection/analyticsjs/events#implementation
if (options.nonInteraction) {
evt.nonInteraction = 1
options.nonInteraction = 1
}

// Set the transport method for the event
// Typically used for enabling `navigator.sendBeacon` when the page might be unloading
// https://developers.google.com/analytics/devguides/collection/analyticsjs/field-reference#transport
if (options.transport) {
evt.transport = options.transport
if (typeof options === 'object') {
$.extend(evt, options)
}

sendToGa('send', evt)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,13 @@ describe('GOVUK.GoogleAnalyticsUniversalTracker', function () {
)
})

it('tracks custom dimensions', function() {
universal.trackEvent('category', 'action', {dimension29: 'Home'})
expect(window.ga.calls.mostRecent().args).toEqual(
['send', {hitType: 'event', eventCategory: 'category', eventAction: 'action', dimension29: 'Home'}]
Copy link
Contributor

@fofr fofr Jan 12, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you confirmed that this object structure correctly passes the dimension to that event hit in GA?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've now tested this on dev with a forked Frontend Toolkit gem, and a WIP Static branch that sends custom dimensions. All looks ok.

)
})

it('the label is optional', function () {
universal.trackEvent('category', 'action')
expect(window.ga.calls.mostRecent().args).toEqual(
Expand Down