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

Remove Universal Analytics from static #3360

Merged
merged 1 commit into from
Jul 1, 2024
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
18 changes: 0 additions & 18 deletions app/assets/javascripts/modules/global-bar.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
})

Expand All @@ -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
Expand All @@ -73,18 +66,13 @@ window.GOVUK.Modules = window.GOVUK.Modules || {};
if (dismiss) {
dismiss.classList.remove('global-bar__dismiss--show')
}
track('Manually dismissed')
event.preventDefault()
}

function incrementViewCount (count) {
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 () {
Expand All @@ -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
Expand Down
54 changes: 16 additions & 38 deletions app/assets/javascripts/surveys.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@
' </label>' +
' <input name="email_survey_signup[survey_id]" type="hidden" value="{{surveyId}}">' +
' <input name="email_survey_signup[survey_source]" type="hidden" value="{{surveySource}}">' +
' <input name="email_survey_signup[ga_client_id]" type="hidden" value="{{gaClientId}}">' +
' <input class="survey-form-input" name="email_survey_signup[email_address]" id="survey-email-address" type="text" aria-describedby="survey-form-description">' +
' <button class="survey-form-button" type="submit">{{surveyFormCta}}</button>' +
takeSurveyLink('{{surveyFormNoEmailInvite}}') +
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -203,7 +201,6 @@
return
}
userSurveys.incrementSurveySeenCounter(survey)
userSurveys.trackEvent(survey.identifier, 'banner_shown', 'Banner has been shown')
},

displayURLSurvey: function (survey, surveyContainer) {
Expand All @@ -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) {
Expand All @@ -243,15 +236,13 @@
$takeSurvey.addEventListener('click', function () {
userSurveys.setSurveyTakenCookie(survey)
userSurveys.hideSurvey(survey)
userSurveys.trackEvent(survey.identifier, 'no_email_link', 'User taken survey via no email link')
})
}

if ($emailSurveyOpen) {
$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')
Expand All @@ -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()
})
Expand All @@ -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')
Expand Down Expand Up @@ -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()
})
Expand All @@ -362,7 +345,6 @@
$takeSurvey.addEventListener('click', function () {
userSurveys.setSurveyTakenCookie(survey)
userSurveys.hideSurvey(survey)
userSurveys.trackEvent(survey.identifier, 'banner_taken', 'User taken survey')
})
}
},
Expand Down Expand Up @@ -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 })
},
Expand Down Expand Up @@ -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)
}
}
}
}

Expand All @@ -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()
})()
20 changes: 0 additions & 20 deletions docs/analytics.md

This file was deleted.

5 changes: 1 addition & 4 deletions docs/surveys.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand Down Expand Up @@ -128,7 +126,6 @@ The template for a url survey is as follows:
</label>
<input name="email_survey_signup[survey_id]" type="hidden" value="{{surveyId}}">
<input name="email_survey_signup[survey_source]" type="hidden" value="{{surveySource}}">
<input name="email_survey_signup[ga_client_id]" type="hidden" value="{{gaClientId}}">
<input class="survey-form-input" name="email_survey_signup[email_address]" id="survey-email-address" type="text" aria-describedby="survey-form-description">
<button class="survey-form-button" type="submit">{{surveyFormCta}}</button>
<a href="javascript:void()" id="take-survey" target="_blank" rel="noopener noreferrer">{{surveyFormNoEmailInvite}}</a>
Expand Down
1 change: 0 additions & 1 deletion spec/javascripts/helpers/SpecHelper.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
beforeEach(function () {
window.GOVUK.loadAnalytics.loadUa()
jasmine.addMatchers({
toBeEqualAsJSON: function (util, customEqualityTesters) {
return {
Expand Down
47 changes: 0 additions & 47 deletions spec/javascripts/modules/global-bar.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,56 +98,9 @@ describe('Global bar module', function () {
})
})

describe('tracking global banner interactions', function () {
var element

beforeEach(function () {
element = $(
'<div id="global-bar" data-module="global-bar">' +
'<a href="/register-to-vote" class="govuk-link js-call-to-action">Register to Vote</a>' +
'<a href="#hide-message" class="govuk-link dismiss" role="button" aria-controls="global-bar">Hide message</a>' +
'</div>'
)

$(document.body).append(element)

document.cookie = 'global_bar_seen=; Path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT;'
spyOn(GOVUK.analytics, 'trackEvent')
})

it('tracks when dismiss link is clicked', function () {
globalBar = new GOVUK.Modules.GlobalBar(element[0])
globalBar.init()

$(element).find('.dismiss')[0].click()

expect(GOVUK.analytics.trackEvent).toHaveBeenCalledWith('Global bar', 'Manually dismissed', { nonInteraction: 1 })
})

it('tracks when clicking on a link marked with js-call-to-action', function () {
globalBar = new GOVUK.Modules.GlobalBar(element[0])
globalBar.init()

var link = $(element).find('.js-call-to-action')[0]
link.addEventListener('click', function (e) {
e.preventDefault()
})
link.click()

expect(GOVUK.analytics.trackEvent).toHaveBeenCalledWith('Global bar', '/register-to-vote', { nonInteraction: 1 })
})

it('tracks when global bar is seen', function () {
globalBar = new GOVUK.Modules.GlobalBar(element[0])
globalBar.init()
expect(GOVUK.CustomDimensions.getAndExtendDefaultTrackingOptions().dimension38.value).toBe('Global Banner viewed')
})
})

describe('always on', function () {
beforeEach(function () {
document.cookie = 'global_bar_seen=; Path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT;'
spyOn(GOVUK.analytics, 'trackEvent')
})

it('does not increment view count when on', function () {
Expand Down
Loading
Loading