Skip to content

Commit

Permalink
Save the analytics instance in the window object
Browse files Browse the repository at this point in the history
  • Loading branch information
ahosgood committed May 7, 2024
1 parent 4e2b0ae commit da314a5
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 6 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions src/nationalarchives/analytics.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "",
Expand All @@ -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 || [];
Expand All @@ -264,7 +264,7 @@ class GA4 extends EventTracker {
}

destroy() {
GA4._instance = null;
window.TNAFrontendAnalytics = null;
}

/** @protected */
Expand Down
2 changes: 1 addition & 1 deletion test/analytics.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "";

Expand Down

0 comments on commit da314a5

Please sign in to comment.