From 0942162b467fce9d96a4c0046acb17d7b437f301 Mon Sep 17 00:00:00 2001
From: Eugen Neuber <eugen.neuber@tugraz.at>
Date: Wed, 16 Sep 2020 15:30:07 +0200
Subject: [PATCH] Simplify call to matomo.track()

See issue #29
---
 packages/app-shell/src/app-shell.js | 10 +++++++---
 packages/matomo/src/matomo.js       |  2 +-
 2 files changed, 8 insertions(+), 4 deletions(-)

diff --git a/packages/app-shell/src/app-shell.js b/packages/app-shell/src/app-shell.js
index 012fd97a..6fe7b6dc 100644
--- a/packages/app-shell/src/app-shell.js
+++ b/packages/app-shell/src/app-shell.js
@@ -61,6 +61,7 @@ export class AppShell extends ScopedElementsMixin(LitElement) {
 
         this.matomoUrl = '';
         this.matomoSiteId = -1;
+        this.matomo = null;
 
         this._attrObserver = new MutationObserver(this.onAttributeObserved);
     }
@@ -255,6 +256,10 @@ export class AppShell extends ScopedElementsMixin(LitElement) {
         this.initRouter();
 
         this._bus.subscribe('auth-update', this._updateAuth);
+
+        this.updateComplete.then(()=> {
+            this.matomo = this.shadowRoot.querySelector(this.constructor.getScopedTagName('dbp-matomo'));
+        });
     }
 
     disconnectedCallback() {
@@ -668,9 +673,8 @@ export class AppShell extends ScopedElementsMixin(LitElement) {
     }
 
     track(action, message) {
-        const matomo = this.shadowRoot.querySelector(this.constructor.getScopedTagName('dbp-matomo'));
-        if (matomo !== null) {
-            matomo.track(action, message);
+        if (this.matomo !== null) {
+            this.matomo.track(action, message);
         }
     }
 
diff --git a/packages/matomo/src/matomo.js b/packages/matomo/src/matomo.js
index 47702a41..17d9b793 100644
--- a/packages/matomo/src/matomo.js
+++ b/packages/matomo/src/matomo.js
@@ -107,7 +107,7 @@ export class MatomoElement extends DBPLitElement {
     }
 
     track (action, message) {
-        console.log('MatomoElement (' + this.isRunning + '): ' + action + ', ' + message);
+        console.log('MatomoElement  (' + this.isRunning + '): ' + action + ', ' + message);
         const event = ['trackEvent', action, message];
         if (this.isRunning) {
             _paq.push(event);
-- 
GitLab