Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(): prevent initialization errors of matomo #1311

Merged
merged 5 commits into from
Jun 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/app/core/analytics/analytics.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
Expand Down
96 changes: 48 additions & 48 deletions src/app/core/analytics/analytics.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ export class AnalyticsService {
return md5(AppConfig.settings?.site_name + username);
}

private isInitialized = false;

constructor(
private angulartics2: Angulartics2,
private angulartics2Matomo: Angulartics2Matomo,
Expand All @@ -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,
Expand All @@ -47,61 +66,42 @@ export class AnalyticsService {
});
}

private setConfigValues() {
const { url, site_id, no_cookies } =
this.configService.getConfig<UsageAnalyticsConfig>(
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.
*
* 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);
private setConfigValues() {
const {
url,
site_id,
no_cookies,
} = this.configService.getConfig<UsageAnalyticsConfig>(
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]);
}
}

/**
Expand Down