Skip to content

Commit

Permalink
Load modules after analytics has loaded
Browse files Browse the repository at this point in the history
Some of the modules require Universal Analytics to exist, so we should load our analytics first to reduce the risk of crashing JavaScript. I have also ensured auto-scroll-tracker isn't relying on a different module initialising window.GOVUK.analyticsVars
  • Loading branch information
AshGDS committed Jan 13, 2023
1 parent 376a27f commit d0dc059
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
window.GOVUK = window.GOVUK || {}
window.GOVUK.Modules = window.GOVUK.Modules || {};
window.GOVUK.Modules = window.GOVUK.Modules || {}
window.GOVUK.analyticsVars = window.GOVUK.analyticsVars || {};

(function (Modules) {
function AutoScrollTracker ($module) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,18 @@
// = require ./modules.js

document.addEventListener('DOMContentLoaded', function () {
window.GOVUK.modules.start()
window.GOVUK.analyticsGa4 = window.GOVUK.analyticsGa4 || {}
window.GOVUK.analyticsVars = window.GOVUK.analyticsVars || {}

// if statements ensure these functions don't execute during testing
if (typeof window.GOVUK.loadAnalytics !== 'undefined') {
window.GOVUK.analyticsGa4 = window.GOVUK.analyticsGa4 || {}
if (typeof window.GOVUK.analyticsGa4.vars === 'undefined') {
window.GOVUK.loadAnalytics.loadGa4()
}
if (typeof window.GOVUK.analyticsVars === 'undefined') {
if (typeof window.GOVUK.analyticsVars.gaProperty === 'undefined') {
window.GOVUK.loadAnalytics.loadUa()
}
}

window.GOVUK.modules.start()
})

0 comments on commit d0dc059

Please sign in to comment.