Skip to content

Commit

Permalink
Add function to remove UA cookies
Browse files Browse the repository at this point in the history
This function simply gets rid of cookies associated with UA and our old UA properties.
  • Loading branch information
owenatgov committed Jul 1, 2024
1 parent d02bd6f commit e42b246
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
12 changes: 12 additions & 0 deletions __tests__/cookie-functions.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -301,4 +301,16 @@ describe('Cookie settings', () => {
expect(document.cookie).toEqual('')
})
})

describe('removeUACookies', () => {
it('removes cookies specifc to UA', () => {
document.cookie = '_gid=test'
document.cookie = '_gat_UA-26179049-17=test'
document.cookie = '_gat_UA-116229859-1=test'

CookieHelpers.removeUACookies()

expect(document.cookie).toEqual('')
})
})
})
7 changes: 6 additions & 1 deletion src/javascripts/application.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import BackToTop from './components/back-to-top.mjs'
import CookieBanner from './components/cookie-banner.mjs'
import {
getConsentCookie,
isValidConsentCookie
isValidConsentCookie,
removeUACookies
} from './components/cookie-functions.mjs'
import CookiesPage from './components/cookies-page.mjs'
import Copy from './components/copy.mjs'
Expand All @@ -34,6 +35,10 @@ if ($cookieBanner) {
const userConsent = getConsentCookie()
if (userConsent && isValidConsentCookie(userConsent) && userConsent.analytics) {
loadAnalytics()

// Remove UA cookies if the user previously had them set or Google attempts
// to set them
removeUACookies()
}

// Initialise example frames
Expand Down
21 changes: 21 additions & 0 deletions src/javascripts/components/cookie-functions.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,9 @@ export function resetCookies() {
window[`ga-disable-G-${TRACKING_PREVIEW_ID}`] = false
window[`ga-disable-G-${TRACKING_LIVE_ID}`] = false
loadAnalytics()

// Unset UA cookies if they've been set by GTM
removeUACookies()
} else {
// Disable GA if not allowed
window[`ga-disable-G-${TRACKING_PREVIEW_ID}`] = true
Expand All @@ -192,6 +195,24 @@ export function resetCookies() {
}
}

/**
* Remove UA cookies for user and prevent Google setting them.
*
* We've migrated our analytics from UA (Univesal Analytics) to GA4, however
* users may still have the UA cookie set from our previous implementation.
* Additionally, our UA properties are scheduled for deletion but until they are
* entirely deleted, GTM is still setting UA cookies.
*/
export function removeUACookies() {
for (const UACookie of [
'_gid',
'_gat_UA-26179049-17',
'_gat_UA-116229859-1'
]) {
Cookie(UACookie, null)
}
}

/**
* Check if user allows cookie category
*
Expand Down

0 comments on commit e42b246

Please sign in to comment.