diff --git a/README.md b/README.md index 57a299dab..f4f7837c7 100644 --- a/README.md +++ b/README.md @@ -39,7 +39,7 @@ Background information: - [How Slimmer and Static work together](https://docs.publishing.service.gov.uk/repos/slimmer/what-slimmer-does.html) - [Frontend architecture and long term plan to remove Static / Slimmer](https://docs.publishing.service.gov.uk/manual/frontend-architecture.html) -- [Analytics (no longer in Static, but imported from govuk_publishing_components)](docs/analytics.md) +- [Analytics (no longer in Static, but handled by govuk_publishing_components)](https://github.com/alphagov/govuk_publishing_components/blob/main/docs/analytics-ga4/analytics.md) How to's: diff --git a/app/assets/javascripts/modules/global-bar.js b/app/assets/javascripts/modules/global-bar.js index ccd1179c0..b39fd37ce 100644 --- a/app/assets/javascripts/modules/global-bar.js +++ b/app/assets/javascripts/modules/global-bar.js @@ -40,8 +40,6 @@ window.GOVUK.Modules = window.GOVUK.Modules || {}; var target = e.target if (target.classList.contains('dismiss')) { hide(e) - } else if (target.classList.contains('js-call-to-action')) { - handleCallToActionClick(target) } }) @@ -50,11 +48,6 @@ window.GOVUK.Modules = window.GOVUK.Modules || {}; incrementViewCount(count) } - function handleCallToActionClick (target) { - var url = target.getAttribute('href') - track(url) - } - function hide (event) { var currentCookie = parseCookie(GOVUK.getCookie(GLOBAL_BAR_SEEN_COOKIE)) var cookieVersion = currentCookieVersion @@ -73,7 +66,6 @@ window.GOVUK.Modules = window.GOVUK.Modules || {}; if (dismiss) { dismiss.classList.remove('global-bar__dismiss--show') } - track('Manually dismissed') event.preventDefault() } @@ -81,10 +73,6 @@ window.GOVUK.Modules = window.GOVUK.Modules || {}; count = count + 1 var cookieValue = JSON.stringify({ count: count, version: currentCookieVersion }) GOVUK.setCookie(GLOBAL_BAR_SEEN_COOKIE, cookieValue, { days: 84 }) - - if (count === 2) { - track('Automatically dismissed') - } } function viewCount () { @@ -97,12 +85,6 @@ window.GOVUK.Modules = window.GOVUK.Modules || {}; return viewCount } - - function track (action) { - if (GOVUK.analytics && typeof GOVUK.analytics.trackEvent === 'function') { - GOVUK.analytics.trackEvent('Global bar', action, { nonInteraction: 1 }) - } - } } Modules.GlobalBar = GlobalBar diff --git a/app/assets/javascripts/surveys.js b/app/assets/javascripts/surveys.js index 091e259e9..897d1b6ea 100644 --- a/app/assets/javascripts/surveys.js +++ b/app/assets/javascripts/surveys.js @@ -59,7 +59,6 @@ ' ' + ' ' + ' ' + - ' ' + ' ' + ' ' + takeSurveyLink('{{surveyFormNoEmailInvite}}') + @@ -153,8 +152,7 @@ surveyFailure: 'Sorry, we’re unable to send you an email right now. Please try again later.', surveyId: survey.identifier, surveySource: userSurveys.currentPath(), - surveyUrl: userSurveys.addParamsToURL(userSurveys.getSurveyUrl(survey)), - gaClientId: GOVUK.analytics.gaClientId + surveyUrl: userSurveys.addParamsToURL(userSurveys.getSurveyUrl(survey)) } var mergedArgs = window.GOVUK.extendObject(defaultEmailArgs, survey.templateArgs) return userSurveys.processTemplate(mergedArgs, EMAIL_SURVEY_TEMPLATE) @@ -203,7 +201,6 @@ return } userSurveys.incrementSurveySeenCounter(survey) - userSurveys.trackEvent(survey.identifier, 'banner_shown', 'Banner has been shown') }, displayURLSurvey: function (survey, surveyContainer) { @@ -222,11 +219,7 @@ addParamsToURL: function (surveyUrl) { var newSurveyUrl = surveyUrl.replace(/\{\{currentPath\}\}/g, userSurveys.currentPath()) - if (surveyUrl.indexOf('?c=') !== -1) { - return newSurveyUrl + '&gcl=' + GOVUK.analytics.gaClientId - } else { - return newSurveyUrl + '?gcl=' + GOVUK.analytics.gaClientId - } + return newSurveyUrl }, setEmailSurveyEventHandlers: function (survey) { @@ -243,7 +236,6 @@ $takeSurvey.addEventListener('click', function () { userSurveys.setSurveyTakenCookie(survey) userSurveys.hideSurvey(survey) - userSurveys.trackEvent(survey.identifier, 'no_email_link', 'User taken survey via no email link') }) } @@ -251,7 +243,6 @@ $emailSurveyOpen.addEventListener('click', function (e) { e.preventDefault() // otherwise focus doesn't work properly and screen jumps to form survey.surveyExpanded = true - userSurveys.trackEvent(survey.identifier, 'email_survey_open', 'Email survey opened') $emailSurveyPre.classList.add('js-hidden') $emailSurveyPre.setAttribute('aria-hidden', 'true') $emailSurveyForm.classList.remove('js-hidden') @@ -265,11 +256,6 @@ $emailSurveyCancel.addEventListener('click', function (e) { userSurveys.setSurveyTakenCookie(survey) userSurveys.hideSurvey(survey) - if (survey.surveyExpanded) { - userSurveys.trackEvent(survey.identifier, 'email_survey_cancel', 'Email survey cancelled') - } else { - userSurveys.trackEvent(survey.identifier, 'banner_no_thanks', 'No thanks clicked') - } e.stopPropagation() e.preventDefault() }) @@ -284,8 +270,6 @@ $emailSurveyPostSuccess.setAttribute('aria-hidden', 'false') $emailSurveyPostSuccess.focus() userSurveys.setSurveyTakenCookie(survey) - userSurveys.trackEvent(survey.identifier, 'email_survey_taken', 'Email survey taken') - userSurveys.trackEvent(survey.identifier, 'banner_taken', 'User taken survey') } var errorCallback = function () { $emailSurveyForm.classList.add('js-hidden') @@ -352,7 +336,6 @@ $emailSurveyCancel.addEventListener('click', function (e) { userSurveys.setSurveyTakenCookie(survey) userSurveys.hideSurvey(survey) - userSurveys.trackEvent(survey.identifier, 'banner_no_thanks', 'No thanks clicked') e.stopPropagation() e.preventDefault() }) @@ -362,7 +345,6 @@ $takeSurvey.addEventListener('click', function () { userSurveys.setSurveyTakenCookie(survey) userSurveys.hideSurvey(survey) - userSurveys.trackEvent(survey.identifier, 'banner_taken', 'User taken survey') }) } }, @@ -405,14 +387,6 @@ } }, - trackEvent: function (identifier, action, label) { - window.GOVUK.analytics.trackEvent(identifier, action, { - label: label, - value: 1, - nonInteraction: true - }) - }, - setSurveyTakenCookie: function (survey) { window.GOVUK.cookie(userSurveys.surveyTakenCookieName(survey), true, { days: 30 * 3 }) }, @@ -634,6 +608,19 @@ var cookieVersion = parseFloat(tlsCookie.replace('TLSv', '')) return cookieVersion || '' } + }, + + startModule: function () { + if (GOVUK.userSurveys) { + var consentCookie = window.GOVUK.getConsentCookie() + + if (consentCookie && consentCookie.usage) { + window.GOVUK.userSurveys.init() + } else { + this.start = this.startModule.bind(this) + window.addEventListener('cookie-consent', this.start) + } + } } } @@ -655,14 +642,5 @@ } window.GOVUK.userSurveys = userSurveys - - if (GOVUK.userSurveys) { - if (GOVUK.analytics && GOVUK.analytics.gaClientId) { - window.GOVUK.userSurveys.init() - } else { - window.addEventListener('gaClientSet', function () { - window.GOVUK.userSurveys.init() - }) - } - } + window.GOVUK.userSurveys.startModule() })() diff --git a/docs/analytics.md b/docs/analytics.md deleted file mode 100644 index 62835de38..000000000 --- a/docs/analytics.md +++ /dev/null @@ -1,20 +0,0 @@ -# Analytics - -The analytics code for GOV.UK (including the documentation) has now been migrated to [govuk_publishing_components](https://github.com/alphagov/govuk_publishing_components/blob/master/docs/analytics.md) and is now pulled into static from there. - -## Adding domains to GOV.UK's cross domain linker - -Once a service starts sending analytics data to the shared GA property and has configured their system to include `www.gov.uk` in their cross domain linking, we must also configure `www.gov.uk` to include their domain in our cross linker configuration. - -Add the domain of the service to the `linkedDomains` var in [`analytics.js.erb`](https://github.com/alphagov/static/blob/master/app/assets/javascripts/analytics.js.erb). - -Only the domain should be added, GA will ignore any path details e.g. `/government/service/` - -Note that GA matches subdomains of any domains added this way. So `passport.service.gov.uk` also includes `www.passport.service.gov.uk`. - -On that basis the simplest course of action would appear to be to simply add `gov.uk` or even `service.gov.uk` and then it'll just match everything we want it to. **We must not do this** for very good reasons: - -- any link from `www.gov.uk` to a matching domain would have a URL parameter automatically added to it e.g. `https://something.service.gov.uk/?_ga=23.32423234.213.1.2213`. This could interfere with that site's own analytics. -- the client ID might be reset. - -Once the changes to the linker configuration have been deployed a performance analyst will need to check that everything is working correctly. Since static PRs can take upwards of half an hour to pass CI tests, it's worth having a revert PR ready early on just in case. diff --git a/docs/surveys.md b/docs/surveys.md index 79abf1e26..0de698871 100644 --- a/docs/surveys.md +++ b/docs/surveys.md @@ -35,7 +35,7 @@ Once a user takes the survey (clicks the link or fills in their email address an ## About the data structure ### `identifier` -This is used both as the Google Analytics tag to record events against, and used (after being transformed to camelCase) to set a cookie so a visitor is not prompted to take the survey more than once. +This is used (after being transformed to camelCase) to set a cookie so a visitor is not prompted to take the survey more than once. It was also previously used as a tag to record events against in old analytics code. ### `frequency` How frequently to show the survey. A frequency of `1` means the survey shows to every visitor. A frequency of `50` means the survey shows to 1 in 50 visitors. @@ -51,8 +51,6 @@ Used in a link in the survey that the user is directed to click on. This should * `https://www.smartsurvey.com/s/2AAAAAA` - it will be left alone and inserted in the template as-is. * `https://www.smartsurvey.com/s/2AAAAAA?c={{currentPath}}` - will be transformed into `https://www.smartsurvey.com/s/2AAAAAA?c=/government/publications/the-kingdom-of-the-crystal-skull` (assuming the page the survey was shown on was https://www.gov.uk/government/publications/the-kindgom-of-the-crystal-skull). -Please be aware that the GA client ID will also be appended to the end of the url, so the final result will be: `https://www.smartsurvey.com/s/2AAAAAA?c=/government/publications/the-kingdom-of-the-crystal-skull&gcl=12345.67890`. -If the `?c={{currentPath}}` template param is missing from the link, then the resulting url will be: `https://www.smartsurvey.com/s/2AAAAAA?gcl=12345.67890`. The value can be an array of URLs, in which case the URL to use will be chosen randomly from the array. @@ -128,7 +126,6 @@ The template for a url survey is as follows: - {{surveyFormNoEmailInvite}} diff --git a/spec/javascripts/helpers/SpecHelper.js b/spec/javascripts/helpers/SpecHelper.js index 34c0cbd91..c2f958b56 100644 --- a/spec/javascripts/helpers/SpecHelper.js +++ b/spec/javascripts/helpers/SpecHelper.js @@ -1,5 +1,4 @@ beforeEach(function () { - window.GOVUK.loadAnalytics.loadUa() jasmine.addMatchers({ toBeEqualAsJSON: function (util, customEqualityTesters) { return { diff --git a/spec/javascripts/surveys.spec.js b/spec/javascripts/surveys.spec.js index 1491799ae..161cd1c43 100644 --- a/spec/javascripts/surveys.spec.js +++ b/spec/javascripts/surveys.spec.js @@ -42,11 +42,6 @@ describe('Surveys', function () { e.preventDefault() } }) - - GOVUK.analytics = { - trackEvent: function () {}, - gaClientId: '12345.67890' - } }) afterEach(function () { @@ -81,6 +76,24 @@ describe('Surveys', function () { }) }) + describe('cookie consent', function () { + it('starts the JS if the usage cookie is true', function () { + window.GOVUK.setConsentCookie({ usage: true }) + + spyOn(surveys, 'init') + surveys.startModule() + expect(surveys.init).toHaveBeenCalled() + }) + + it('does not start the JS if the usage cookie is not true', function () { + window.GOVUK.setConsentCookie({ usage: false }) + + spyOn(surveys, 'init') + surveys.startModule() + expect(surveys.init).not.toHaveBeenCalled() + }) + }) + describe('displaySurvey', function () { it('displays the user satisfaction div', function () { expect($('#user-satisfaction-survey').length).toBe(0) @@ -134,12 +147,6 @@ describe('Surveys', function () { }) describe("for a 'url' survey", function () { - it('records an event when showing the survey', function () { - spyOn(surveys, 'trackEvent') - surveys.displaySurvey(urlSurvey) - expect(surveys.trackEvent).toHaveBeenCalledWith(urlSurvey.identifier, 'banner_shown', 'Banner has been shown') - }) - it('sets event handlers on the survey', function () { spyOn(surveys, 'setURLSurveyEventHandlers') surveys.displaySurvey(urlSurvey) @@ -160,7 +167,7 @@ describe('Surveys', function () { it('does not inject the current path if the survey url does not contain the {{currentPath}} template parameter', function () { surveys.displaySurvey(urlSurvey) - var expectedUrl = urlSurvey.url + '?gcl=' + GOVUK.analytics.gaClientId + var expectedUrl = urlSurvey.url expect($('#take-survey').attr('href')).toEqual(expectedUrl) }) @@ -234,24 +241,12 @@ describe('Surveys', function () { expect($('#email-survey-form input[name="email_survey_signup[survey_source]"]').val()).toEqual(window.location.pathname) }) - it('adds the GA client ID to the form', function () { - surveys.displaySurvey(emailSurvey) - - expect($('#email-survey-form input[name="email_survey_signup[ga_client_id]"]').val()).toEqual(GOVUK.analytics.gaClientId) - }) - it('sets event handlers on the survey', function () { spyOn(surveys, 'setEmailSurveyEventHandlers') surveys.displaySurvey(emailSurvey) expect(surveys.setEmailSurveyEventHandlers).toHaveBeenCalledWith(emailSurvey) }) - it('records an event when showing the survey', function () { - spyOn(surveys, 'trackEvent') - surveys.displaySurvey(emailSurvey) - expect(surveys.trackEvent).toHaveBeenCalledWith(emailSurvey.identifier, 'banner_shown', 'Banner has been shown') - }) - it('replaces the current path if the survey url contains the {{currentPath}} template parameter', function () { var emailSurveyWithCurrentPath = { surveyType: 'email', @@ -269,23 +264,6 @@ describe('Surveys', function () { expect($('#take-survey').attr('href')).not.toContain('?c=' + window.location.pathname) }) - it('adds the GA client ID to the survey url if the {{currentPath}} template parameter is present', function () { - var emailSurveyWithCurrentPath = { - surveyType: 'email', - url: 'smartsurvey.com/default?c={{currentPath}}', - identifier: 'email-survey' - } - surveys.displaySurvey(emailSurveyWithCurrentPath) - - expect($('#take-survey').attr('href')).toContain('?c=' + window.location.pathname + '&gcl=' + GOVUK.analytics.gaClientId) - }) - - it('adds the GA client ID to the survey url if the {{currentPath}} template parameter is missing', function () { - surveys.displaySurvey(emailSurvey) - - expect($('#take-survey').attr('href')).toEqual(emailSurvey.url + '?gcl=' + GOVUK.analytics.gaClientId) - }) - describe('without overrides for the template defaults', function () { it('uses the default title', function () { surveys.displaySurvey(emailSurvey) @@ -732,18 +710,6 @@ describe('Surveys', function () { $('#user-survey-cancel')[0].click() expect($('#user-satisfaction-survey').hasClass('visible')).toBe(false) }) - - it("records an event when clicking 'take survey'", function () { - spyOn(surveys, 'trackEvent') - $('#take-survey')[0].click() - expect(surveys.trackEvent).toHaveBeenCalledWith(defaultSurvey.identifier, 'banner_taken', 'User taken survey') - }) - - it("records an event when clicking 'no thanks'", function () { - spyOn(surveys, 'trackEvent') - $('#user-survey-cancel')[0].click() - expect(surveys.trackEvent).toHaveBeenCalledWith(defaultSurvey.identifier, 'banner_no_thanks', 'No thanks clicked') - }) }) describe('for an email survey', function () { @@ -767,13 +733,6 @@ describe('Surveys', function () { expect($('#user-satisfaction-survey').hasClass('visible')).toBe(false) }) - it("records an event when clicking 'no thanks'", function () { - spyOn(surveys, 'trackEvent') - emailSurvey.surveyExpanded = false - $('#user-survey-cancel')[0].click() - expect(surveys.trackEvent).toHaveBeenCalledWith(emailSurvey.identifier, 'banner_no_thanks', 'No thanks clicked') - }) - it("opens the email form when clicking on 'Your feedback will help us ...'", function () { $('#email-survey-open')[0].click() expect($('#email-survey-form').hasClass('js-hidden')).toBe(false) @@ -784,12 +743,6 @@ describe('Surveys', function () { expect($('#email-survey-pre').hasClass('js-hidden')).toBe(true) }) - it("records an event when clicking on 'Your feedback will help us ...'", function () { - spyOn(surveys, 'trackEvent') - $('#email-survey-open')[0].click() - expect(surveys.trackEvent).toHaveBeenCalledWith(emailSurvey.identifier, 'email_survey_open', 'Email survey opened') - }) - describe('once the email form is opened', function () { beforeEach(function () { jasmine.Ajax.install() @@ -836,13 +789,6 @@ describe('Surveys', function () { expect(GOVUK.cookie(surveys.surveyTakenCookieName(emailSurvey))).toBe('true') }) - it('records an event', function () { - spyOn(surveys, 'trackEvent') - window.GOVUK.triggerEvent($('#email-survey-form')[0], 'submit') - expect(surveys.trackEvent).toHaveBeenCalledWith(emailSurvey.identifier, 'email_survey_taken', 'Email survey taken') - expect(surveys.trackEvent).toHaveBeenCalledWith(emailSurvey.identifier, 'banner_taken', 'User taken survey') - }) - it('adds the GA4 form complete ', function () { window.GOVUK.triggerEvent($('#email-survey-form')[0], 'submit') // For some reason the submit event triggers twice in tests, so we have to get the second . @@ -883,12 +829,6 @@ describe('Surveys', function () { expect(GOVUK.cookie(surveys.surveyTakenCookieName(emailSurvey))).not.toBe('true') }) - it('does not records any events', function () { - spyOn(surveys, 'trackEvent') - window.GOVUK.triggerEvent($('#email-survey-form')[0], 'submit') - expect(surveys.trackEvent).not.toHaveBeenCalled() - }) - it('adds the GA4 form complete ', function () { window.GOVUK.triggerEvent($('#email-survey-form')[0], 'submit') // For some reason the submit event triggers twice in tests, so we have to get the second . @@ -917,13 +857,6 @@ describe('Surveys', function () { expect($('#user-satisfaction-survey').hasClass('visible')).toBe(false) }) - it("records an event when clicking 'no thanks'", function () { - spyOn(surveys, 'trackEvent') - emailSurvey.surveyExpanded = true - $('#user-survey-cancel')[0].click() - expect(surveys.trackEvent).toHaveBeenCalledWith(emailSurvey.identifier, 'email_survey_cancel', 'Email survey cancelled') - }) - it("hides the email form when clicking 'take survey' if no email", function () { $('#take-survey')[0].click() expect(GOVUK.cookie(surveys.surveyTakenCookieName(emailSurvey))).toBe('true') @@ -933,12 +866,6 @@ describe('Surveys', function () { $('#take-survey')[0].click() expect($('#user-satisfaction-survey').hasClass('visible')).toBe(false) }) - - it("records an event when clicking 'take survey' if no email", function () { - spyOn(surveys, 'trackEvent') - $('#take-survey')[0].click() - expect(surveys.trackEvent).toHaveBeenCalledWith(emailSurvey.identifier, 'no_email_link', 'User taken survey via no email link') - }) }) }) })