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/modules/global-bar.spec.js b/spec/javascripts/modules/global-bar.spec.js index 0788d8991..2b290d8d6 100644 --- a/spec/javascripts/modules/global-bar.spec.js +++ b/spec/javascripts/modules/global-bar.spec.js @@ -98,56 +98,9 @@ describe('Global bar module', function () { }) }) - describe('tracking global banner interactions', function () { - var element - - beforeEach(function () { - element = $( - '