Skip to content

Commit

Permalink
Remove the need for ga4-core to be .erb
Browse files Browse the repository at this point in the history
- turns out that using .erb like this is deprecated, so we should avoid it
- also because it stops such JS files from being linted, see #2982
- instead, we can get the value for the current gem version in static, where we're already using a .js.erb file, and next change this file back to being a normal .js file
- also clean up some of the tests and test data
  • Loading branch information
andysellick committed Sep 27, 2022
1 parent d28b8a1 commit e1593f1
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// note that because this file is .js.erb it is not linted
// temporarily rename to .js to check with the linter
window.GOVUK = window.GOVUK || {}
window.GOVUK.analyticsGa4 = window.GOVUK.analyticsGa4 || {};

Expand Down Expand Up @@ -55,7 +53,7 @@ window.GOVUK.analyticsGa4 = window.GOVUK.analyticsGa4 || {};
},

getGemVersion: function () {
return '<%= GovukPublishingComponents::VERSION %>'
return window.GOVUK.analyticsGA4.vars.gem_version || 'not found'
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@ describe('GA4 core', function () {
var GOVUK = window.GOVUK

beforeEach(function () {
window.GOVUK.analyticsGa4.vars = {}
window.dataLayer = []
window.GOVUK.analyticsGa4.vars = {}
})

afterEach(function () {
window.GOVUK.analyticsGa4.vars = null
window.dataLayer = []
window.GOVUK.analyticsGa4.vars.id = 'test-id'
window.GOVUK.analyticsGa4.vars.auth = 'test-auth'
window.GOVUK.analyticsGa4.vars.preview = 'test-preview'
window.GOVUK.analyticsGa4.vars.gem_version = 'gem-version'
window.GOVUK.analyticsGa4.vars.gtag_id = null
})

it('loads the GTM snippet', function () {
Expand All @@ -25,19 +29,19 @@ describe('GA4 core', function () {

describe('calls the right URL from Google', function () {
it('if all three env vars are present', function () {
window.GOVUK.analyticsGa4.vars.id = 'myId'
window.GOVUK.analyticsGa4.vars.auth = 'myAuth'
window.GOVUK.analyticsGa4.vars.preview = 'myPreview'
window.GOVUK.analyticsGa4.vars.id = 'test-id'
window.GOVUK.analyticsGa4.vars.auth = 'test-auth'
window.GOVUK.analyticsGa4.vars.preview = 'test-preview'
GOVUK.analyticsGa4.core.load()

expect(GOVUK.analyticsGa4.core.googleSrc).toEqual('https://www.googletagmanager.com/gtm.js?id=myId&gtm_auth=myAuth&gtm_preview=myPreview&gtm_cookies_win=x')
expect(GOVUK.analyticsGa4.core.googleSrc).toEqual('https://www.googletagmanager.com/gtm.js?id=test-id&gtm_auth=test-auth&gtm_preview=test-preview&gtm_cookies_win=x')
})

it('if only id is present', function () {
window.GOVUK.analyticsGa4.vars.id = 'myId'
window.GOVUK.analyticsGa4.vars.id = 'test-id'
GOVUK.analyticsGa4.core.load()

expect(GOVUK.analyticsGa4.core.googleSrc).toEqual('https://www.googletagmanager.com/gtm.js?id=myId')
expect(GOVUK.analyticsGa4.core.googleSrc).toEqual('https://www.googletagmanager.com/gtm.js?id=test-id')
})
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@
describe('Initialising GA4', function () {
var GOVUK = window.GOVUK

beforeEach(function () {
window.GOVUK.analyticsGa4.vars = window.GOVUK.analyticsGa4.vars || {}
})

afterEach(function () {
GOVUK.analyticsGa4.analyticsModules.Ga4LinkTracker.stopTracking()
window.dataLayer = []
Expand Down
7 changes: 4 additions & 3 deletions spec/javascripts/helpers/SpecHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ beforeAll(function () {
window.GOVUK.analyticsVars.linkedDomains = ['www.gov.uk']

window.GOVUK.analyticsGa4.vars = window.GOVUK.analyticsGa4.vars || {}
window.GOVUK.analyticsGa4.vars.id = 'GTM-test'
window.GOVUK.analyticsGa4.vars.auth = 'test'
window.GOVUK.analyticsGa4.vars.preview = 'env-test'
window.GOVUK.analyticsGa4.vars.id = 'test-id'
window.GOVUK.analyticsGa4.vars.auth = 'test-auth'
window.GOVUK.analyticsGa4.vars.preview = 'test-preview'
window.GOVUK.analyticsGa4.vars.gem_version = 'gem-version'

delete ga
window.GOVUK.analyticsInit()
Expand Down

0 comments on commit e1593f1

Please sign in to comment.