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

Support cross domain event tracking #472

Merged
merged 1 commit into from
Oct 3, 2018
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
24 changes: 18 additions & 6 deletions javascripts/govuk/analytics/google-analytics-universal-tracker.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
GoogleAnalyticsUniversalTracker.prototype.trackEvent = function (category, action, options) {
options = options || {}
var value
var trackerName = ''
var evt = {
hitType: 'event',
eventCategory: category,
Expand All @@ -97,6 +98,12 @@
delete options.value
}

// trackerName is optional
if (typeof options.trackerName === 'string') {
trackerName = options.trackerName + '.'
delete options.trackerName
}

// Prevents an event from affecting bounce rate
// https://developers.google.com/analytics/devguides/collection/analyticsjs/events#implementation
if (options.nonInteraction) {
Expand All @@ -107,7 +114,7 @@
$.extend(evt, options)
}

sendToGa('send', evt)
sendToGa(trackerName + 'send', evt)
}

/*
Expand All @@ -132,11 +139,13 @@

/*
https://developers.google.com/analytics/devguides/collection/analyticsjs/cross-domain
trackerId - the UA account code to track the domain against
name - name for the tracker
domain - the domain to track
trackerId - the UA account code to track the domain against
name - name for the tracker
domain - the domain to track
sendPageView - optional argument which controls the legacy behaviour of sending a pageview
on creation of the linked domain.
*/
GoogleAnalyticsUniversalTracker.prototype.addLinkedTrackerDomain = function (trackerId, name, domain) {
GoogleAnalyticsUniversalTracker.prototype.addLinkedTrackerDomain = function (trackerId, name, domain, sendPageView) {
sendToGa('create',
trackerId,
'auto',
Expand All @@ -151,7 +160,10 @@

sendToGa(name + '.set', 'anonymizeIp', true)
sendToGa(name + '.set', 'displayFeaturesTask', null)
sendToGa(name + '.send', 'pageview')

if (typeof sendPageView === 'undefined' || sendPageView === true) {
sendToGa(name + '.send', 'pageview')
}
}

// https://developers.google.com/analytics/devguides/collection/analyticsjs/custom-dims-mets
Expand Down
30 changes: 30 additions & 0 deletions spec/unit/analytics/google-analytics-universal-tracker.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,13 @@ describe('GOVUK.GoogleAnalyticsUniversalTracker', function () {
)
})

it('the option trackerName overrides the default tracker', function () {
universal.trackEvent('category', 'action', {trackerName: 'testTracker'})
expect(window.ga.calls.mostRecent().args).toEqual(
['testTracker.send', {hitType: 'event', eventCategory: 'category', eventAction: 'action'}]
)
})

it('only sends values if they are parseable as numbers', function () {
universal.trackEvent('category', 'action', {label: 'label', value: '10'})
expect(eventObjectFromSpy()['eventValue']).toEqual(10)
Expand Down Expand Up @@ -184,4 +191,27 @@ describe('GOVUK.GoogleAnalyticsUniversalTracker', function () {
expect(window.ga.calls.mostRecent().args[2]).toContain('address=[email]')
})
})

describe('adding a linked tracker', function () {
var callIndex

beforeEach(function () {
callIndex = window.ga.calls.count()
universal.addLinkedTrackerDomain('UA-123456', 'testTracker', 'some.service.gov.uk')
})
it('creates a tracker for the ID', function () {
expect(window.ga.calls.argsFor(callIndex)).toEqual(['create', 'UA-123456', 'auto', Object({ name: 'testTracker' })])
})
it('requires and configures the linker plugin', function () {
expect(window.ga.calls.argsFor(callIndex + 1)).toEqual(['require', 'linker'])
expect(window.ga.calls.argsFor(callIndex + 2)).toEqual(['testTracker.require', 'linker'])
})
it('sends a pageview', function () {
expect(window.ga.calls.mostRecent().args).toEqual(['testTracker.send', 'pageview'])
})
it('can omit sending a pageview', function () {
universal.addLinkedTrackerDomain('UA-123456', 'testTracker', 'some.service.gov.uk', false)
expect(window.ga.calls.mostRecent().args).not.toEqual(['testTracker.send', 'pageview'])
})
})
})