From 5335b8256017d68825cbfeb3418f16f9e22aba58 Mon Sep 17 00:00:00 2001 From: Simon Date: Wed, 15 Jun 2022 11:51:58 +0200 Subject: [PATCH 1/4] fix(): prevent initialization errors of matomo --- src/app/core/analytics/analytics.service.ts | 88 ++++++++++----------- 1 file changed, 44 insertions(+), 44 deletions(-) diff --git a/src/app/core/analytics/analytics.service.ts b/src/app/core/analytics/analytics.service.ts index e2cb8d03e4..21ed0ec056 100644 --- a/src/app/core/analytics/analytics.service.ts +++ b/src/app/core/analytics/analytics.service.ts @@ -23,6 +23,8 @@ export class AnalyticsService { return md5(AppConfig.settings?.site_name + username); } + private isInitialized = false; + constructor( private angulartics2: Angulartics2, private angulartics2Matomo: Angulartics2Matomo, @@ -35,6 +37,23 @@ export class AnalyticsService { ); } + /** + * Set up usage analytics tracking - if the AppConfig specifies the required settings. + */ + init(): void { + window["_paq"] = window["_paq"] || []; + window["_paq"].push([ + "setDocumentTitle", + document.domain + "/" + document.title, + ]); + window["_paq"].push(["trackPageView"]); + window["_paq"].push(["enableLinkTracking"]); + this.setVersion(); + this.setOrganization(AppConfig.settings.site_name); + this.setUser(undefined); + this.configService.configUpdates.subscribe(() => this.setConfigValues()); + } + private setVersion(): void { this.angulartics2.setUserProperties.next({ dimension1: "ndb-core@" + environment.appVersion, @@ -47,16 +66,37 @@ export class AnalyticsService { }); } + /** + * dynamically sets up everything for Matomo. + * + * The code is inspired by: + * https://github.com/Arnaud73/ngx-matomo/blob/master/projects/ngx-matomo/src/lib/matomo-injector.service.ts + * @private + */ private setConfigValues() { const { url, site_id, no_cookies } = - this.configService.getConfig( - USAGE_ANALYTICS_CONFIG_ID - ) || {}; + this.configService.getConfig( + USAGE_ANALYTICS_CONFIG_ID + ) || {}; + const u = url.endsWith("/") ? url : url + "/"; + + if (!this.isInitialized) { + const d = document; + const g = d.createElement("script"); + const s = d.getElementsByTagName("script")[0]; + g.type = "text/javascript"; + g.async = true; + g.defer = true; + g.src = u + "matomo.js"; + s.parentNode.insertBefore(g, s); + this.angulartics2Matomo.startTracking(); + this.isInitialized = true; + } + if (no_cookies) { window["_paq"].push(["disableCookies"]); } if (url) { - const u = url.endsWith("/") ? url : url + "/"; window["_paq"].push(["setTrackerUrl", u + "matomo.php"]); } if (site_id) { @@ -64,46 +104,6 @@ export class AnalyticsService { } } - /** - * Set up usage analytics tracking - if the AppConfig specifies the required settings. - */ - init(): void { - this.setUpMatomo(); - - this.setVersion(); - this.setOrganization(AppConfig.settings.site_name); - this.setUser(undefined); - this.configService.configUpdates.subscribe(() => this.setConfigValues()); - - this.angulartics2Matomo.startTracking(); - } - - /** - * dynamically sets up everything for Matomo. - * - * The code is inspired by: - * https://github.com/Arnaud73/ngx-matomo/blob/master/projects/ngx-matomo/src/lib/matomo-injector.service.ts - * @private - */ - private setUpMatomo() { - window["_paq"] = window["_paq"] || []; - window["_paq"].push([ - "setDocumentTitle", - document.domain + "/" + document.title, - ]); - window["_paq"].push(["trackPageView"]); - window["_paq"].push(["enableLinkTracking"]); - const d = document; - const g = d.createElement("script"); - const s = d.getElementsByTagName("script")[0]; - g.type = "text/javascript"; - g.async = true; - g.defer = true; - // TODO this should be configurable - g.src = "https://matomo.aam-digital.org/matomo.js"; - s.parentNode.insertBefore(g, s); - } - /** * Explicitly record a user action * @param action String identifying the action From ab189a97cf7c7e25a4e32cd02d90c6d076f8e55e Mon Sep 17 00:00:00 2001 From: Simon Date: Wed, 15 Jun 2022 12:04:45 +0200 Subject: [PATCH 2/4] fixed styling issues --- src/app/core/analytics/analytics.service.ts | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/app/core/analytics/analytics.service.ts b/src/app/core/analytics/analytics.service.ts index 21ed0ec056..7bffb5bfa7 100644 --- a/src/app/core/analytics/analytics.service.ts +++ b/src/app/core/analytics/analytics.service.ts @@ -75,15 +75,14 @@ export class AnalyticsService { */ private setConfigValues() { const { url, site_id, no_cookies } = - this.configService.getConfig( - USAGE_ANALYTICS_CONFIG_ID - ) || {}; + this.configService.getConfig( + USAGE_ANALYTICS_CONFIG_ID + ) || {}; const u = url.endsWith("/") ? url : url + "/"; if (!this.isInitialized) { - const d = document; - const g = d.createElement("script"); - const s = d.getElementsByTagName("script")[0]; + const g = document.createElement("script"); + const s = document.getElementsByTagName("script")[0]; g.type = "text/javascript"; g.async = true; g.defer = true; From 9027f009ba7d3c753d9fbd9e22447dd1b54aa6ee Mon Sep 17 00:00:00 2001 From: Simon Date: Wed, 15 Jun 2022 12:28:49 +0200 Subject: [PATCH 3/4] fixed failing tests --- src/app/core/analytics/analytics.service.spec.ts | 3 ++- src/app/core/analytics/analytics.service.ts | 11 +++++++---- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/app/core/analytics/analytics.service.spec.ts b/src/app/core/analytics/analytics.service.spec.ts index 5dc728491b..1047d21c6f 100644 --- a/src/app/core/analytics/analytics.service.spec.ts +++ b/src/app/core/analytics/analytics.service.spec.ts @@ -61,8 +61,9 @@ describe("AnalyticsService", () => { expect(mockMatomo.startTracking).not.toHaveBeenCalled(); }); - it("should start tracking after calling init", () => { + it("should start tracking after calling init and config is loaded", () => { service.init(); + configUpdates.next(new Config()); expect(mockMatomo.startTracking).toHaveBeenCalled(); }); diff --git a/src/app/core/analytics/analytics.service.ts b/src/app/core/analytics/analytics.service.ts index 7bffb5bfa7..032147faa4 100644 --- a/src/app/core/analytics/analytics.service.ts +++ b/src/app/core/analytics/analytics.service.ts @@ -74,10 +74,13 @@ export class AnalyticsService { * @private */ private setConfigValues() { - const { url, site_id, no_cookies } = - this.configService.getConfig( - USAGE_ANALYTICS_CONFIG_ID - ) || {}; + const { + url, + site_id, + no_cookies, + } = this.configService.getConfig( + USAGE_ANALYTICS_CONFIG_ID + ) || { url: "https://matomo.aam-digital.org" }; const u = url.endsWith("/") ? url : url + "/"; if (!this.isInitialized) { From 69234da9d651fa9433ea7ef2cc595b2a6ae9ad74 Mon Sep 17 00:00:00 2001 From: Simon Date: Wed, 15 Jun 2022 12:29:15 +0200 Subject: [PATCH 4/4] removed unneeded check --- src/app/core/analytics/analytics.service.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/app/core/analytics/analytics.service.ts b/src/app/core/analytics/analytics.service.ts index 032147faa4..e64a8cba52 100644 --- a/src/app/core/analytics/analytics.service.ts +++ b/src/app/core/analytics/analytics.service.ts @@ -95,12 +95,10 @@ export class AnalyticsService { this.isInitialized = true; } + window["_paq"].push(["setTrackerUrl", u + "matomo.php"]); if (no_cookies) { window["_paq"].push(["disableCookies"]); } - if (url) { - window["_paq"].push(["setTrackerUrl", u + "matomo.php"]); - } if (site_id) { window["_paq"].push(["setSiteId", site_id]); }