Skip to content

Commit

Permalink
Merge pull request #466 from alphagov/remove-email-from-location
Browse files Browse the repository at this point in the history
Strip the user's email address out of the location used by GA
  • Loading branch information
brucebolt authored Jul 17, 2018
2 parents cf1c3fe + 9791be0 commit 2bc5813
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
10 changes: 10 additions & 0 deletions javascripts/govuk/analytics/google-analytics-universal-tracker.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
sendToGa('set', 'displayFeaturesTask', null)
}

function stripLocationPII () {
sendToGa('set', 'location', stripEmailAddressesFromString(window.location.href))
}

// Support legacy cookieDomain param
if (typeof fieldsObject === 'string') {
fieldsObject = { cookieDomain: fieldsObject }
Expand All @@ -28,6 +32,7 @@
configureProfile()
anonymizeIp()
disableAdTracking()
stripLocationPII()
}

GoogleAnalyticsUniversalTracker.load = function () {
Expand Down Expand Up @@ -160,6 +165,11 @@
}
}

function stripEmailAddressesFromString (string) {
var stripped = string.replace(/[^\s=/?&]+(?:@|%40)[^\s=/?&]+/g, '[email]')
return stripped
}

GOVUK.GoogleAnalyticsUniversalTracker = GoogleAnalyticsUniversalTracker

global.GOVUK = GOVUK
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,4 +177,11 @@ describe('GOVUK.GoogleAnalyticsUniversalTracker', function () {
expect(window.ga.calls.mostRecent().args).toEqual(['set', 'dimension1', '10'])
})
})

describe('when tracking all events', function () {
window.history.replaceState(null, null, '[email protected]')
it('removes any email address from the location', function () {
expect(window.ga.calls.mostRecent().args[2]).toContain('address=[email]')
})
})
})

0 comments on commit 2bc5813

Please sign in to comment.