Skip to content

Commit

Permalink
Merge pull request #1832 from alphagov/improve-cross-domain-analytics…
Browse files Browse the repository at this point in the history
…-code

Update cross domain analytics code and extend tests
  • Loading branch information
andysellick authored Sep 2, 2019
2 parents 6a0d8a6 + a34ec73 commit e0dc110
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 17 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,6 @@ group :development, :test do
end

gem 'plek', '3.0.0'
gem 'govuk_frontend_toolkit', '~> 8.2.0'
gem 'govuk_frontend_toolkit', '~> 9.0.0'
gem 'govuk_template', '0.26.0'
gem 'gds-api-adapters', '~> 60.0'
6 changes: 3 additions & 3 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ GEM
sentry-raven (>= 2.7.1, < 2.12.0)
statsd-ruby (~> 1.4.0)
unicorn (>= 5.4, < 5.6)
govuk_frontend_toolkit (8.2.0)
govuk_frontend_toolkit (9.0.0)
railties (>= 3.1.0)
sass (>= 3.2.0)
govuk_publishing_components (17.21.0)
Expand Down Expand Up @@ -221,7 +221,7 @@ GEM
rails-dom-testing (2.0.3)
activesupport (>= 4.2.0)
nokogiri (>= 1.6)
rails-html-sanitizer (1.1.0)
rails-html-sanitizer (1.2.0)
loofah (~> 2.2, >= 2.2.2)
railties (5.2.3)
actionpack (= 5.2.3)
Expand Down Expand Up @@ -344,7 +344,7 @@ DEPENDENCIES
govuk-content-schema-test-helpers (~> 1.6)
govuk-lint (~> 3.11)
govuk_app_config (~> 1.20.2)
govuk_frontend_toolkit (~> 8.2.0)
govuk_frontend_toolkit (~> 9.0.0)
govuk_publishing_components (~> 17.21.0)
govuk_template (= 0.26.0)
govuk_test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@
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
domain - the domain to track (must be an array of strings)
sendPageView - optional argument which controls the legacy behaviour of sending a pageview
on creation of the linked domain.
*/
Expand All @@ -161,8 +161,8 @@
sendToGa(name + '.require', 'linker')

// Define which domains to autoLink.
sendToGa('linker:autoLink', [domain])
sendToGa(name + '.linker:autoLink', [domain])
sendToGa('linker:autoLink', domain)
sendToGa(name + '.linker:autoLink', domain)

sendToGa(name + '.set', 'anonymizeIp', true)
sendToGa(name + '.set', 'displayFeaturesTask', null)
Expand Down
2 changes: 2 additions & 0 deletions doc/analytics.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ for pages like:
GOVUK.analytics.addLinkedTrackerDomain(trackerIdHere, nameForTracker, domainToLinkTo);
```

Note that `domainToLinkTo` must be an array of strings.

Once this is done hits to that page will be tracked in both your local and the
named tracker, and sessions will persist to the other domain.

Expand Down
38 changes: 29 additions & 9 deletions spec/javascripts/analytics_toolkit/analytics.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,45 +99,37 @@ describe('GOVUK.Analytics', function () {

it('injects a default path if no args are supplied', function () {
analytics.trackPageview()
console.log(window.ga.calls.mostRecent().args)
expect(window.ga.calls.mostRecent().args[2].page).toEqual('/a/page?with=a&query=string')
})

it('injects a default path if args are supplied, but the path arg is blank', function () {
analytics.trackPageview(null)
console.log(window.ga.calls.mostRecent().args)
expect(window.ga.calls.mostRecent().args[2].page).toEqual('/a/page?with=a&query=string')

analytics.trackPageview(undefined)
console.log(window.ga.calls.mostRecent().args)
expect(window.ga.calls.mostRecent().args[2].page).toEqual('/a/page?with=a&query=string')
})

it('uses the supplied path', function () {
analytics.trackPageview('/foo')
console.log(window.ga.calls.mostRecent().args)
expect(window.ga.calls.mostRecent().args[2].page).toEqual('/foo')
})

it('does not inject a default title if no args are supplied', function () {
analytics.trackPageview()
console.log(window.ga.calls.mostRecent().args)
expect(window.ga.calls.mostRecent().args[2].title).toEqual(undefined)
})

it('does not inject a default title if args are supplied, but the title arg is blank', function () {
analytics.trackPageview('/foo', null)
console.log(window.ga.calls.mostRecent().args)
expect(window.ga.calls.mostRecent().args[2].title).toEqual(undefined)

analytics.trackPageview('/foo', undefined)
console.log(window.ga.calls.mostRecent().args)
expect(window.ga.calls.mostRecent().args[2].title).toEqual(undefined)
})

it('uses the supplied title', function () {
analytics.trackPageview('/foo', 'A page')
console.log(window.ga.calls.mostRecent().args)
expect(window.ga.calls.mostRecent().args[2].title).toEqual('A page')
})
})
Expand Down Expand Up @@ -387,7 +379,7 @@ describe('GOVUK.Analytics', function () {

describe('when adding a linked domain', function () {
it('adds a linked domain to universal analytics', function () {
analytics.addLinkedTrackerDomain('1234', 'test', 'www.example.com')
analytics.addLinkedTrackerDomain('1234', 'test', ['www.example.com'])

var allArgs = window.ga.calls.allArgs()
expect(allArgs).toContain(['create', '1234', 'auto', {'name': 'test'}])
Expand All @@ -399,5 +391,33 @@ describe('GOVUK.Analytics', function () {
expect(allArgs).toContain(['test.set', 'displayFeaturesTask', null])
expect(allArgs).toContain(['test.send', 'pageview'])
})

it('adds two linked domains to universal analytics', function () {
analytics.addLinkedTrackerDomain('5678', 'test2', ['www.example.com', 'www.something.com'])

var allArgs = window.ga.calls.allArgs()
expect(allArgs).toContain(['create', '5678', 'auto', {'name': 'test2'}])
expect(allArgs).toContain(['require', 'linker'])
expect(allArgs).toContain(['test2.require', 'linker'])
expect(allArgs).toContain(['linker:autoLink', ['www.example.com', 'www.something.com']])
expect(allArgs).toContain(['test2.linker:autoLink', ['www.example.com', 'www.something.com']])
expect(allArgs).toContain(['test2.set', 'anonymizeIp', true])
expect(allArgs).toContain(['test2.set', 'displayFeaturesTask', null])
expect(allArgs).toContain(['test2.send', 'pageview'])
})

it('adds multiple linked domains to universal analytics', function () {
analytics.addLinkedTrackerDomain('5678', 'test3', ['www.example.com', 'www.something.com', 'www.else.com'])

var allArgs = window.ga.calls.allArgs()
expect(allArgs).toContain(['create', '5678', 'auto', {'name': 'test3'}])
expect(allArgs).toContain(['require', 'linker'])
expect(allArgs).toContain(['test3.require', 'linker'])
expect(allArgs).toContain(['linker:autoLink', ['www.example.com', 'www.something.com', 'www.else.com']])
expect(allArgs).toContain(['test3.linker:autoLink', ['www.example.com', 'www.something.com', 'www.else.com']])
expect(allArgs).toContain(['test3.set', 'anonymizeIp', true])
expect(allArgs).toContain(['test3.set', 'displayFeaturesTask', null])
expect(allArgs).toContain(['test3.send', 'pageview'])
})
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ describe('GOVUK.GoogleAnalyticsUniversalTracker', function () {

beforeEach(function () {
callIndex = window.ga.calls.count()
universal.addLinkedTrackerDomain('UA-123456', 'testTracker', 'some.service.gov.uk')
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' })])
Expand All @@ -210,6 +210,10 @@ describe('GOVUK.GoogleAnalyticsUniversalTracker', function () {
expect(window.ga.calls.argsFor(callIndex + 1)).toEqual(['require', 'linker'])
expect(window.ga.calls.argsFor(callIndex + 2)).toEqual(['testTracker.require', 'linker'])
})
it('configures the domain', function () {
expect(window.ga.calls.argsFor(callIndex + 3)).toEqual(['linker:autoLink', ['some.service.gov.uk']])
expect(window.ga.calls.argsFor(callIndex + 4)).toEqual(['testTracker.linker:autoLink', ['some.service.gov.uk']])
})
it('sends a pageview', function () {
expect(window.ga.calls.mostRecent().args).toEqual(['testTracker.send', 'pageview'])
})
Expand All @@ -218,4 +222,50 @@ describe('GOVUK.GoogleAnalyticsUniversalTracker', function () {
expect(window.ga.calls.mostRecent().args).not.toEqual(['testTracker.send', 'pageview'])
})
})

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

beforeEach(function () {
callIndex = window.ga.calls.count()
universal.addLinkedTrackerDomain('UA-789', 'multiple', ['some.service.gov.uk', 'another.service.gov.uk'])
})
it('creates a tracker for the ID', function () {
expect(window.ga.calls.argsFor(callIndex)).toEqual(['create', 'UA-789', 'auto', Object({ name: 'multiple' })])
})
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(['multiple.require', 'linker'])
})
it('configures the domains', function () {
expect(window.ga.calls.argsFor(callIndex + 3)).toEqual(['linker:autoLink', ['some.service.gov.uk', 'another.service.gov.uk']])
expect(window.ga.calls.argsFor(callIndex + 4)).toEqual(['multiple.linker:autoLink', ['some.service.gov.uk', 'another.service.gov.uk']])
})
it('sends a pageview', function () {
expect(window.ga.calls.mostRecent().args).toEqual(['multiple.send', 'pageview'])
})
})

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

beforeEach(function () {
callIndex = window.ga.calls.count()
universal.addLinkedTrackerDomain('UA-987', 'multiple', ['some.service.gov.uk', 'another.service.gov.uk', 'third.service.gov.uk', 'fourth.service.gov.uk'])
})
it('creates a tracker for the ID', function () {
expect(window.ga.calls.argsFor(callIndex)).toEqual(['create', 'UA-987', 'auto', Object({ name: 'multiple' })])
})
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(['multiple.require', 'linker'])
})
it('configures the domains', function () {
expect(window.ga.calls.argsFor(callIndex + 3)).toEqual(['linker:autoLink', ['some.service.gov.uk', 'another.service.gov.uk', 'third.service.gov.uk', 'fourth.service.gov.uk']])
expect(window.ga.calls.argsFor(callIndex + 4)).toEqual(['multiple.linker:autoLink', ['some.service.gov.uk', 'another.service.gov.uk', 'third.service.gov.uk', 'fourth.service.gov.uk']])
})
it('sends a pageview', function () {
expect(window.ga.calls.mostRecent().args).toEqual(['multiple.send', 'pageview'])
})
})
})

0 comments on commit e0dc110

Please sign in to comment.