diff --git a/CHANGELOG.md b/CHANGELOG.md index 9f1071b3..f290e630 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,7 +16,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Removed single `content` property in hero components in favour of separate fields (i.e. `title`, `text`/`body`) - Nested unordered lists use the same marker -- The Cookie library is now a singleton +- The cookie library is now a singleton +- The analytics library instance is saved to the window object - Improved typeface definitions and includes ### Deprecated diff --git a/src/nationalarchives/analytics.mjs b/src/nationalarchives/analytics.mjs index d9d01bc1..7cff9d9d 100644 --- a/src/nationalarchives/analytics.mjs +++ b/src/nationalarchives/analytics.mjs @@ -242,8 +242,8 @@ class GA4 extends EventTracker { gTagId; constructor(options = {}) { - if (GA4._instance) { - return GA4._instance; + if (window.TNAFrontendAnalytics) { + return window.TNAFrontendAnalytics; } const { id = "", @@ -252,7 +252,7 @@ class GA4 extends EventTracker { addTrackingCode = true, } = options; super({ prefix, addTrackingCode }); - GA4._instance = this; + window.TNAFrontendAnalytics = this; this.gTagId = id; this.ga4Disable = `ga-disable-${this.gTagId}`; window.dataLayer = window.dataLayer || []; @@ -264,7 +264,7 @@ class GA4 extends EventTracker { } destroy() { - GA4._instance = null; + window.TNAFrontendAnalytics = null; } /** @protected */ diff --git a/test/analytics.test.js b/test/analytics.test.js index 36d4b3a1..42649d60 100644 --- a/test/analytics.test.js +++ b/test/analytics.test.js @@ -73,7 +73,7 @@ describe("With consent", () => { document.clearAllCookies(); document.cookie = "cookies_policy=%7B%22usage%22%3Atrue%2C%22settings%22%3Atrue%2C%22essential%22%3Atrue%7D"; - GA4._instance = null; + window.TNAFrontendAnalytics = null; document.head.innerHTML = ""; document.body.innerHTML = "";