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 e2cb8d03e4..e64a8cba52 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,37 +66,6 @@ export class AnalyticsService { }); } - private setConfigValues() { - const { url, site_id, no_cookies } = - this.configService.getConfig( - USAGE_ANALYTICS_CONFIG_ID - ) || {}; - 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) { - window["_paq"].push(["setSiteId", site_id]); - } - } - - /** - * 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. * @@ -85,23 +73,35 @@ export class AnalyticsService { * 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); + private setConfigValues() { + 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) { + const g = document.createElement("script"); + const s = document.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; + } + + window["_paq"].push(["setTrackerUrl", u + "matomo.php"]); + if (no_cookies) { + window["_paq"].push(["disableCookies"]); + } + if (site_id) { + window["_paq"].push(["setSiteId", site_id]); + } } /**